mirror of
https://github.com/rommapp/romm.git
synced 2026-06-29 23:35:47 +00:00
Merge pull request #992 from rommapp/fix/collections-migration
Avoid orphan collections table on migration fail
This commit is contained in:
@@ -13,6 +13,7 @@ import shutil
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
from config import RESOURCES_BASE_PATH
|
||||
from sqlalchemy import inspect
|
||||
from sqlalchemy.dialects import mysql
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
@@ -24,39 +25,8 @@ depends_on = None
|
||||
|
||||
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 +79,42 @@ def upgrade() -> None:
|
||||
"id": rom.id,
|
||||
},
|
||||
)
|
||||
|
||||
inspector = inspect(connection)
|
||||
if not inspector.has_table("collections"):
|
||||
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 ###
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user