summaryrefslogtreecommitdiff
path: root/migrate/changeset/databases/sqlite.py
diff options
context:
space:
mode:
authoriElectric <unknown>2009-06-27 14:13:27 +0000
committeriElectric <unknown>2009-06-27 14:13:27 +0000
commit9f7ab96881415ec6bd7fc7729f5196d75d01b8ab (patch)
tree3eef10782c578b10b1e2186fcec0d07aa43d15ba /migrate/changeset/databases/sqlite.py
parenta8c31eb25f89542c9be72d0a8f90b4d984b6aead (diff)
downloadsqlalchemy-migrate-9f7ab96881415ec6bd7fc7729f5196d75d01b8ab.tar.gz
- completely refactored ColumnDelta to extract differences between columns/parameters (also fixes issue #23)
- fixed some bugs (passing server_default) on column.alter - updated tests, specially ColumnDelta and column.alter - introduced alter_metadata which can preserve altering existing objects if False (defaults to True) - updated documentation
Diffstat (limited to 'migrate/changeset/databases/sqlite.py')
-rw-r--r--migrate/changeset/databases/sqlite.py28
1 files changed, 20 insertions, 8 deletions
diff --git a/migrate/changeset/databases/sqlite.py b/migrate/changeset/databases/sqlite.py
index fa9f381..479cf45 100644
--- a/migrate/changeset/databases/sqlite.py
+++ b/migrate/changeset/databases/sqlite.py
@@ -3,6 +3,9 @@
.. _`SQLite`: http://www.sqlite.org/
"""
+from UserDict import DictMixin
+from copy import copy
+
from sqlalchemy.databases import sqlite as sa_base
from migrate.changeset import ansisql, exceptions
@@ -19,18 +22,25 @@ class SQLiteCommon(object):
class SQLiteHelper(SQLiteCommon):
- def visit_column(self, column):
- table = self._to_table(column.table)
+ def visit_column(self, delta):
+ if isinstance(delta, DictMixin):
+ column = delta.result_column
+ table = self._to_table(delta.table)
+ else:
+ column = delta
+ table = self._to_table(column.table)
table_name = self.preparer.format_table(table)
# we remove all constraints, indexes so it doesnt recreate them
+ ixbackup = copy(table.indexes)
+ consbackup = copy(table.constraints)
table.indexes = set()
table.constraints = set()
self.append('ALTER TABLE %s RENAME TO migration_tmp' % table_name)
self.execute()
- insertion_string = self._modify_table(table, column)
+ insertion_string = self._modify_table(table, column, delta)
table.create()
self.append(insertion_string % {'table_name': table_name})
@@ -38,6 +48,10 @@ class SQLiteHelper(SQLiteCommon):
self.append('DROP TABLE migration_tmp')
self.execute()
+ # restore indexes, constraints
+ table.indexes = ixbackup
+ table.constraints = consbackup
+
class SQLiteColumnGenerator(SQLiteSchemaGenerator, SQLiteCommon,
ansisql.ANSIColumnGenerator):
@@ -51,7 +65,7 @@ class SQLiteColumnGenerator(SQLiteSchemaGenerator, SQLiteCommon,
class SQLiteColumnDropper(SQLiteHelper, ansisql.ANSIColumnDropper):
"""SQLite ColumnDropper"""
- def _modify_table(self, table, column):
+ def _modify_table(self, table, column, delta):
columns = ' ,'.join(map(self.preparer.format_column, table.columns))
return 'INSERT INTO %(table_name)s SELECT ' + columns + \
' from migration_tmp'
@@ -60,11 +74,8 @@ class SQLiteColumnDropper(SQLiteHelper, ansisql.ANSIColumnDropper):
class SQLiteSchemaChanger(SQLiteHelper, ansisql.ANSISchemaChanger):
"""SQLite SchemaChanger"""
- def _modify_table(self, table, column):
- delta = column.delta
+ def _modify_table(self, table, column, delta):
column = table.columns[delta.current_name]
- for k, v in delta.items():
- setattr(column, k, v)
return 'INSERT INTO %(table_name)s SELECT * from migration_tmp'
def visit_index(self, index):
@@ -94,6 +105,7 @@ class SQLiteConstraintDropper(ansisql.ANSIColumnDropper, ansisql.ANSIConstraintC
self.execute()
# TODO: add not_supported tags for constraint dropper/generator
+# TODO: technically primary key is a NOT NULL + UNIQUE constraint, should add NOT NULL to index
class SQLiteDialect(ansisql.ANSIDialect):
columngenerator = SQLiteColumnGenerator