From d04464995dba8056fd54b4ddd1ae34dfd0bf0cc2 Mon Sep 17 00:00:00 2001
From: Syrine Neifar <syrine@greenhost.nl>
Date: Mon, 23 Jan 2023 13:54:49 +0100
Subject: [PATCH] Fixes new migration

---
 .../versions/7d27395c892a_new_migration.py    | 33 ++++++++-----------
 1 file changed, 14 insertions(+), 19 deletions(-)

diff --git a/backend/migrations/versions/7d27395c892a_new_migration.py b/backend/migrations/versions/7d27395c892a_new_migration.py
index b29cdef8..a375acc5 100644
--- a/backend/migrations/versions/7d27395c892a_new_migration.py
+++ b/backend/migrations/versions/7d27395c892a_new_migration.py
@@ -31,20 +31,6 @@ def upgrade():
             sa.Column("url", sa.String(length=128), nullable=True),
             sa.PrimaryKeyConstraint("id"),
             sa.UniqueConstraint("slug"),
-            keep_existing= True
-        )
-
-    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"),
         )
 
     if "role" not in tables:
@@ -52,26 +38,35 @@ def upgrade():
             "role",
             sa.Column("id", sa.Integer(), nullable=False),
             sa.Column("name", sa.String(length=64), nullable=True),
-            sa.PrimaryKeyConstraint("id"),
+            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"])
+    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.PrimaryKeyConstraint("user_id", "app_id"),
+            sa.ForeignKeyConstraint(["app_id"],["app.id"]),
+            sa.ForeignKeyConstraint(["role_id"],["role.id"])
+        )
 
     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'),
+            sa.ForeignKeyConstraint(['app_id'], ['app.id'], name='oauthclient_app_fk_app_id'),
             mysql_default_charset='utf8mb3',
             mysql_engine='InnoDB'
         )
 
 def downgrade():
+    op.drop_table("oauthclient_app")
     op.drop_table("role")
     op.drop_table("app_role")
-    op.drop_table("app")
-    op.drop_table("oauthclient_app")
+    op.drop_table("app")
\ No newline at end of file
-- 
GitLab