From 7ac11a1156492d000d674e4c6596a5b0656ccbac Mon Sep 17 00:00:00 2001 From: Zurdi Date: Wed, 10 Jul 2024 22:21:07 +0200 Subject: [PATCH 1/3] avoid orphan collections table on migration fail --- backend/alembic/versions/0022_collections_.py | 66 ++++++++++--------- 1 file changed, 34 insertions(+), 32 deletions(-) diff --git a/backend/alembic/versions/0022_collections_.py b/backend/alembic/versions/0022_collections_.py index 7c11b82af..d4ed0fde1 100644 --- a/backend/alembic/versions/0022_collections_.py +++ b/backend/alembic/versions/0022_collections_.py @@ -24,39 +24,8 @@ def upgrade() -> None: # ### commands auto generated by Alembic - please adjust! ### - op.create_table( - "collections", - sa.Column("id", sa.Integer(), autoincrement=True, nullable=False), - sa.Column("name", sa.String(length=400), nullable=False), - sa.Column("description", sa.Text(), nullable=True), - sa.Column("path_cover_l", sa.String(length=1000), nullable=True), - sa.Column("path_cover_s", sa.String(length=1000), nullable=True), - sa.Column("url_cover", sa.Text(), nullable=True), - sa.Column("roms", sa.JSON(), nullable=False), - sa.Column("user_id", sa.Integer(), nullable=False), - sa.Column("is_public", sa.Boolean(), nullable=False), - sa.Column( - "created_at", - sa.DateTime(timezone=True), - server_default=sa.text("now()"), - nullable=False, - ), - sa.Column( - "updated_at", - sa.DateTime(timezone=True), - server_default=sa.text("now()"), - nullable=False, - ), - sa.ForeignKeyConstraint(["user_id"], ["users.id"], ondelete="CASCADE"), - sa.PrimaryKeyConstraint("id"), - ) - with op.batch_alter_table("rom_user", schema=None) as batch_op: - batch_op.alter_column( - "is_main_sibling", - existing_type=mysql.TINYINT(display_width=1), - nullable=True, - ) + # Doing first the resources migration to avoid orphan collections table if migration fails connection = op.get_bind() roms = connection.execute( sa.text( @@ -109,6 +78,39 @@ def upgrade() -> None: "id": rom.id, }, ) + + op.create_table( + "collections", + sa.Column("id", sa.Integer(), autoincrement=True, nullable=False), + sa.Column("name", sa.String(length=400), nullable=False), + sa.Column("description", sa.Text(), nullable=True), + sa.Column("path_cover_l", sa.String(length=1000), nullable=True), + sa.Column("path_cover_s", sa.String(length=1000), nullable=True), + sa.Column("url_cover", sa.Text(), nullable=True), + sa.Column("roms", sa.JSON(), nullable=False), + sa.Column("user_id", sa.Integer(), nullable=False), + sa.Column("is_public", sa.Boolean(), nullable=False), + sa.Column( + "created_at", + sa.DateTime(timezone=True), + server_default=sa.text("now()"), + nullable=False, + ), + sa.Column( + "updated_at", + sa.DateTime(timezone=True), + server_default=sa.text("now()"), + nullable=False, + ), + sa.ForeignKeyConstraint(["user_id"], ["users.id"], ondelete="CASCADE"), + sa.PrimaryKeyConstraint("id"), + ) + with op.batch_alter_table("rom_user", schema=None) as batch_op: + batch_op.alter_column( + "is_main_sibling", + existing_type=mysql.TINYINT(display_width=1), + nullable=True, + ) # ### end Alembic commands ### From a2c55de6024dc82863178beb6ba13f4dfc33d559 Mon Sep 17 00:00:00 2001 From: Zurdi Date: Wed, 10 Jul 2024 23:30:10 +0200 Subject: [PATCH 2/3] create collections table if not exists --- backend/alembic/versions/0022_collections_.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/backend/alembic/versions/0022_collections_.py b/backend/alembic/versions/0022_collections_.py index d4ed0fde1..4fdcab691 100644 --- a/backend/alembic/versions/0022_collections_.py +++ b/backend/alembic/versions/0022_collections_.py @@ -13,6 +13,7 @@ import sqlalchemy as sa from alembic import op from config import RESOURCES_BASE_PATH +from sqlalchemy import MetaData, Table, inspect from sqlalchemy.dialects import mysql # revision identifiers, used by Alembic. @@ -79,8 +80,11 @@ def upgrade() -> None: }, ) - op.create_table( + metadata = MetaData() + + collections_table = Table( "collections", + metadata, sa.Column("id", sa.Integer(), autoincrement=True, nullable=False), sa.Column("name", sa.String(length=400), nullable=False), sa.Column("description", sa.Text(), nullable=True), @@ -105,6 +109,12 @@ def upgrade() -> None: sa.ForeignKeyConstraint(["user_id"], ["users.id"], ondelete="CASCADE"), sa.PrimaryKeyConstraint("id"), ) + + # Create tabe if not exists + inspector = inspect(connection) + if not inspector.has_table("collections"): + collections_table.create(connection) + with op.batch_alter_table("rom_user", schema=None) as batch_op: batch_op.alter_column( "is_main_sibling", From da6adfe719aa4b9f43dd3b24b2d3e94bc5bfe9c9 Mon Sep 17 00:00:00 2001 From: Zurdi Date: Wed, 10 Jul 2024 23:47:44 +0200 Subject: [PATCH 3/3] fixed if not exists collections logic --- backend/alembic/versions/0022_collections_.py | 60 +++++++++---------- 1 file changed, 27 insertions(+), 33 deletions(-) diff --git a/backend/alembic/versions/0022_collections_.py b/backend/alembic/versions/0022_collections_.py index 4fdcab691..d64bcdfb7 100644 --- a/backend/alembic/versions/0022_collections_.py +++ b/backend/alembic/versions/0022_collections_.py @@ -13,7 +13,7 @@ import sqlalchemy as sa from alembic import op from config import RESOURCES_BASE_PATH -from sqlalchemy import MetaData, Table, inspect +from sqlalchemy import inspect from sqlalchemy.dialects import mysql # revision identifiers, used by Alembic. @@ -80,40 +80,34 @@ def upgrade() -> None: }, ) - metadata = MetaData() - - collections_table = Table( - "collections", - metadata, - sa.Column("id", sa.Integer(), autoincrement=True, nullable=False), - sa.Column("name", sa.String(length=400), nullable=False), - sa.Column("description", sa.Text(), nullable=True), - sa.Column("path_cover_l", sa.String(length=1000), nullable=True), - sa.Column("path_cover_s", sa.String(length=1000), nullable=True), - sa.Column("url_cover", sa.Text(), nullable=True), - sa.Column("roms", sa.JSON(), nullable=False), - sa.Column("user_id", sa.Integer(), nullable=False), - sa.Column("is_public", sa.Boolean(), nullable=False), - sa.Column( - "created_at", - sa.DateTime(timezone=True), - server_default=sa.text("now()"), - nullable=False, - ), - sa.Column( - "updated_at", - sa.DateTime(timezone=True), - server_default=sa.text("now()"), - nullable=False, - ), - sa.ForeignKeyConstraint(["user_id"], ["users.id"], ondelete="CASCADE"), - sa.PrimaryKeyConstraint("id"), - ) - - # Create tabe if not exists inspector = inspect(connection) if not inspector.has_table("collections"): - collections_table.create(connection) + op.create_table( + "collections", + sa.Column("id", sa.Integer(), autoincrement=True, nullable=False), + sa.Column("name", sa.String(length=400), nullable=False), + sa.Column("description", sa.Text(), nullable=True), + sa.Column("path_cover_l", sa.String(length=1000), nullable=True), + sa.Column("path_cover_s", sa.String(length=1000), nullable=True), + sa.Column("url_cover", sa.Text(), nullable=True), + sa.Column("roms", sa.JSON(), nullable=False), + sa.Column("user_id", sa.Integer(), nullable=False), + sa.Column("is_public", sa.Boolean(), nullable=False), + sa.Column( + "created_at", + sa.DateTime(timezone=True), + server_default=sa.text("now()"), + nullable=False, + ), + sa.Column( + "updated_at", + sa.DateTime(timezone=True), + server_default=sa.text("now()"), + nullable=False, + ), + sa.ForeignKeyConstraint(["user_id"], ["users.id"], ondelete="CASCADE"), + sa.PrimaryKeyConstraint("id"), + ) with op.batch_alter_table("rom_user", schema=None) as batch_op: batch_op.alter_column(