diff --git a/backend/migrations/versions/7d27395c892a_new_migration.py b/backend/migrations/versions/7d27395c892a_new_migration.py index 7c7dd577ab838ef8a190ca0803c698bfaf73c071..b29cdef8d719f0add29af252e69631f988868cc8 100644 --- a/backend/migrations/versions/7d27395c892a_new_migration.py +++ b/backend/migrations/versions/7d27395c892a_new_migration.py @@ -8,6 +8,7 @@ Create Date: 2023-01-18 14:48:23.996261 from alembic import op import sqlalchemy as sa from sqlalchemy.dialects import mysql +from sqlalchemy.engine.reflection import Inspector # revision identifiers, used by Alembic. revision = '7d27395c892a' @@ -15,54 +16,62 @@ down_revision = None branch_labels = None depends_on = None +conn = op.get_bind() +inspector = Inspector.from_engine(conn) +tables = inspector.get_table_names() + def upgrade(): - op.create_table( - "app", - sa.Column("id", sa.Integer(), nullable=False), - sa.Column("name", sa.String(length=64), nullable=True), - sa.Column("slug", sa.String(length=64), nullable=True), - sa.Column('external', sa.Boolean(), server_default='0', nullable=False), - sa.Column('url', sa.String(length=128), nullable=True), - sa.PrimaryKeyConstraint("id"), - sa.UniqueConstraint("slug"), - ) + if "app" not in tables: + op.create_table( + "app", + sa.Column("id", sa.Integer(), nullable=False), + sa.Column("name", sa.String(length=64), nullable=True), + sa.Column("slug", sa.String(length=64), nullable=True), + sa.Column("external", sa.Boolean(), server_default='0', nullable=False), + sa.Column("url", sa.String(length=128), nullable=True), + sa.PrimaryKeyConstraint("id"), + sa.UniqueConstraint("slug"), + keep_existing= True + ) - op.create_table( - "app_role", - sa.Column("user_id", sa.String(length=64), nullable=False), - sa.Column("app_id", sa.Integer(), nullable=False), - sa.Column("role_id", sa.Integer(), nullable=True), - sa.ForeignKeyConstraint( - ["app_id"], - ["app.id"], - ), - sa.PrimaryKeyConstraint("user_id", "app_id"), - ) + if "app_role" not in tables: + op.create_table( + "app_role", + sa.Column("user_id", sa.String(length=64), nullable=False), + sa.Column("app_id", sa.Integer(), nullable=False), + sa.Column("role_id", sa.Integer(), nullable=True), + sa.ForeignKeyConstraint( + ["app_id"], + ["app.id"], + ), + sa.PrimaryKeyConstraint("user_id", "app_id"), + ) - op.create_table( - "role", - sa.Column("id", sa.Integer(), nullable=False), - sa.Column("name", sa.String(length=64), nullable=True), - sa.PrimaryKeyConstraint("id"), - ) + if "role" not in tables: + op.create_table( + "role", + sa.Column("id", sa.Integer(), nullable=False), + sa.Column("name", sa.String(length=64), nullable=True), + sa.PrimaryKeyConstraint("id"), + ) + op.execute("INSERT INTO `role` (id, `name`) VALUES (1, 'admin')") + op.execute("INSERT INTO `role` (id, `name`) VALUES (2, 'user')") + op.execute("INSERT INTO `role` (id, `name`) VALUES (3, 'no access')") op.create_foreign_key(None, "app_role", "role", ["role_id"], ["id"]) - op.execute("INSERT INTO `role` (id, `name`) VALUES (1, 'admin')") - op.execute("INSERT INTO `role` (id, `name`) VALUES (2, 'user')") - op.execute("INSERT INTO `role` (id, `name`) VALUES (3, 'no access')") - - op.create_table('oauthclient_app', - sa.Column('oauthclient_id', mysql.VARCHAR(length=64), nullable=False), - sa.Column('app_id', mysql.INTEGER(display_width=11), autoincrement=False, nullable=False), - sa.ForeignKeyConstraint(['app_id'], ['app.id'], name='oauthclient_app_fk_app_id'), - sa.PrimaryKeyConstraint('oauthclient_id'), - mysql_default_charset='utf8mb3', - mysql_engine='InnoDB' - ) + if "oauthclient_app" not in tables: + op.create_table('oauthclient_app', + sa.Column('oauthclient_id', mysql.VARCHAR(length=64), nullable=False), + sa.Column('app_id', mysql.INTEGER(display_width=11), autoincrement=False, nullable=False), + sa.ForeignKeyConstraint(['app_id'], ['app.id'], name='oauthclient_app_fk_app_id'), + sa.PrimaryKeyConstraint('oauthclient_id'), + mysql_default_charset='utf8mb3', + mysql_engine='InnoDB' + ) def downgrade(): op.drop_table("role") op.drop_table("app_role") op.drop_table("app") - op.drop_table('oauthclient_app') + op.drop_table("oauthclient_app")