diff --git a/areas/apps/models.py b/areas/apps/models.py index aa6b20e..85dc4ae 100644 --- a/areas/apps/models.py +++ b/areas/apps/models.py @@ -23,7 +23,7 @@ class AppRole(db.Model): user_id = db.Column(String(length=64), primary_key=True) app_id = db.Column(Integer, ForeignKey("app.id"), primary_key=True) - role = db.Column(String(length=64)) + role_id = db.Column(Integer, ForeignKey("role.id")) def __repr__(self): return f"{self.role} for {self.user_id} on {self.app_id}" diff --git a/areas/users/__init__.py b/areas/users/__init__.py index 642b070..a19fa8f 100644 --- a/areas/users/__init__.py +++ b/areas/users/__init__.py @@ -1 +1,2 @@ -from .users import * \ No newline at end of file +from .users import * +from .models import * diff --git a/areas/users/models.py b/areas/users/models.py new file mode 100644 index 0000000..8f7c53a --- /dev/null +++ b/areas/users/models.py @@ -0,0 +1,10 @@ +from sqlalchemy import Integer, String +from database import db + + +class Role(db.Model): + id = db.Column(Integer, primary_key=True) + name = db.Column(String(length=64)) + + def __repr__(self): + return f"Role {self.name}" diff --git a/migrations/versions/5f462d2d9d25_convert_role_column_to_table.py b/migrations/versions/5f462d2d9d25_convert_role_column_to_table.py new file mode 100644 index 0000000..c315ddd --- /dev/null +++ b/migrations/versions/5f462d2d9d25_convert_role_column_to_table.py @@ -0,0 +1,41 @@ +"""convert role column to table + +Revision ID: 5f462d2d9d25 +Revises: 27761560bbcb +Create Date: 2022-04-13 15:00:27.182898 + +""" +from alembic import op +import sqlalchemy as sa +from sqlalchemy.dialects import mysql + +# revision identifiers, used by Alembic. +revision = "5f462d2d9d25" +down_revision = "27761560bbcb" +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table( + "role", + sa.Column("id", sa.Integer(), nullable=False), + sa.Column("name", sa.String(length=64), nullable=True), + sa.PrimaryKeyConstraint("id"), + ) + op.add_column("app_role", sa.Column("role_id", sa.Integer(), nullable=True)) + op.create_foreign_key(None, "app_role", "role", ["role_id"], ["id"]) + op.drop_column("app_role", "role") + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column( + "app_role", sa.Column("role", mysql.VARCHAR(length=64), nullable=True) + ) + op.drop_constraint(None, "app_role", type_="foreignkey") + op.drop_column("app_role", "role_id") + op.drop_table("role") + # ### end Alembic commands ###