1 | from migrate.versioning.script import base |
---|
2 | |
---|
3 | class SqlScript(base.BaseScript): |
---|
4 | """A file containing plain SQL statements.""" |
---|
5 | def run(self, engine, step): |
---|
6 | text = self.source() |
---|
7 | # Don't rely on SA's autocommit here |
---|
8 | # (SA uses .startswith to check if a commit is needed. What if script |
---|
9 | # starts with a comment?) |
---|
10 | conn = engine.connect() |
---|
11 | try: |
---|
12 | trans = conn.begin() |
---|
13 | try: |
---|
14 | # ###HACK: SQLite doesn't allow multiple statements through |
---|
15 | # its execute() method, but it provides executescript() instead |
---|
16 | dbapi = conn.engine.raw_connection() |
---|
17 | if getattr(dbapi, 'executescript', None): |
---|
18 | dbapi.executescript(text) |
---|
19 | else: |
---|
20 | conn.execute(text) |
---|
21 | # Success |
---|
22 | trans.commit() |
---|
23 | except: |
---|
24 | trans.rollback() |
---|
25 | raise |
---|
26 | finally: |
---|
27 | conn.close() |
---|