Use legacy_alter_table ON in sqlite recreate_table

Use "PRAGMA legacy_alter_table = ON" with sqlite >= 3.26 when
using "ALTER TABLE RENAME TO migration_tmp" to maintain legacy
behavior.

As of sqlite version 3.26, when a table is renamed using
"ALTER TABLE RENAME TO", REFERENCES clauses that refer to the
table will be updated. To maintain legacy (3.24 and earlier)
behavior, "PRAGMA legacy_alter_table" can be set to true and
"PRAGMA foreign_keys" can be set to false. [1]

[1] https://www.sqlite.org/src/info/ae9638e9c0ad0c36

Thanks to "László Böszörményi (GCS)" <gcs@debian.org> for
providing the code for this patch, which has since been
slightly modified.

Change-Id: I539988ab2ad6df6c8f423ecec15364ad8fcc7267
Closes-Bug: 1807262
This commit is contained in:
Corey Bryant 2018-12-07 13:49:20 -05:00
parent 02c26a2ced
commit 4a7f9c46cb
1 changed files with 8 additions and 0 deletions

View File

@ -9,6 +9,7 @@ except ImportError: # Python 2
from UserDict import DictMixin
from copy import copy
import re
import sqlite3
from sqlalchemy.databases import sqlite as sa_base
from sqlalchemy.schema import ForeignKeyConstraint
@ -96,8 +97,15 @@ class SQLiteHelper(SQLiteCommon):
if omit_constraints is None or cons.name not in omit_constraints
])
tup = sqlite3.sqlite_version_info
if tup[0] > 3 or (tup[0] == 3 and tup[1] >= 26):
self.append('PRAGMA legacy_alter_table = ON')
self.execute()
self.append('ALTER TABLE %s RENAME TO migration_tmp' % table_name)
self.execute()
if tup[0] > 3 or (tup[0] == 3 and tup[1] >= 26):
self.append('PRAGMA legacy_alter_table = OFF')
self.execute()
insertion_string = self._modify_table(table, column, delta)