From e2fbf421d71cd947bd20a00407f0f02230531aa8 Mon Sep 17 00:00:00 2001 From: belthlemar Date: Mon, 20 Jan 2025 11:29:31 +0100 Subject: [PATCH] add alembic migration --- ...3f1b6_create_links_ts_generation_tables.py | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 alembic/versions/dadad513f1b6_create_links_ts_generation_tables.py diff --git a/alembic/versions/dadad513f1b6_create_links_ts_generation_tables.py b/alembic/versions/dadad513f1b6_create_links_ts_generation_tables.py new file mode 100644 index 0000000000..2a7f2550ab --- /dev/null +++ b/alembic/versions/dadad513f1b6_create_links_ts_generation_tables.py @@ -0,0 +1,68 @@ +"""create_links_ts_generation_tables + +Revision ID: dadad513f1b6 +Revises: bae9c99bc42d +Create Date: 2025-01-20 10:11:01.293931 + +""" +from alembic import op +import sqlalchemy as sa +from sqlalchemy import table, column +from sqlalchemy.orm import Session + +# revision identifiers, used by Alembic. +revision = 'dadad513f1b6' +down_revision = 'bae9c99bc42d' +branch_labels = None +depends_on = None + + +def upgrade(): + op.create_table( + "study_nb_ts_gen", + sa.Column("id", sa.String(length=36), nullable=False), + sa.Column("links", sa.Integer(), server_default="1", nullable=False), + sa.ForeignKeyConstraint( + ["id"], + ["study.id"], + ondelete="CASCADE" + ), + sa.PrimaryKeyConstraint("id"), + ) + + # Fill the table + bind = op.get_bind() + session = Session(bind=bind) + ts_gen_table = table("study_nb_ts_gen", column("id"), column("links")) + study_table = table('study', column('id')) + study_ids = session.query(study_table).all() + data_to_insert = [{'id': study_id[0], 'links': 1} for study_id in study_ids] + op.bulk_insert(ts_gen_table, data_to_insert) + + op.create_table( + "links_ts_gen_properties", + sa.Column("id", sa.Integer()), + sa.Column("area_from", sa.String(), nullable=False), + sa.Column("area_to", sa.String(), nullable=False), + sa.Column("prepro", sa.String(), nullable=False), + sa.Column("modulation", sa.String(), nullable=False), + sa.Column("unit_count", sa.String(), nullable=False), + sa.Column("nominal_capacity", sa.String(), nullable=False), + sa.Column("law_planned", sa.Enum('uniform', 'geometric', name='lawplanned'), nullable=False), + sa.Column("law_forced", sa.Enum('uniform', 'geometric', name='lawforced'), nullable=False), + sa.Column("volatility_planned", sa.String(), nullable=False), + sa.Column("volatility_forced", sa.String(), nullable=False), + sa.Column("force_no_generation", sa.Boolean(), nullable=False), + sa.Column("study_id", sa.String(length=36), nullable=False), + sa.ForeignKeyConstraint( + ["study_id"], + ["study.id"], + ondelete="CASCADE" + ), + sa.PrimaryKeyConstraint("id") + ) + + +def downgrade(): + op.drop_table("study_nb_ts_gen") + op.drop_table("links_ts_gen_properties")