From e14de715f7ac2709f7fb6f34ad5c88a2687fa49b Mon Sep 17 00:00:00 2001 From: meanmail Date: Wed, 3 Mar 2021 21:04:54 +0700 Subject: [PATCH 1/2] Support for adding a column to the beginning of a table --- src/infi/clickhouse_orm/migrations.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/infi/clickhouse_orm/migrations.py b/src/infi/clickhouse_orm/migrations.py index c8c656a..6fea1c0 100644 --- a/src/infi/clickhouse_orm/migrations.py +++ b/src/infi/clickhouse_orm/migrations.py @@ -83,10 +83,12 @@ class AlterTable(ModelOperation): 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' % (name, field.get_sql(db=database)) if is_regular_field: - cmd += ' AFTER %s' % prev_name + if prev_name is not None: + cmd += ' AFTER %s' % prev_name + else: + cmd += ' FIRST' self._alter_table(database, cmd) if is_regular_field: From 6a5889280d7072ff7e57ad8bbc8f33baeac0568c Mon Sep 17 00:00:00 2001 From: meanmail Date: Thu, 4 Mar 2021 11:28:27 +0700 Subject: [PATCH 2/2] Simplified --- src/infi/clickhouse_orm/migrations.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/infi/clickhouse_orm/migrations.py b/src/infi/clickhouse_orm/migrations.py index 6fea1c0..af7dc51 100644 --- a/src/infi/clickhouse_orm/migrations.py +++ b/src/infi/clickhouse_orm/migrations.py @@ -85,7 +85,7 @@ class AlterTable(ModelOperation): logger.info(' Add column %s', name) cmd = 'ADD COLUMN %s %s' % (name, field.get_sql(db=database)) if is_regular_field: - if prev_name is not None: + if prev_name: cmd += ' AFTER %s' % prev_name else: cmd += ' FIRST'