Skip to content

Commit

Permalink
Fix migrations reusing column objects
Browse files Browse the repository at this point in the history
Closes: #1080

Related to: sqlalchemy/sqlalchemy#5669
  • Loading branch information
Adrian Turjak authored and mergify[bot] committed Nov 4, 2020
1 parent 64ff78f commit 5eeb3f0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,18 @@ def upgrade():

existing_type = sa.types.DECIMAL(
precision=20, scale=6, asdecimal=True)
existing_col = sa.Column(
existing_col = lambda: sa.Column(
column_name,
existing_type,
nullable=nullable)
temp_col = sa.Column(
temp_col = lambda: sa.Column(
column_name + "_ts",
sqlalchemy_types.TimestampUTC(),
nullable=True)
op.add_column(table_name, temp_col)
t = sa.sql.table(table_name, existing_col, temp_col)
op.add_column(table_name, temp_col())
t = sa.sql.table(table_name, existing_col(), temp_col())
op.execute(t.update().values(
**{column_name + "_ts": func.from_unixtime(existing_col)}))
**{column_name + "_ts": func.from_unixtime(existing_col())}))
op.drop_column(table_name, column_name)
op.alter_column(table_name,
column_name + "_ts",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,24 @@

def upgrade():
for table_name in ("resource", "resource_history", "metric"):
creator_col = sa.Column("creator", sa.String(255))
created_by_user_id_col = sa.Column("created_by_user_id",
sa.String(255))
created_by_project_id_col = sa.Column("created_by_project_id",
sa.String(255))
op.add_column(table_name, creator_col)
creator_col = lambda: sa.Column("creator", sa.String(255))
created_by_user_id_col = lambda: sa.Column(
"created_by_user_id", sa.String(255))
created_by_project_id_col = lambda: sa.Column(
"created_by_project_id", sa.String(255))
op.add_column(table_name, creator_col())
t = sa.sql.table(
table_name, creator_col,
created_by_user_id_col, created_by_project_id_col)
table_name,
creator_col(),
created_by_user_id_col(),
created_by_project_id_col(),
)
op.execute(
t.update().values(
creator=(
created_by_user_id_col + ":" + created_by_project_id_col
)).where((created_by_user_id_col is not None)
| (created_by_project_id_col is not None)))
created_by_user_id_col() + ":" +
created_by_project_id_col()
)).where((created_by_user_id_col() is not None)
| (created_by_project_id_col() is not None)))
op.drop_column(table_name, "created_by_user_id")
op.drop_column(table_name, "created_by_project_id")

0 comments on commit 5eeb3f0

Please sign in to comment.