From 1a2586fea26436d82257154485b65c356946cb8f Mon Sep 17 00:00:00 2001 From: Puyodead1 Date: Tue, 3 Dec 2024 17:55:29 -0500 Subject: [PATCH] I hate migrations --- alembic/versions/51d65f145c1f_create_prd.py | 20 +-- alembic/versions/592b912adf54_init.py | 156 +++++++++++++------- 2 files changed, 108 insertions(+), 68 deletions(-) diff --git a/alembic/versions/51d65f145c1f_create_prd.py b/alembic/versions/51d65f145c1f_create_prd.py index 43c6764..0e0cef3 100644 --- a/alembic/versions/51d65f145c1f_create_prd.py +++ b/alembic/versions/51d65f145c1f_create_prd.py @@ -26,30 +26,20 @@ def upgrade() -> None: sa.Column("id", sa.Integer(), autoincrement=True, nullable=False), sa.Column("hash", sa.String(length=255), nullable=False), sa.Column("prd", sa.Text(), nullable=False), - sa.Column( - "uploaded_by", - sa.String(length=19, collation="utf8mb4_general_ci"), - nullable=False, - ), - sa.PrimaryKeyConstraint("id"), - sa.UniqueConstraint("hash"), + sa.Column("uploaded_by", sa.String(length=19), nullable=False), sa.ForeignKeyConstraint( ["uploaded_by"], ["users.id"], ), - mysql_engine="InnoDB", - mysql_charset="utf8mb4", - mysql_collate="utf8mb4_general_ci", + sa.PrimaryKeyConstraint("id"), + sa.UniqueConstraint("hash"), ) op.create_table( "user_prd", - sa.Column("user_id", sa.String(length=19, collation="utf8mb4_general_ci"), nullable=False), - sa.Column("device_hash", sa.String(length=255, collation="utf8mb4_general_ci"), nullable=False), + sa.Column("user_id", sa.String(length=19), nullable=False), + sa.Column("device_hash", sa.String(length=255), nullable=False), sa.ForeignKeyConstraint(["device_hash"], ["prds.hash"], ondelete="CASCADE"), sa.ForeignKeyConstraint(["user_id"], ["users.id"], ondelete="CASCADE"), - mysql_engine="InnoDB", - mysql_charset="utf8mb4", - mysql_collate="utf8mb4_general_ci", ) # ### end Alembic commands ### diff --git a/alembic/versions/592b912adf54_init.py b/alembic/versions/592b912adf54_init.py index aff4573..34a42fe 100644 --- a/alembic/versions/592b912adf54_init.py +++ b/alembic/versions/592b912adf54_init.py @@ -23,68 +23,118 @@ def upgrade() -> None: bind = op.get_bind() inspector = sa.Inspector.from_engine(bind) - has_apikeys = "apikeys" in inspector.get_table_names() - has_users = "users" in inspector.get_table_names() - has_cdms = "cdms" in inspector.get_table_names() - has_keys = "keys_" in inspector.get_table_names() + migrate_existing_apikeys = "apikeys" in inspector.get_table_names() + migrate_existing_users = "users" in inspector.get_table_names() + migrate_existing_cdms = "cdms" in inspector.get_table_names() + migrate_existing_keys = "keys_" in inspector.get_table_names() + + if migrate_existing_apikeys: + op.rename_table("apikeys", "apikeys_old") + + if migrate_existing_users: + op.rename_table("users", "users_old") + + if migrate_existing_cdms: + op.rename_table("cdms", "cdms_old") + + if migrate_existing_keys: + op.rename_table("keys_", "keys__old") # ### commands auto generated by Alembic - please adjust! ### - if not has_apikeys: - op.create_table( - "apikeys", - sa.Column("id", sa.Integer(), autoincrement=True, nullable=False), - sa.Column("created_at", sa.DateTime(), nullable=False), - sa.Column("api_key", sa.String(length=255), nullable=False), - sa.Column("user_id", sa.String(length=255), nullable=False), - sa.PrimaryKeyConstraint("id"), - sa.UniqueConstraint("id"), + op.create_table( + "apikeys", + sa.Column("id", sa.Integer(), autoincrement=True, nullable=False), + sa.Column("created_at", sa.DateTime(), nullable=False), + sa.Column("api_key", sa.String(length=255), nullable=False), + sa.Column("user_id", sa.String(length=255), nullable=False), + sa.PrimaryKeyConstraint("id"), + sa.UniqueConstraint("id"), + ) + + op.create_table( + "users", + sa.Column("id", sa.String(length=255), nullable=False), + sa.Column("username", sa.String(length=255), nullable=False), + sa.Column("discriminator", sa.String(length=255), nullable=False), + sa.Column("avatar", sa.String(length=255), nullable=True), + sa.Column("public_flags", sa.Integer(), nullable=False), + sa.Column("api_key", sa.String(length=255), nullable=False), + sa.Column("flags", sa.Integer(), nullable=False), + sa.PrimaryKeyConstraint("id"), + sa.UniqueConstraint("id"), + ) + + op.create_table( + "cdms", + sa.Column("id", sa.Integer(), autoincrement=True, nullable=False), + sa.Column("session_id_type", sa.String(length=255), nullable=False), + sa.Column("security_level", sa.Integer(), nullable=False), + sa.Column("client_id_blob_filename", sa.Text(), nullable=False), + sa.Column("device_private_key", sa.Text(), nullable=False), + sa.Column("code", sa.Text(), nullable=False), + sa.Column("uploaded_by", sa.String(length=255), nullable=True), + sa.ForeignKeyConstraint( + ["uploaded_by"], + ["users.id"], + ), + sa.PrimaryKeyConstraint("id"), + ) + + op.create_table( + "keys_", + sa.Column("kid", sa.String(length=32), nullable=False), + sa.Column("added_at", sa.Integer(), nullable=False), + sa.Column("added_by", sa.String(length=255), nullable=True), + sa.Column("license_url", sa.Text(), nullable=False), + sa.Column("key_", sa.String(length=255), nullable=False), + sa.ForeignKeyConstraint( + ["added_by"], + ["users.id"], + ), + sa.PrimaryKeyConstraint("kid"), + ) + + if ( + not migrate_existing_apikeys + and not migrate_existing_users + and not migrate_existing_cdms + and not migrate_existing_keys + ): + return + + # copy data from old tables to new tables + if migrate_existing_users: + op.execute( + "INSERT INTO users (id, username, discriminator, avatar, public_flags, api_key, flags) SELECT id, username, discriminator, avatar, public_flags, api_key, flags FROM users_old" ) - if not has_users: - op.create_table( - "users", - sa.Column("id", sa.String(length=255), nullable=False), - sa.Column("username", sa.String(length=255), nullable=False), - sa.Column("discriminator", sa.String(length=255), nullable=False), - sa.Column("avatar", sa.String(length=255), nullable=True), - sa.Column("public_flags", sa.Integer(), nullable=False), - sa.Column("api_key", sa.String(length=255), nullable=False), - sa.Column("flags", sa.Integer(), nullable=False), - sa.PrimaryKeyConstraint("id"), - sa.UniqueConstraint("id"), + if migrate_existing_cdms: + op.execute( + "INSERT INTO cdms (id, session_id_type, security_level, client_id_blob_filename, device_private_key, code, uploaded_by) SELECT id, session_id_type, security_level, client_id_blob_filename, device_private_key, code, uploaded_by FROM cdms_old" ) - if not has_cdms: - op.create_table( - "cdms", - sa.Column("id", sa.Integer(), autoincrement=True, nullable=False), - sa.Column("session_id_type", sa.String(length=255), nullable=False), - sa.Column("security_level", sa.Integer(), nullable=False), - sa.Column("client_id_blob_filename", sa.Text(), nullable=False), - sa.Column("device_private_key", sa.Text(), nullable=False), - sa.Column("code", sa.Text(), nullable=False), - sa.Column("uploaded_by", sa.String(length=255), nullable=True), - sa.ForeignKeyConstraint( - ["uploaded_by"], - ["users.id"], - ), - sa.PrimaryKeyConstraint("id"), + if migrate_existing_apikeys: + op.execute( + "INSERT INTO apikeys (id, created_at, api_key, user_id) SELECT id, created_at, api_key, user_id FROM apikeys_old" ) - if not has_keys: - op.create_table( - "keys_", - sa.Column("kid", sa.String(length=32), nullable=False), - sa.Column("added_at", sa.Integer(), nullable=False), - sa.Column("added_by", sa.String(length=255), nullable=True), - sa.Column("license_url", sa.Text(), nullable=False), - sa.Column("key_", sa.String(length=255), nullable=False), - sa.ForeignKeyConstraint( - ["added_by"], - ["users.id"], - ), - sa.PrimaryKeyConstraint("kid"), + if migrate_existing_keys: + op.execute( + "INSERT INTO keys_ (kid, added_at, added_by, license_url, key_) SELECT kid, added_at, added_by, license_url, key_ FROM keys__old" ) + + # drop old tables + if migrate_existing_apikeys: + op.drop_table("apikeys_old") + + if migrate_existing_users: + op.drop_table("users_old") + + if migrate_existing_cdms: + op.drop_table("cdms_old") + + if migrate_existing_keys: + op.drop_table("keys__old") # ### end Alembic commands ###