Migrations - cannot add a new materialized field after a regular field

This commit is contained in:
Itai Shirav 2019-01-28 10:08:07 +02:00
parent 7aef68ab14
commit efe270b502
3 changed files with 10 additions and 4 deletions

View File

@ -6,6 +6,7 @@ Unreleased
- Added `timeout` parameter to database initializer (SUHAR1K)
- Added `verify_ssl_cert` parameter to database initializer
- Added `final()` method to querysets (M1hacka)
- Fixed a migrations problem - cannot add a new materialized field after a regular field
v1.0.3
------

View File

@ -75,13 +75,16 @@ class AlterTable(Operation):
# Identify fields that were added to the model
prev_name = None
for name, field in iteritems(self.model_class.fields()):
is_regular_field = not (field.materialized or field.alias)
if name not in table_fields:
logger.info(' Add column %s', name)
assert prev_name, 'Cannot add a column to the beginning of the table'
cmd = 'ADD COLUMN %s %s AFTER %s' % (name, field.get_sql(), prev_name)
cmd = 'ADD COLUMN %s %s' % (name, field.get_sql())
if is_regular_field:
cmd += ' AFTER %s' % prev_name
self._alter_table(database, cmd)
if not field.materialized and not field.alias:
if is_regular_field:
# ALIAS and MATERIALIZED fields are not stored in the database, and raise DatabaseError
# (no AFTER column). So we will skip them
prev_name = name

View File

@ -93,10 +93,10 @@ class MigrationsTestCase(unittest.TestCase):
self.database.migrate('tests.sample_migrations', 14)
self.assertTrue(self.tableExists(MaterializedModel1))
self.assertEqual(self.getTableFields(MaterializedModel1),
[('date_time', "DateTime"), ('int_field', 'Int8'), ('date', 'Date')])
[('date_time', 'DateTime'), ('int_field', 'Int8'), ('date', 'Date'), ('int_field_plus_one', 'Int8')])
self.assertTrue(self.tableExists(AliasModel1))
self.assertEqual(self.getTableFields(AliasModel1),
[('date', 'Date'), ('int_field', 'Int8'), ('date_alias', "Date")])
[('date', 'Date'), ('int_field', 'Int8'), ('date_alias', 'Date'), ('int_field_plus_one', 'Int8')])
# Several different models with the same table name, to simulate a table that changes over time
@ -183,6 +183,7 @@ class MaterializedModel1(Model):
date_time = DateTimeField()
date = DateField(materialized='toDate(date_time)')
int_field = Int8Field()
int_field_plus_one = Int8Field(materialized='int_field + 1')
engine = MergeTree('date', ('date',))
@ -206,6 +207,7 @@ class AliasModel1(Model):
date = DateField()
date_alias = DateField(alias='date')
int_field = Int8Field()
int_field_plus_one = Int8Field(alias='int_field + 1')
engine = MergeTree('date', ('date',))