Browse files

schema-evolution:

added sqlite3 unit tests
greatly expanded the number of evolutions supported by the sqlite3 backend
changed all get_<evolution_type> calls to return lists of strings
fixed sqlite3 get_indexes introspection



git-svn-id: http://code.djangoproject.com/svn/django/branches/schema-evolution@5787 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
1 parent 38c1cd7 commit fd77e425091fdeaf321ca7658534c2f906074084 @keredson keredson committed Aug 2, 2007
View
12 django/core/management.py
@@ -569,7 +569,7 @@ def get_sql_evolution_check_for_new_fields(klass, new_table_name):
data_type = f.get_internal_type()
col_type = data_types[data_type]
if col_type is not None:
- output.append( backend.get_add_column_sql( db_table, f.column, style.SQL_COLTYPE(col_type % rel_field.__dict__), f.null, f.unique, f.primary_key ) )
+ output.extend( backend.get_add_column_sql( db_table, f.column, style.SQL_COLTYPE(col_type % rel_field.__dict__), f.null, f.unique, f.primary_key ) )
return output
def get_sql_evolution_check_for_changed_model_name(klass):
@@ -580,9 +580,9 @@ def get_sql_evolution_check_for_changed_model_name(klass):
if klass._meta.db_table in table_list:
return [], None
if klass._meta.aka in table_list:
- return [ backend.get_change_table_name_sql( klass._meta.db_table, klass._meta.aka) ], klass._meta.aka
+ return backend.get_change_table_name_sql( klass._meta.db_table, klass._meta.aka), klass._meta.aka
elif len(set(klass._meta.aka) & set(table_list))==1:
- return [ backend.get_change_table_name_sql( klass._meta.db_table, klass._meta.aka[0]) ], klass._meta.aka[0]
+ return backend.get_change_table_name_sql( klass._meta.db_table, klass._meta.aka[0]), klass._meta.aka[0]
else:
return [], None
@@ -608,14 +608,12 @@ def get_sql_evolution_check_for_changed_field_name(klass, new_table_name):
data_type = f.get_internal_type()
col_type = data_types[data_type]
if col_type is not None:
- field_output = []
col_def = style.SQL_COLTYPE(col_type % rel_field.__dict__) +' '+ style.SQL_KEYWORD('%sNULL' % (not f.null and 'NOT ' or ''))
if f.unique:
col_def += style.SQL_KEYWORD(' UNIQUE')
if f.primary_key:
col_def += style.SQL_KEYWORD(' PRIMARY KEY')
- field_output.append( backend.get_change_column_name_sql( klass._meta.db_table, introspection.get_indexes(cursor,db_table), backend.quote_name(old_col), backend.quote_name(f.column), col_def ) )
- output.append(' '.join(field_output))
+ output.extend( backend.get_change_column_name_sql( klass._meta.db_table, introspection.get_indexes(cursor,db_table), old_col, f.column, col_def ) )
return output
def get_sql_evolution_check_for_changed_field_flags(klass, new_table_name):
@@ -666,7 +664,7 @@ def get_sql_evolution_check_for_changed_field_flags(klass, new_table_name):
# col_def += ' '+ style.SQL_KEYWORD('UNIQUE')
# if f.primary_key:
# col_def += ' '+ style.SQL_KEYWORD('PRIMARY KEY')
- output.append( backend.get_change_column_def_sql( db_table, cf, col_type_def, f.null, f.unique, f.primary_key ) )
+ output.extend( backend.get_change_column_def_sql( db_table, cf, col_type_def, f.null, f.unique, f.primary_key ) )
#print db_table, cf, f.maxlength, introspection.get_known_column_flags(cursor, db_table, cf)
return output
View
8 django/db/backends/mysql/base.py
@@ -244,7 +244,7 @@ def get_sql_sequence_reset(style, model_list):
return []
def get_change_table_name_sql( table_name, old_table_name ):
- return 'ALTER TABLE '+ quote_name(old_table_name) +' RENAME TO '+ quote_name(table_name) + ';'
+ return ['ALTER TABLE '+ quote_name(old_table_name) +' RENAME TO '+ quote_name(table_name) + ';']
def get_change_column_name_sql( table_name, indexes, old_col_name, new_col_name, col_def ):
# mysql doesn't support column renames (AFAIK), so we fake it
@@ -254,7 +254,7 @@ def get_change_column_name_sql( table_name, indexes, old_col_name, new_col_name,
if indexes[key]['primary_key']: pk_name = key
output = []
output.append( 'ALTER TABLE '+ quote_name(table_name) +' CHANGE COLUMN '+ quote_name(old_col_name) +' '+ quote_name(new_col_name) +' '+ col_def + ';' )
- return '\n'.join(output)
+ return output
def get_change_column_def_sql( table_name, col_name, col_type, null, unique, primary_key ):
output = []
@@ -264,7 +264,7 @@ def get_change_column_def_sql( table_name, col_name, col_type, null, unique, pri
if primary_key:
col_def += ' '+ 'PRIMARY KEY'
output.append( 'ALTER TABLE '+ quote_name(table_name) +' MODIFY COLUMN '+ quote_name(col_name) +' '+ col_def + ';' )
- return '\n'.join(output)
+ return output
def get_add_column_sql( table_name, col_name, col_type, null, unique, primary_key ):
output = []
@@ -280,7 +280,7 @@ def get_add_column_sql( table_name, col_name, col_type, null, unique, primary_ke
if primary_key:
field_output.append(('PRIMARY KEY'))
output.append(' '.join(field_output) + ';')
- return '\n'.join(output)
+ return output
def get_drop_column_sql( table_name, col_name ):
output = []
View
8 django/db/backends/postgresql/base.py
@@ -286,7 +286,7 @@ def typecast_string(s):
def get_change_table_name_sql( table_name, old_table_name ):
output = []
output.append('ALTER TABLE '+ quote_name(old_table_name) +' RENAME TO '+ quote_name(table_name) + ';')
- return '\n'.join(output)
+ return output
def get_change_column_name_sql( table_name, indexes, old_col_name, new_col_name, col_def ):
# TODO: only supports a single primary key so far
@@ -295,7 +295,7 @@ def get_change_column_name_sql( table_name, indexes, old_col_name, new_col_name,
if indexes[key]['primary_key']: pk_name = key
output = []
output.append( 'ALTER TABLE '+ quote_name(table_name) +' RENAME COLUMN '+ quote_name(old_col_name) +' TO '+ quote_name(new_col_name) +';' )
- return '\n'.join(output)
+ return output
def get_change_column_def_sql( table_name, col_name, col_type, null, unique, primary_key ):
output = []
@@ -308,7 +308,7 @@ def get_change_column_def_sql( table_name, col_name, col_type, null, unique, pri
if unique:
output.append( 'ALTER TABLE '+ quote_name(table_name) +' ADD CONSTRAINT '+ table_name +'_'+ col_name +'_unique_constraint UNIQUE('+ col_name +');' )
- return '\n'.join(output)
+ return output
def get_add_column_sql( table_name, col_name, col_type, null, unique, primary_key ):
output = []
@@ -317,7 +317,7 @@ def get_add_column_sql( table_name, col_name, col_type, null, unique, primary_ke
output.append( 'ALTER TABLE '+ quote_name(table_name) +' ALTER COLUMN '+ quote_name(col_name) +' SET NOT NULL;' )
if unique:
output.append( 'ALTER TABLE '+ quote_name(table_name) +' ADD CONSTRAINT '+ table_name +'_'+ col_name +'_unique_constraint UNIQUE('+ col_name +');' )
- return '\n'.join(output)
+ return output
def get_drop_column_sql( table_name, col_name ):
output = []
View
63 django/db/backends/sqlite3/base.py
@@ -217,22 +217,61 @@ def _sqlite_regexp(re_pattern, re_string):
return False
def get_change_table_name_sql( table_name, old_table_name ):
- return 'ALTER TABLE '+ quote_name(old_table_name) +' RENAME TO '+ quote_name(table_name) + ';'
+ return ['ALTER TABLE '+ quote_name(old_table_name) +' RENAME TO '+ quote_name(table_name) + ';']
def get_change_column_name_sql( table_name, indexes, old_col_name, new_col_name, col_def ):
# sqlite doesn't support column renames, so we fake it
- # TODO: only supports a single primary key so far
- pk_name = None
- for key in indexes.keys():
- if indexes[key]['primary_key']: pk_name = key
+ model = get_model_from_table_name(table_name)
output = []
- output.append( 'ALTER TABLE '+ quote_name(table_name) +' ADD COLUMN '+ quote_name(new_col_name) +' '+ col_def + ';' )
- output.append( 'UPDATE '+ quote_name(table_name) +' SET '+ new_col_name +' = '+ old_col_name +' WHERE '+ pk_name +'=(select '+ pk_name +' from '+ table_name +');' )
- output.append( '-- FYI: sqlite does not support deleting columns, so '+ quote_name(old_col_name) +' remains as cruft' )
- return '\n'.join(output)
+ output.append( '-- FYI: sqlite does not support renaming columns, so we create a new '+ quote_name(table_name) +' and delete the old (ie, this could take a while)' )
+
+ tmp_table_name = table_name + '_1337_TMP' # unlikely to produce a namespace conflict
+ output.extend( get_change_table_name_sql( tmp_table_name, table_name ) )
+ output.extend( management._get_sql_model_create(model, set())[0] )
+
+ old_cols = []
+ for f in model._meta.fields:
+ if f.column != new_col_name:
+ old_cols.append( quote_name(f.column) )
+ else:
+ old_cols.append( quote_name(old_col_name) )
+
+ output.append( 'INSERT INTO '+ quote_name(table_name) +' SELECT '+ ','.join(old_cols) +' FROM '+ quote_name(tmp_table_name) +';' )
+ output.append( 'DROP TABLE '+ quote_name(tmp_table_name) +';' )
+
+ return output
def get_change_column_def_sql( table_name, col_name, col_type, null, unique, primary_key ):
# sqlite doesn't support column modifications, so we fake it
+
+ model = get_model_from_table_name(table_name)
+ if not model: return ['-- model not found']
+ output = []
+ output.append( '-- FYI: sqlite does not support changing columns, so we create a new '+ quote_name(table_name) +' and delete the old (ie, this could take a while)' )
+
+ tmp_table_name = table_name + '_1337_TMP' # unlikely to produce a namespace conflict
+ output.extend( get_change_table_name_sql( tmp_table_name, table_name ) )
+ output.extend( management._get_sql_model_create(model, set())[0] )
+
+ old_cols = []
+ for f in model._meta.fields:
+ old_cols.append( quote_name(f.column) )
+
+ output.append( 'INSERT INTO '+ quote_name(table_name) +' SELECT '+ ','.join(old_cols) +' FROM '+ quote_name(tmp_table_name) +';' )
+ output.append( 'DROP TABLE '+ quote_name(tmp_table_name) +';' )
+
+ return output
+
+
+
+
+
+
+
+
+
+
+
output = []
col_def = col_type +' '+ ('%sNULL' % (not null and 'NOT ' or ''))
if unique or primary_key:
@@ -241,7 +280,7 @@ def get_change_column_def_sql( table_name, col_name, col_type, null, unique, pri
col_def += ' '+ 'PRIMARY KEY'
# TODO: fake via renaming the table, building a new one and deleting the old
output.append('-- sqlite does not support column modifications '+ quote_name(table_name) +'.'+ quote_name(col_name) +' to '+ col_def)
- return '\n'.join(output)
+ return output
def get_add_column_sql( table_name, col_name, col_type, null, unique, primary_key ):
output = []
@@ -257,14 +296,14 @@ def get_add_column_sql( table_name, col_name, col_type, null, unique, primary_ke
if primary_key:
field_output.append(('PRIMARY KEY'))
output.append(' '.join(field_output) + ';')
- return '\n'.join(output)
+ return output
def get_drop_column_sql( table_name, col_name ):
model = get_model_from_table_name(table_name)
output = []
output.append( '-- FYI: sqlite does not support deleting columns, so we create a new '+ quote_name(col_name) +' and delete the old (ie, this could take a while)' )
tmp_table_name = table_name + '_1337_TMP' # unlikely to produce a namespace conflict
- output.append( get_change_table_name_sql( tmp_table_name, table_name ) )
+ output.extend( get_change_table_name_sql( tmp_table_name, table_name ) )
output.extend( management._get_sql_model_create(model, set())[0] )
new_cols = []
for f in model._meta.fields:
View
18 django/db/backends/sqlite3/introspection.py
@@ -26,21 +26,23 @@ def get_indexes(cursor, table_name):
'unique': boolean representing whether it's a unique index}
"""
indexes = {}
- for info in _table_info(cursor, table_name):
- indexes[info['name']] = {'primary_key': info['pk'] != 0,
- 'unique': False}
cursor.execute('PRAGMA index_list(%s)' % quote_name(table_name))
# seq, name, unique
for index, unique in [(field[1], field[2]) for field in cursor.fetchall()]:
if not unique:
continue
cursor.execute('PRAGMA index_info(%s)' % quote_name(index))
info = cursor.fetchall()
- # Skip indexes across multiple fields
- if len(info) != 1:
- continue
- name = info[0][2] # seqno, cid, name
- indexes[name]['unique'] = True
+ for x in info:
+ name = x[2] # seqno, cid, name
+ cursor.execute('PRAGMA table_info(%s)' % quote_name(table_name))
+ for row in cursor.fetchall():
+ if row[1]==name:
+ indexes[name] = {'primary_key': False, 'unique': False}
+ if row[2]=='integer':
+ indexes[name]['primary_key'] = True
+ else:
+ indexes[name]['unique'] = True
return indexes
def get_columns(cursor, table_name):
View
88 tests/modeltests/schema_evolution/models.py
@@ -44,8 +44,8 @@ class Meta:
# delete a column, so it looks like we've recently added a field
>>> sql = backend.get_drop_column_sql( 'schema_evolution_person', 'gender' )
>>> print sql
-ALTER TABLE `schema_evolution_person` DROP COLUMN `gender`;
->>> cursor.execute(sql)
+['ALTER TABLE `schema_evolution_person` DROP COLUMN `gender`;']
+>>> for s in sql: cursor.execute(s)
0L
>>> management.get_sql_evolution(app)
['ALTER TABLE `schema_evolution_person` ADD COLUMN `gender` varchar(1) NOT NULL;']
@@ -58,7 +58,7 @@ class Meta:
>>> cursor.execute('ALTER TABLE `schema_evolution_person` ADD COLUMN `gender_nothere` varchar(1) NOT NULL;')
0L
>>> management.get_sql_evolution(app)
-['-- warning: as the following may cause data loss, it/they must be run manually', u'-- ALTER TABLE `schema_evolution_person` DROP COLUMN `gender_nothere`;', '-- end warning']
+['-- warning: the following may cause data loss', u'ALTER TABLE `schema_evolution_person` DROP COLUMN `gender_nothere`;', '-- end warning']
# reset the db
>>> cursor.execute('DROP TABLE schema_evolution_person;'); cursor.execute(create_table_sql[0])
@@ -103,41 +103,41 @@ class Meta:
[]
# delete a column, so it looks like we've recently added a field
->>> cursor.execute( backend.get_drop_column_sql( 'schema_evolution_person', 'gender' ) )
+>>> for sql in backend.get_drop_column_sql( 'schema_evolution_person', 'gender' ): cursor.execute(sql)
>>> management.get_sql_evolution(app)
-['ALTER TABLE "schema_evolution_person" ADD COLUMN "gender" varchar(1);\\nALTER TABLE "schema_evolution_person" ALTER COLUMN "gender" SET NOT NULL;']
+['ALTER TABLE "schema_evolution_person" ADD COLUMN "gender" varchar(1);', 'ALTER TABLE "schema_evolution_person" ALTER COLUMN "gender" SET NOT NULL;']
# reset the db
>>> cursor.execute('DROP TABLE schema_evolution_person;'); cursor.execute(create_table_sql[0])
# add a column, so it looks like we've recently deleted a field
->>> cursor.execute( backend.get_add_column_sql( 'schema_evolution_person', 'gender_nothere', 'varchar(1)', True, False, False ) )
+>>> for sql in backend.get_add_column_sql( 'schema_evolution_person', 'gender_nothere', 'varchar(1)', True, False, False ): cursor.execute(sql)
>>> management.get_sql_evolution(app)
-['-- warning: as the following may cause data loss, it/they must be run manually', u'-- ALTER TABLE "schema_evolution_person" DROP COLUMN "gender_nothere";', '-- end warning']
+['-- warning: the following may cause data loss', u'ALTER TABLE "schema_evolution_person" DROP COLUMN "gender_nothere";', '-- end warning']
# reset the db
>>> cursor.execute('DROP TABLE schema_evolution_person;'); cursor.execute(create_table_sql[0])
# rename column, so it looks like we've recently renamed a field
->>> cursor.execute( backend.get_change_column_name_sql( 'schema_evolution_person', {}, 'gender2', 'gender_old', 'varchar(1)' ) )
+>>> for sql in backend.get_change_column_name_sql( 'schema_evolution_person', {}, 'gender2', 'gender_old', 'varchar(1)' ): cursor.execute(sql)
>>> management.get_sql_evolution(app)
['ALTER TABLE "schema_evolution_person" RENAME COLUMN "gender_old" TO "gender2";']
# reset the db
>>> cursor.execute('DROP TABLE schema_evolution_person;'); cursor.execute(create_table_sql[0])
# rename table, so it looks like we've recently renamed a model
->>> cursor.execute( backend.get_change_table_name_sql( 'schema_evolution_personold', 'schema_evolution_person' ) )
+>>> for sql in backend.get_change_table_name_sql( 'schema_evolution_personold', 'schema_evolution_person' ): cursor.execute(sql)
>>> management.get_sql_evolution(app)
['ALTER TABLE "schema_evolution_personold" RENAME TO "schema_evolution_person";']
# reset the db
>>> cursor.execute(create_table_sql[0])
# change column flags, so it looks like we've recently changed a column flag
->>> cursor.execute( backend.get_change_column_def_sql( 'schema_evolution_person', 'name', 'varchar(10)', True, False, False ) )
+>>> for sql in backend.get_change_column_def_sql( 'schema_evolution_person', 'name', 'varchar(10)', True, False, False ): cursor.execute(sql)
>>> management.get_sql_evolution(app)
-['ALTER TABLE "schema_evolution_person" ADD COLUMN "name_tmp" varchar(20);\\nUPDATE "schema_evolution_person" SET "name_tmp" = "name";\\nALTER TABLE "schema_evolution_person" DROP COLUMN "name";\\nALTER TABLE "schema_evolution_person" RENAME COLUMN "name_tmp" TO "name";\\nALTER TABLE "schema_evolution_person" ALTER COLUMN "name" SET NOT NULL;']
+['ALTER TABLE "schema_evolution_person" ADD COLUMN "name_tmp" varchar(20);', 'UPDATE "schema_evolution_person" SET "name_tmp" = "name";', 'ALTER TABLE "schema_evolution_person" DROP COLUMN "name";', 'ALTER TABLE "schema_evolution_person" RENAME COLUMN "name_tmp" TO "name";', 'ALTER TABLE "schema_evolution_person" ALTER COLUMN "name" SET NOT NULL;']
"""
@@ -171,39 +171,77 @@ class Meta:
<class 'django.db.backends.sqlite3.base.SQLiteCursorWrapper'>
>>> cursor.execute( 'CREATE TABLE "schema_evolution_person" ( "id" integer NOT NULL UNIQUE PRIMARY KEY, "name" varchar(20) NOT NULL, "gender" varchar(1) NOT NULL, "gender2" varchar(1) NOT NULL, "gender_new" varchar(1) NOT NULL );' ).__class__
<class 'django.db.backends.sqlite3.base.SQLiteCursorWrapper'>
->>> management.get_sql_evolution(app)
+>>> cursor.execute( 'insert into "schema_evolution_person" values (1,2,3,4,5);' ).__class__
+<class 'django.db.backends.sqlite3.base.SQLiteCursorWrapper'>
+>>> sql = management.get_sql_evolution(app)
+>>> print sql
['-- warning: the following may cause data loss', u'-- FYI: sqlite does not support deleting columns, so we create a new "gender_new" and delete the old (ie, this could take a while)', 'ALTER TABLE "schema_evolution_person" RENAME TO "schema_evolution_person_1337_TMP";', 'CREATE TABLE "schema_evolution_person" (\\n "id" integer NOT NULL UNIQUE PRIMARY KEY,\\n "name" varchar(20) NOT NULL,\\n "gender" varchar(1) NOT NULL,\\n "gender2" varchar(1) NOT NULL\\n)\\n;', 'INSERT INTO "schema_evolution_person" SELECT "id","name","gender","gender2" FROM "schema_evolution_person_1337_TMP";', 'DROP TABLE "schema_evolution_person_1337_TMP";', '-- end warning']
+>>> for s in sql: cursor.execute(s).__class__
+<class 'django.db.backends.sqlite3.base.SQLiteCursorWrapper'>
+<class 'django.db.backends.sqlite3.base.SQLiteCursorWrapper'>
+<class 'django.db.backends.sqlite3.base.SQLiteCursorWrapper'>
+<class 'django.db.backends.sqlite3.base.SQLiteCursorWrapper'>
+<class 'django.db.backends.sqlite3.base.SQLiteCursorWrapper'>
+<class 'django.db.backends.sqlite3.base.SQLiteCursorWrapper'>
+<class 'django.db.backends.sqlite3.base.SQLiteCursorWrapper'>
+>>> cursor.execute('select * from "schema_evolution_person";').__class__
+<class 'django.db.backends.sqlite3.base.SQLiteCursorWrapper'>
+>>> cursor.fetchall()[0]
+(1, u'2', u'3', u'4')
# reset the db
>>> cursor.execute('DROP TABLE schema_evolution_person;').__class__
<class 'django.db.backends.sqlite3.base.SQLiteCursorWrapper'>
>>> cursor.execute(create_table_sql[0]).__class__
<class 'django.db.backends.sqlite3.base.SQLiteCursorWrapper'>
-"""
-
-crap = """
-
# rename column, so it looks like we've recently renamed a field
->>> cursor.execute( backend.get_change_column_name_sql( 'schema_evolution_person', {}, 'gender2', 'gender_old', 'varchar(1)' ) )
->>> management.get_sql_evolution(app)
-['ALTER TABLE "schema_evolution_person" RENAME COLUMN "gender_old" TO "gender2";']
+>>> cursor.execute('DROP TABLE "schema_evolution_person"').__class__
+<class 'django.db.backends.sqlite3.base.SQLiteCursorWrapper'>
+>>> cursor.execute('').__class__
+<class 'django.db.backends.sqlite3.base.SQLiteCursorWrapper'>
+>>> cursor.execute('CREATE TABLE "schema_evolution_person" ("id" integer NOT NULL UNIQUE PRIMARY KEY, "name" varchar(20) NOT NULL, "gender" varchar(1) NOT NULL, "gender_old" varchar(1) NOT NULL );').__class__
+<class 'django.db.backends.sqlite3.base.SQLiteCursorWrapper'>
+>>> cursor.execute( 'insert into "schema_evolution_person" values (1,2,3,4);' ).__class__
+<class 'django.db.backends.sqlite3.base.SQLiteCursorWrapper'>
+>>> sql = management.get_sql_evolution(app)
+>>> print sql
+['-- FYI: sqlite does not support renaming columns, so we create a new "schema_evolution_person" and delete the old (ie, this could take a while)', 'ALTER TABLE "schema_evolution_person" RENAME TO "schema_evolution_person_1337_TMP";', 'CREATE TABLE "schema_evolution_person" (\\n "id" integer NOT NULL UNIQUE PRIMARY KEY,\\n "name" varchar(20) NOT NULL,\\n "gender" varchar(1) NOT NULL,\\n "gender2" varchar(1) NOT NULL\\n)\\n;', 'INSERT INTO "schema_evolution_person" SELECT "id","name","gender","gender_old" FROM "schema_evolution_person_1337_TMP";', 'DROP TABLE "schema_evolution_person_1337_TMP";']
+>>> for s in sql: cursor.execute(s).__class__
+<class 'django.db.backends.sqlite3.base.SQLiteCursorWrapper'>
+<class 'django.db.backends.sqlite3.base.SQLiteCursorWrapper'>
+<class 'django.db.backends.sqlite3.base.SQLiteCursorWrapper'>
+<class 'django.db.backends.sqlite3.base.SQLiteCursorWrapper'>
+<class 'django.db.backends.sqlite3.base.SQLiteCursorWrapper'>
+>>> cursor.execute('select * from "schema_evolution_person";').__class__
+<class 'django.db.backends.sqlite3.base.SQLiteCursorWrapper'>
+>>> cursor.fetchall()[0]
+(1, u'2', u'3', u'4')
# reset the db
->>> cursor.execute('DROP TABLE schema_evolution_person;'); cursor.execute(create_table_sql[0])
+>>> cursor.execute('DROP TABLE schema_evolution_person;').__class__
+<class 'django.db.backends.sqlite3.base.SQLiteCursorWrapper'>
+>>> cursor.execute(create_table_sql[0]).__class__
+<class 'django.db.backends.sqlite3.base.SQLiteCursorWrapper'>
# rename table, so it looks like we've recently renamed a model
->>> cursor.execute( backend.get_change_table_name_sql( 'schema_evolution_personold', 'schema_evolution_person' ) )
+>>> for sql in backend.get_change_table_name_sql( 'schema_evolution_personold', 'schema_evolution_person' ): cursor.execute(sql).__class__
+<class 'django.db.backends.sqlite3.base.SQLiteCursorWrapper'>
>>> management.get_sql_evolution(app)
['ALTER TABLE "schema_evolution_personold" RENAME TO "schema_evolution_person";']
# reset the db
->>> cursor.execute(create_table_sql[0])
+>>> cursor.execute('DROP TABLE schema_evolution_personold;').__class__
+<class 'django.db.backends.sqlite3.base.SQLiteCursorWrapper'>
+>>> cursor.execute(create_table_sql[0]).__class__
+<class 'django.db.backends.sqlite3.base.SQLiteCursorWrapper'>
# change column flags, so it looks like we've recently changed a column flag
->>> cursor.execute( backend.get_change_column_def_sql( 'schema_evolution_person', 'name', 'varchar(10)', True, False, False ) )
+>>> cursor.execute('DROP TABLE "schema_evolution_person";').__class__
+<class 'django.db.backends.sqlite3.base.SQLiteCursorWrapper'>
+>>> cursor.execute('CREATE TABLE "schema_evolution_person" ( "id" integer NOT NULL UNIQUE PRIMARY KEY, "name" varchar(20) NOT NULL, "gender" varchar(1) NOT NULL, "gender2" varchar(1) NULL);').__class__
+<class 'django.db.backends.sqlite3.base.SQLiteCursorWrapper'>
>>> management.get_sql_evolution(app)
-['ALTER TABLE "schema_evolution_person" ADD COLUMN "name_tmp" varchar(20);\\nUPDATE "schema_evolution_person" SET "name_tmp" = "name";\\nALTER TABLE "schema_evolution_person" DROP COLUMN "name";\\nALTER TABLE "schema_evolution_person" RENAME COLUMN "name_tmp" TO "name";\\nALTER TABLE "schema_evolution_person" ALTER COLUMN "name" SET NOT NULL;']
-
+['-- FYI: sqlite does not support changing columns, so we create a new "schema_evolution_person" and delete the old (ie, this could take a while)', 'ALTER TABLE "schema_evolution_person" RENAME TO "schema_evolution_person_1337_TMP";', 'CREATE TABLE "schema_evolution_person" (\\n "id" integer NOT NULL UNIQUE PRIMARY KEY,\\n "name" varchar(20) NOT NULL,\\n "gender" varchar(1) NOT NULL,\\n "gender2" varchar(1) NOT NULL\\n)\\n;', 'INSERT INTO "schema_evolution_person" SELECT "id","name","gender","gender2" FROM "schema_evolution_person_1337_TMP";', 'DROP TABLE "schema_evolution_person_1337_TMP";']
"""

0 comments on commit fd77e42

Please sign in to comment.