diff --git a/migrations/public/versions/07c7128c166e_refactor_to_flask_security.py b/migrations/public/versions/07c7128c166e_refactor_to_flask_security.py new file mode 100644 index 0000000..71ee1d2 --- /dev/null +++ b/migrations/public/versions/07c7128c166e_refactor_to_flask_security.py @@ -0,0 +1,62 @@ +"""Refactor to flask-security + +Revision ID: 07c7128c166e +Revises: b0ac53ab9d12 +Create Date: 2024-04-25 17:13:28.506058 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '07c7128c166e' +down_revision = 'b0ac53ab9d12' +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=80), nullable=True), + sa.Column('description', sa.String(length=255), nullable=True), + sa.PrimaryKeyConstraint('id'), + sa.UniqueConstraint('name'), + schema='public' + ) + op.create_table('roles_users', + sa.Column('user_id', sa.Integer(), nullable=False), + sa.Column('role_id', sa.Integer(), nullable=False), + sa.ForeignKeyConstraint(['role_id'], ['public.role.id'], ondelete='CASCADE'), + sa.ForeignKeyConstraint(['user_id'], ['public.user.id'], ondelete='CASCADE'), + sa.PrimaryKeyConstraint('user_id', 'role_id'), + schema='public' + ) + with op.batch_alter_table('user', schema=None) as batch_op: + batch_op.add_column(sa.Column('fs_uniquifier', sa.String(length=255), nullable=False)) + batch_op.create_unique_constraint(None, ['fs_uniquifier']) + batch_op.drop_constraint('user_tenant_id_fkey', type_='foreignkey') + batch_op.create_foreign_key(None, 'tenant', ['tenant_id'], ['id'], referent_schema='public') + batch_op.drop_column('is_admin') + batch_op.drop_column('is_super') + batch_op.drop_column('is_tester') + + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('user', schema=None) as batch_op: + batch_op.add_column(sa.Column('is_tester', sa.BOOLEAN(), autoincrement=False, nullable=True)) + batch_op.add_column(sa.Column('is_super', sa.BOOLEAN(), autoincrement=False, nullable=True)) + batch_op.add_column(sa.Column('is_admin', sa.BOOLEAN(), autoincrement=False, nullable=True)) + batch_op.drop_constraint(None, type_='foreignkey') + batch_op.create_foreign_key('user_tenant_id_fkey', 'tenant', ['tenant_id'], ['id']) + batch_op.drop_constraint(None, type_='unique') + batch_op.drop_column('fs_uniquifier') + + op.drop_table('roles_users', schema='public') + op.drop_table('role', schema='public') + # ### end Alembic commands ### diff --git a/migrations/public/versions/08e334d64cab_flask_security_active_attribute.py b/migrations/public/versions/08e334d64cab_flask_security_active_attribute.py new file mode 100644 index 0000000..2d5baa8 --- /dev/null +++ b/migrations/public/versions/08e334d64cab_flask_security_active_attribute.py @@ -0,0 +1,48 @@ +"""flask-security active attribute + +Revision ID: 08e334d64cab +Revises: 3ebec5c5b065 +Create Date: 2024-04-29 10:44:35.514268 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '08e334d64cab' +down_revision = '3ebec5c5b065' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('roles_users', schema=None) as batch_op: + batch_op.drop_constraint('roles_users_role_id_fkey', type_='foreignkey') + batch_op.drop_constraint('roles_users_user_id_fkey', type_='foreignkey') + batch_op.create_foreign_key(None, 'role', ['role_id'], ['id'], referent_schema='public', ondelete='CASCADE') + batch_op.create_foreign_key(None, 'user', ['user_id'], ['id'], referent_schema='public', ondelete='CASCADE') + + with op.batch_alter_table('user', schema=None) as batch_op: + batch_op.add_column(sa.Column('active', sa.Boolean(), nullable=True)) + batch_op.drop_constraint('user_tenant_id_fkey', type_='foreignkey') + batch_op.create_foreign_key(None, 'tenant', ['tenant_id'], ['id'], referent_schema='public') + + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('user', schema=None) as batch_op: + batch_op.drop_constraint(None, type_='foreignkey') + batch_op.create_foreign_key('user_tenant_id_fkey', 'tenant', ['tenant_id'], ['id']) + batch_op.drop_column('active') + + with op.batch_alter_table('roles_users', schema=None) as batch_op: + batch_op.drop_constraint(None, type_='foreignkey') + batch_op.drop_constraint(None, type_='foreignkey') + batch_op.create_foreign_key('roles_users_user_id_fkey', 'user', ['user_id'], ['id'], ondelete='CASCADE') + batch_op.create_foreign_key('roles_users_role_id_fkey', 'role', ['role_id'], ['id'], ondelete='CASCADE') + + # ### end Alembic commands ### diff --git a/migrations/public/versions/3ebec5c5b065_flask_security_trackable.py b/migrations/public/versions/3ebec5c5b065_flask_security_trackable.py new file mode 100644 index 0000000..d52010c --- /dev/null +++ b/migrations/public/versions/3ebec5c5b065_flask_security_trackable.py @@ -0,0 +1,60 @@ +"""flask-security Trackable + +Revision ID: 3ebec5c5b065 +Revises: 07c7128c166e +Create Date: 2024-04-25 18:01:32.126442 + +""" +from alembic import op +import sqlalchemy as sa +from sqlalchemy.dialects import postgresql + +# revision identifiers, used by Alembic. +revision = '3ebec5c5b065' +down_revision = '07c7128c166e' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('roles_users', schema=None) as batch_op: + batch_op.drop_constraint('roles_users_user_id_fkey', type_='foreignkey') + batch_op.drop_constraint('roles_users_role_id_fkey', type_='foreignkey') + batch_op.create_foreign_key(None, 'user', ['user_id'], ['id'], referent_schema='public', ondelete='CASCADE') + batch_op.create_foreign_key(None, 'role', ['role_id'], ['id'], referent_schema='public', ondelete='CASCADE') + + with op.batch_alter_table('user', schema=None) as batch_op: + batch_op.add_column(sa.Column('last_login_at', sa.DateTime(), nullable=True)) + batch_op.add_column(sa.Column('current_login_at', sa.DateTime(), nullable=True)) + batch_op.add_column(sa.Column('last_login_ip', sa.String(length=255), nullable=True)) + batch_op.add_column(sa.Column('current_login_ip', sa.String(length=255), nullable=True)) + batch_op.add_column(sa.Column('login_count', sa.Integer(), nullable=False)) + batch_op.drop_constraint('user_tenant_id_fkey', type_='foreignkey') + batch_op.create_foreign_key(None, 'tenant', ['tenant_id'], ['id'], referent_schema='public') + batch_op.drop_column('authenticated') + batch_op.drop_column('last_login') + + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('user', schema=None) as batch_op: + batch_op.add_column(sa.Column('last_login', postgresql.TIMESTAMP(), autoincrement=False, nullable=True)) + batch_op.add_column(sa.Column('authenticated', sa.BOOLEAN(), autoincrement=False, nullable=True)) + batch_op.drop_constraint(None, type_='foreignkey') + batch_op.create_foreign_key('user_tenant_id_fkey', 'tenant', ['tenant_id'], ['id']) + batch_op.drop_column('login_count') + batch_op.drop_column('current_login_ip') + batch_op.drop_column('last_login_ip') + batch_op.drop_column('current_login_at') + batch_op.drop_column('last_login_at') + + with op.batch_alter_table('roles_users', schema=None) as batch_op: + batch_op.drop_constraint(None, type_='foreignkey') + batch_op.drop_constraint(None, type_='foreignkey') + batch_op.create_foreign_key('roles_users_role_id_fkey', 'role', ['role_id'], ['id'], ondelete='CASCADE') + batch_op.create_foreign_key('roles_users_user_id_fkey', 'user', ['user_id'], ['id'], ondelete='CASCADE') + + # ### end Alembic commands ### diff --git a/migrations/tenant/versions/4f22dd6260e3_embeddings_for_openai.py b/migrations/tenant/versions/4f22dd6260e3_embeddings_for_openai.py new file mode 100644 index 0000000..7c3398d --- /dev/null +++ b/migrations/tenant/versions/4f22dd6260e3_embeddings_for_openai.py @@ -0,0 +1,36 @@ +"""Embeddings for OpenAI + +Revision ID: 4f22dd6260e3 +Revises: f5bbaed3a9ee +Create Date: 2024-04-30 21:18:35.548528 + +""" +from alembic import op +import sqlalchemy as sa +import pgvector + + +# revision identifiers, used by Alembic. +revision = '4f22dd6260e3' +down_revision = 'f5bbaed3a9ee' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('embedding_small_open_ai', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('doc_vers_id', sa.Integer(), nullable=False), + sa.Column('active', sa.Boolean(), nullable=False), + sa.Column('embedding', pgvector.sqlalchemy.Vector(dim=1536), nullable=False), + sa.ForeignKeyConstraint(['doc_vers_id'], ['document_version.id'], ), + sa.PrimaryKeyConstraint('id') + ) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('embedding_small_open_ai') + # ### end Alembic commands ### diff --git a/migrations/tenant/versions/83b4a2aedbf1_initial_document_models_added.py b/migrations/tenant/versions/83b4a2aedbf1_initial_document_models_added.py new file mode 100644 index 0000000..67b7abf --- /dev/null +++ b/migrations/tenant/versions/83b4a2aedbf1_initial_document_models_added.py @@ -0,0 +1,65 @@ +"""Initial Document Models added + +Revision ID: 83b4a2aedbf1 +Revises: +Create Date: 2024-04-24 14:39:05.302087 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '83b4a2aedbf1' +down_revision = None +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('document', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('name', sa.String(length=100), nullable=False), + sa.Column('tenant_id', sa.Integer(), nullable=False), + sa.Column('valid_from', sa.DateTime(), nullable=True), + sa.Column('valid_to', sa.DateTime(), nullable=True), + sa.Column('created_at', sa.DateTime(), server_default=sa.text('now()'), nullable=False), + sa.Column('created_by', sa.Integer(), nullable=False), + sa.Column('updated_at', sa.DateTime(), server_default=sa.text('now()'), nullable=False), + sa.Column('updated_by', sa.Integer(), nullable=True), + sa.ForeignKeyConstraint(['created_by'], ['public.user.id'], ), + sa.ForeignKeyConstraint(['tenant_id'], ['public.tenant.id'], ), + sa.ForeignKeyConstraint(['updated_by'], ['public.user.id'], ), + sa.PrimaryKeyConstraint('id') + ) + op.create_table('document_language', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('document_id', sa.Integer(), nullable=False), + sa.Column('language', sa.String(length=2), nullable=False), + sa.Column('created_at', sa.DateTime(), server_default=sa.text('now()'), nullable=False), + sa.Column('created_by', sa.Integer(), nullable=False), + sa.ForeignKeyConstraint(['created_by'], ['public.user.id'], ), + sa.ForeignKeyConstraint(['document_id'], ['document.id'], ), + sa.PrimaryKeyConstraint('id') + ) + op.create_table('document_version', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('doc_lang_id', sa.Integer(), nullable=False), + sa.Column('url', sa.String(length=200), nullable=True), + sa.Column('embeddings', sa.PickleType(), nullable=True), + sa.Column('created_at', sa.DateTime(), server_default=sa.text('now()'), nullable=False), + sa.Column('created_by', sa.Integer(), nullable=True), + sa.ForeignKeyConstraint(['created_by'], ['public.user.id'], ), + sa.ForeignKeyConstraint(['doc_lang_id'], ['document_language.id'], ), + sa.PrimaryKeyConstraint('id') + ) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('document_version') + op.drop_table('document_language') + op.drop_table('document') + # ### end Alembic commands ### diff --git a/migrations/tenant/versions/f5bbaed3a9ee_embeddings_according_to_pgvector_.py b/migrations/tenant/versions/f5bbaed3a9ee_embeddings_according_to_pgvector_.py new file mode 100644 index 0000000..583d66c --- /dev/null +++ b/migrations/tenant/versions/f5bbaed3a9ee_embeddings_according_to_pgvector_.py @@ -0,0 +1,56 @@ +"""Embeddings according to pgvector decision + +Revision ID: f5bbaed3a9ee +Revises: 83b4a2aedbf1 +Create Date: 2024-04-30 10:57:48.310528 + +""" +from alembic import op +import sqlalchemy as sa +from sqlalchemy.dialects import postgresql +import pgvector + +# revision identifiers, used by Alembic. +revision = 'f5bbaed3a9ee' +down_revision = '83b4a2aedbf1' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('embedding_mistral', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('doc_vers_id', sa.Integer(), nullable=False), + sa.Column('active', sa.Boolean(), nullable=False), + sa.Column('embedding', pgvector.sqlalchemy.Vector(dim=1024), nullable=False), + sa.ForeignKeyConstraint(['doc_vers_id'], ['document_version.id'], ), + sa.PrimaryKeyConstraint('id') + ) + op.add_column('document_language', sa.Column('updated_at', sa.DateTime(), server_default=sa.text('now()'), nullable=False)) + op.add_column('document_language', sa.Column('updated_by', sa.Integer(), nullable=True)) + op.create_foreign_key(None, 'document_language', 'user', ['updated_by'], ['id'], referent_schema='public') + op.add_column('document_version', sa.Column('file_location', sa.String(length=255), nullable=True)) + op.add_column('document_version', sa.Column('file_name', sa.String(length=200), nullable=True)) + op.add_column('document_version', sa.Column('file_type', sa.String(length=20), nullable=True)) + op.add_column('document_version', sa.Column('updated_at', sa.DateTime(), server_default=sa.text('now()'), nullable=False)) + op.add_column('document_version', sa.Column('updated_by', sa.Integer(), nullable=True)) + op.create_foreign_key(None, 'document_version', 'user', ['updated_by'], ['id'], referent_schema='public') + op.drop_column('document_version', 'embeddings') + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('document_version', sa.Column('embeddings', postgresql.BYTEA(), autoincrement=False, nullable=True)) + op.drop_constraint(None, 'document_version', type_='foreignkey') + op.drop_column('document_version', 'updated_by') + op.drop_column('document_version', 'updated_at') + op.drop_column('document_version', 'file_type') + op.drop_column('document_version', 'file_name') + op.drop_column('document_version', 'file_location') + op.drop_constraint(None, 'document_language', type_='foreignkey') + op.drop_column('document_language', 'updated_by') + op.drop_column('document_language', 'updated_at') + op.drop_table('embedding_mistral') + # ### end Alembic commands ###