From 98207edb138c04a576d157dcb2ecf872a6f443e2 Mon Sep 17 00:00:00 2001 From: rimu <3310831+rimu@users.noreply.github.com> Date: Tue, 16 Apr 2024 21:16:10 +1200 Subject: [PATCH] migration for tags and languages --- app/models.py | 27 ++++++++ .../fd2af23f4b1f_tags_and_languages.py | 69 +++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 migrations/versions/fd2af23f4b1f_tags_and_languages.py diff --git a/app/models.py b/app/models.py index b96264f7..6a174cff 100644 --- a/app/models.py +++ b/app/models.py @@ -1294,6 +1294,33 @@ class Site(db.Model): return User.query.filter_by(deleted=False, banned=False).join(user_role).filter(user_role.c.role_id == 4).all() +class Tag(db.Model): + id = db.Column(db.Integer, primary_key=True) + name = db.Column(db.String(256)) + + +class Language(db.Model): + id = db.Column(db.Integer, primary_key=True) + code = db.Column(db.String(5), index=True) + name = db.Column(db.String(50)) + + +post_language = db.Table('post_language', db.Column('post_id', db.Integer, db.ForeignKey('post.id')), + db.Column('language_id', db.Integer, db.ForeignKey('language.id')), + db.PrimaryKeyConstraint('post_id', 'language_id') + ) + +community_language = db.Table('community_language', db.Column('community_id', db.Integer, db.ForeignKey('community.id')), + db.Column('language_id', db.Integer, db.ForeignKey('language.id')), + db.PrimaryKeyConstraint('community_id', 'language_id') + ) + +post_tag = db.Table('post_tag', db.Column('post_id', db.Integer, db.ForeignKey('post.id')), + db.Column('tag_id', db.Integer, db.ForeignKey('tag.id')), + db.PrimaryKeyConstraint('post_id', 'tag_id') + ) + + @login.user_loader def load_user(id): return User.query.get(int(id)) diff --git a/migrations/versions/fd2af23f4b1f_tags_and_languages.py b/migrations/versions/fd2af23f4b1f_tags_and_languages.py new file mode 100644 index 00000000..6c5e0a00 --- /dev/null +++ b/migrations/versions/fd2af23f4b1f_tags_and_languages.py @@ -0,0 +1,69 @@ +"""tags and languages + +Revision ID: fd2af23f4b1f +Revises: 91a931afd6d9 +Create Date: 2024-04-16 21:15:07.225254 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'fd2af23f4b1f' +down_revision = '91a931afd6d9' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('language', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('code', sa.String(length=5), nullable=True), + sa.Column('name', sa.String(length=50), nullable=True), + sa.PrimaryKeyConstraint('id') + ) + with op.batch_alter_table('language', schema=None) as batch_op: + batch_op.create_index(batch_op.f('ix_language_code'), ['code'], unique=False) + + op.create_table('tag', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('name', sa.String(length=256), nullable=True), + sa.PrimaryKeyConstraint('id') + ) + op.create_table('community_language', + sa.Column('community_id', sa.Integer(), nullable=False), + sa.Column('language_id', sa.Integer(), nullable=False), + sa.ForeignKeyConstraint(['community_id'], ['community.id'], ), + sa.ForeignKeyConstraint(['language_id'], ['language.id'], ), + sa.PrimaryKeyConstraint('community_id', 'language_id') + ) + op.create_table('post_language', + sa.Column('post_id', sa.Integer(), nullable=False), + sa.Column('language_id', sa.Integer(), nullable=False), + sa.ForeignKeyConstraint(['language_id'], ['language.id'], ), + sa.ForeignKeyConstraint(['post_id'], ['post.id'], ), + sa.PrimaryKeyConstraint('post_id', 'language_id') + ) + op.create_table('post_tag', + sa.Column('post_id', sa.Integer(), nullable=False), + sa.Column('tag_id', sa.Integer(), nullable=False), + sa.ForeignKeyConstraint(['post_id'], ['post.id'], ), + sa.ForeignKeyConstraint(['tag_id'], ['tag.id'], ), + sa.PrimaryKeyConstraint('post_id', 'tag_id') + ) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('post_tag') + op.drop_table('post_language') + op.drop_table('community_language') + op.drop_table('tag') + with op.batch_alter_table('language', schema=None) as batch_op: + batch_op.drop_index(batch_op.f('ix_language_code')) + + op.drop_table('language') + # ### end Alembic commands ###