1
0
Эх сурвалжийг харах

feat: 添加数据库模型

SongZihuan 2 жил өмнө
parent
commit
bcabb80803

+ 10 - 0
README.md

@@ -2,6 +2,16 @@
 基于Python-Flask的沟通交流平台。
 
 ## 初始化数据库
+### 使用migration
+```shell
+$ flask db upgrade e6b4151608e7
+$ flask shell
+$ >>> from app.db import create_all  
+$ >>> create_all()
+$ >>> quit()
+```
+
+### 不使用migration
 ```shell
 $ flask shell
 $ >>> from app.db import create_all  

+ 5 - 1
app/db.py

@@ -1,3 +1,4 @@
+import sqlalchemy
 from flask_sqlalchemy import SQLAlchemy
 from flask_login import UserMixin, AnonymousUserMixin
 from datetime import datetime
@@ -90,7 +91,10 @@ class Archive(db.Model):
 
 
 def create_all():
-    db.create_all()
+    try:
+        db.create_all()
+    except Exception:
+        pass
 
     admin = Role(name="admin", permission=2047)
     coordinator = Role(name="coordinator", permission=1023)

+ 82 - 0
migrations/versions/e6b4151608e7_.py

@@ -0,0 +1,82 @@
+"""empty message
+
+Revision ID: e6b4151608e7
+Revises: 
+Create Date: 2022-10-15 18:47:18.044633
+
+"""
+from alembic import op
+import sqlalchemy as sa
+
+
+# revision identifiers, used by Alembic.
+revision = 'e6b4151608e7'
+down_revision = None
+branch_labels = None
+depends_on = None
+
+
+def upgrade():
+    # ### commands auto generated by Alembic - please adjust! ###
+    op.create_table('archive',
+    sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
+    sa.Column('name', sa.String(length=32), nullable=False),
+    sa.Column('describe', sa.String(length=100), nullable=False),
+    sa.PrimaryKeyConstraint('id'),
+    sa.UniqueConstraint('name')
+    )
+    op.create_table('role',
+    sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
+    sa.Column('name', sa.String(length=32), nullable=False),
+    sa.Column('permission', sa.Integer(), nullable=False),
+    sa.PrimaryKeyConstraint('id'),
+    sa.UniqueConstraint('name')
+    )
+    op.create_table('user',
+    sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
+    sa.Column('email', sa.String(length=32), nullable=False),
+    sa.Column('passwd_hash', sa.String(length=128), nullable=False),
+    sa.Column('role_id', sa.Integer(), nullable=True),
+    sa.ForeignKeyConstraint(['role_id'], ['role.id'], ),
+    sa.PrimaryKeyConstraint('id'),
+    sa.UniqueConstraint('email')
+    )
+    op.create_table('comment',
+    sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
+    sa.Column('title', sa.String(length=32), nullable=True),
+    sa.Column('content', sa.Text(), nullable=False),
+    sa.Column('create_time', sa.DateTime(), nullable=False),
+    sa.Column('update_time', sa.DateTime(), nullable=False),
+    sa.Column('auth_id', sa.Integer(), nullable=True),
+    sa.Column('father_id', sa.Integer(), nullable=True),
+    sa.ForeignKeyConstraint(['auth_id'], ['user.id'], ),
+    sa.ForeignKeyConstraint(['father_id'], ['comment.id'], ),
+    sa.PrimaryKeyConstraint('id', 'auth_id', 'father_id')
+    )
+    op.create_table('follow',
+    sa.Column('time', sa.DateTime(), nullable=False),
+    sa.Column('follower_id', sa.Integer(), nullable=True),
+    sa.Column('followed_id', sa.Integer(), nullable=True),
+    sa.ForeignKeyConstraint(['followed_id'], ['user.id'], ),
+    sa.ForeignKeyConstraint(['follower_id'], ['user.id'], ),
+    sa.PrimaryKeyConstraint('follower_id', 'followed_id')
+    )
+    op.create_table('archive_comment',
+    sa.Column('archive_id', sa.Integer(), nullable=False),
+    sa.Column('comment_id', sa.Integer(), nullable=False),
+    sa.ForeignKeyConstraint(['archive_id'], ['archive.id'], ),
+    sa.ForeignKeyConstraint(['comment_id'], ['comment.id'], ),
+    sa.PrimaryKeyConstraint('archive_id', 'comment_id')
+    )
+    # ### end Alembic commands ###
+
+
+def downgrade():
+    # ### commands auto generated by Alembic - please adjust! ###
+    op.drop_table('archive_comment')
+    op.drop_table('follow')
+    op.drop_table('comment')
+    op.drop_table('user')
+    op.drop_table('role')
+    op.drop_table('archive')
+    # ### end Alembic commands ###

+ 0 - 50
migrations/versions/f0c2c737b69b_.py

@@ -1,50 +0,0 @@
-"""empty message
-
-Revision ID: f0c2c737b69b
-Revises: 
-Create Date: 2022-10-15 18:44:21.972709
-
-"""
-from alembic import op
-import sqlalchemy as sa
-from sqlalchemy.dialects import mysql
-
-# revision identifiers, used by Alembic.
-revision = 'f0c2c737b69b'
-down_revision = None
-branch_labels = None
-depends_on = None
-
-
-def upgrade():
-    # ### commands auto generated by Alembic - please adjust! ###
-    op.alter_column('comment', 'auth_id',
-               existing_type=mysql.INTEGER(),
-               nullable=True)
-    op.alter_column('comment', 'father_id',
-               existing_type=mysql.INTEGER(),
-               nullable=True)
-    op.alter_column('follow', 'follower_id',
-               existing_type=mysql.INTEGER(),
-               nullable=True)
-    op.alter_column('follow', 'followed_id',
-               existing_type=mysql.INTEGER(),
-               nullable=True)
-    # ### end Alembic commands ###
-
-
-def downgrade():
-    # ### commands auto generated by Alembic - please adjust! ###
-    op.alter_column('follow', 'followed_id',
-               existing_type=mysql.INTEGER(),
-               nullable=False)
-    op.alter_column('follow', 'follower_id',
-               existing_type=mysql.INTEGER(),
-               nullable=False)
-    op.alter_column('comment', 'father_id',
-               existing_type=mysql.INTEGER(),
-               nullable=False)
-    op.alter_column('comment', 'auth_id',
-               existing_type=mysql.INTEGER(),
-               nullable=False)
-    # ### end Alembic commands ###