From 55d8fd10a9b39e08a24bc7cc99d04b84d6c2d6ad Mon Sep 17 00:00:00 2001 From: Fridolin Glatter Date: Tue, 17 Dec 2024 14:20:53 +0100 Subject: [PATCH] Add DB migration file for Table --- ...66_normalize_optimization_table_storage.py | 151 ++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 ixmp4/db/migrations/versions/bd37acfe8566_normalize_optimization_table_storage.py diff --git a/ixmp4/db/migrations/versions/bd37acfe8566_normalize_optimization_table_storage.py b/ixmp4/db/migrations/versions/bd37acfe8566_normalize_optimization_table_storage.py new file mode 100644 index 00000000..4af4b9de --- /dev/null +++ b/ixmp4/db/migrations/versions/bd37acfe8566_normalize_optimization_table_storage.py @@ -0,0 +1,151 @@ +# type: ignore +"""Normalize optimization.table storage + +Revision ID: bd37acfe8566 +Revises: d66a8276ba0a +Create Date: 2024-12-17 14:19:27.618301 + +""" + +import sqlalchemy as sa +from alembic import op +from sqlalchemy.dialects import sqlite + +# Revision identifiers, used by Alembic. +revision = "bd37acfe8566" +down_revision = "d66a8276ba0a" +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table( + "optimization_tabledata", + sa.Column("table__id", sa.Integer(), nullable=False), + sa.Column("value_0", sa.String(), nullable=False), + sa.Column("value_1", sa.String(), nullable=True), + sa.Column("value_2", sa.String(), nullable=True), + sa.Column("value_3", sa.String(), nullable=True), + sa.Column("value_4", sa.String(), nullable=True), + sa.Column("value_5", sa.String(), nullable=True), + sa.Column("value_6", sa.String(), nullable=True), + sa.Column("value_7", sa.String(), nullable=True), + sa.Column("value_8", sa.String(), nullable=True), + sa.Column("value_9", sa.String(), nullable=True), + sa.Column("value_10", sa.String(), nullable=True), + sa.Column("value_11", sa.String(), nullable=True), + sa.Column("value_12", sa.String(), nullable=True), + sa.Column("value_13", sa.String(), nullable=True), + sa.Column("value_14", sa.String(), nullable=True), + sa.Column( + "id", + sa.Integer(), + sa.Identity(always=False, on_null=True, start=1, increment=1), + nullable=False, + ), + sa.ForeignKeyConstraint( + ["table__id"], + ["optimization_table.id"], + name=op.f("fk_optimization_tabledata_table__id_optimization_table"), + ), + sa.PrimaryKeyConstraint("id", name=op.f("pk_optimization_tabledata")), + sa.UniqueConstraint( + "table__id", + "value_0", + "value_1", + "value_2", + "value_3", + "value_4", + "value_5", + "value_6", + "value_7", + "value_8", + "value_9", + "value_10", + "value_11", + "value_12", + "value_13", + "value_14", + name=op.f( + "uq_optimization_tabledata_table__id_value_0_value_1_value_2_value_3_value_4_value_5_value_6_value_7_value_8_value_9_value_10_value_11_value_12_value_13_value_14" + ), + ), + ) + with op.batch_alter_table("optimization_tabledata", schema=None) as batch_op: + batch_op.create_index( + batch_op.f("ix_optimization_tabledata_table__id"), + ["table__id"], + unique=False, + ) + + op.create_table( + "optimization_tableindexsetassociation", + sa.Column("table_id", sa.Integer(), nullable=False), + sa.Column("indexset_id", sa.Integer(), nullable=False), + sa.Column("column_name", sa.String(length=255), nullable=True), + sa.Column( + "id", + sa.Integer(), + sa.Identity(always=False, on_null=True, start=1, increment=1), + nullable=False, + ), + sa.ForeignKeyConstraint( + ["indexset_id"], + ["optimization_indexset.id"], + name=op.f( + "fk_optimization_tableindexsetassociation_indexset_id_optimization_indexset" + ), + ), + sa.ForeignKeyConstraint( + ["table_id"], + ["optimization_table.id"], + name=op.f( + "fk_optimization_tableindexsetassociation_table_id_optimization_table" + ), + ), + sa.PrimaryKeyConstraint( + "id", name=op.f("pk_optimization_tableindexsetassociation") + ), + ) + with op.batch_alter_table( + "optimization_tableindexsetassociation", schema=None + ) as batch_op: + batch_op.create_index( + batch_op.f("ix_optimization_tableindexsetassociation_indexset_id"), + ["indexset_id"], + unique=False, + ) + batch_op.create_index( + batch_op.f("ix_optimization_tableindexsetassociation_table_id"), + ["table_id"], + unique=False, + ) + + with op.batch_alter_table("optimization_table", schema=None) as batch_op: + batch_op.drop_column("data") + + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table("optimization_table", schema=None) as batch_op: + batch_op.add_column(sa.Column("data", sqlite.JSON(), nullable=False)) + + with op.batch_alter_table( + "optimization_tableindexsetassociation", schema=None + ) as batch_op: + batch_op.drop_index( + batch_op.f("ix_optimization_tableindexsetassociation_table_id") + ) + batch_op.drop_index( + batch_op.f("ix_optimization_tableindexsetassociation_indexset_id") + ) + + op.drop_table("optimization_tableindexsetassociation") + with op.batch_alter_table("optimization_tabledata", schema=None) as batch_op: + batch_op.drop_index(batch_op.f("ix_optimization_tabledata_table__id")) + + op.drop_table("optimization_tabledata") + # ### end Alembic commands ###