Skip to content

Commit f9fd8cf

Browse files
committed
Schema migrations: CrateDB workaround for add_column_with_default_value
The `ALTER TABLE ... ADD COLUMN ... DEFAULT` statement of CrateaDB does not respect the right semantics of the DEFAULT clause yet.
1 parent f9efad4 commit f9fd8cf

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/cratedb_fivetran_destination/schema_migration_helper.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,20 @@ def handle_add(self, add_op, schema, table, table_obj: common_pb2.Table):
177177
new_col = table_obj.columns.add()
178178
new_col.name = add_col_default_with_value.column
179179
new_col.type = add_col_default_with_value.column_type
180+
default_value = add_col_default_with_value.default_value
180181
type_ = TypeMap.to_cratedb(new_col.type, new_col.params)
181-
sql = f'ALTER TABLE "{schema}"."{table}" ADD COLUMN "{new_col.name}" {type_};'
182+
# FIXME: Improve after CrateDB does it right.
183+
# - https://github.com/crate/cratedb-fivetran-destination/issues/111
184+
# - https://github.com/crate/crate/issues/18783
185+
# sql = f'ALTER TABLE "{schema}"."{table}" ADD COLUMN "{new_col.name}" {type_} DEFAULT \'{default_value}\';' # noqa: ERA001
186+
sql_bag = SqlBag()
187+
sql_bag.add(f'ALTER TABLE "{schema}"."{table}" ADD COLUMN "{new_col.name}" {type_};')
188+
if default_value is not None:
189+
sql_bag.add(
190+
f'UPDATE "{schema}"."{table}" SET "{new_col.name}" = \'{default_value}\';'
191+
)
182192
with self.engine.connect() as conn:
183-
conn.execute(sa.text(sql))
193+
sql_bag.execute(conn)
184194

185195
log_message(
186196
LOG_INFO,

0 commit comments

Comments
 (0)