From 603c1aa71e92cc8aa871562089b6ce2e65209d3b Mon Sep 17 00:00:00 2001 From: Davor Date: Wed, 8 Jun 2022 17:59:36 +0200 Subject: [PATCH] Added migration script --- ...462d2d9d25_convert_role_column_to_table.py | 2 -- .../versions/b514cca2d47b_add_user_role.py | 31 +++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 migrations/versions/b514cca2d47b_add_user_role.py diff --git a/migrations/versions/5f462d2d9d25_convert_role_column_to_table.py b/migrations/versions/5f462d2d9d25_convert_role_column_to_table.py index bfc7843..53a8a1d 100644 --- a/migrations/versions/5f462d2d9d25_convert_role_column_to_table.py +++ b/migrations/versions/5f462d2d9d25_convert_role_column_to_table.py @@ -30,10 +30,8 @@ def upgrade(): # Insert default role "admin" as ID 1 op.execute(sa.insert(role_table).values(id=1,name="admin")) - op.execute(sa.insert(role_table).values(id=2,name="user")) # Set role_id 1 to all current "admin" users op.execute("UPDATE app_role SET role_id = 1 WHERE role = 'admin'") - op.execute("UPDATE app_role SET role_id = 2 WHERE role = 'user'") # Drop old column op.drop_column("app_role", "role") diff --git a/migrations/versions/b514cca2d47b_add_user_role.py b/migrations/versions/b514cca2d47b_add_user_role.py new file mode 100644 index 0000000..9209492 --- /dev/null +++ b/migrations/versions/b514cca2d47b_add_user_role.py @@ -0,0 +1,31 @@ +"""add user role + +Revision ID: b514cca2d47b +Revises: 5f462d2d9d25 +Create Date: 2022-06-08 17:24:51.305129 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'b514cca2d47b' +down_revision = '5f462d2d9d25' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### end Alembic commands ### + + # Insert role "user" as ID 2 + op.execute("INSERT INTO `role` (id, `name`) VALUES (2, 'user')") + # Set role_id 2 to all current "user" users which by have NULL role ID + op.execute("UPDATE app_role SET role_id = 2 WHERE role_id IS NULL") + + +def downgrade(): + op.execute("UPDATE app_role SET role_id = NULL WHERE role_id = 2") + op.execute("DELETE FROM `role` WHERE id = 2") + pass