Initial certificate system
This commit is contained in:
45
backend/alembic/env.py
Normal file
45
backend/alembic/env.py
Normal file
@@ -0,0 +1,45 @@
|
||||
from logging.config import fileConfig
|
||||
|
||||
from alembic import context
|
||||
from sqlalchemy import engine_from_config, pool
|
||||
|
||||
from app.core.config import settings
|
||||
from app.db.base import Base
|
||||
import app.models # noqa: F401
|
||||
|
||||
config = context.config
|
||||
config.set_main_option("sqlalchemy.url", settings.database_url)
|
||||
|
||||
if config.config_file_name is not None:
|
||||
fileConfig(config.config_file_name)
|
||||
|
||||
target_metadata = Base.metadata
|
||||
|
||||
|
||||
def run_migrations_offline() -> None:
|
||||
context.configure(
|
||||
url=settings.database_url,
|
||||
target_metadata=target_metadata,
|
||||
literal_binds=True,
|
||||
dialect_opts={"paramstyle": "named"},
|
||||
)
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
def run_migrations_online() -> None:
|
||||
connectable = engine_from_config(
|
||||
config.get_section(config.config_ini_section, {}),
|
||||
prefix="sqlalchemy.",
|
||||
poolclass=pool.NullPool,
|
||||
)
|
||||
with connectable.connect() as connection:
|
||||
context.configure(connection=connection, target_metadata=target_metadata)
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
if context.is_offline_mode():
|
||||
run_migrations_offline()
|
||||
else:
|
||||
run_migrations_online()
|
||||
23
backend/alembic/script.py.mako
Normal file
23
backend/alembic/script.py.mako
Normal file
@@ -0,0 +1,23 @@
|
||||
"""${message}
|
||||
|
||||
Revision ID: ${up_revision}
|
||||
Revises: ${down_revision | comma,n}
|
||||
Create Date: ${create_date}
|
||||
"""
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
${imports if imports else ""}
|
||||
|
||||
revision = ${repr(up_revision)}
|
||||
down_revision = ${repr(down_revision)}
|
||||
branch_labels = ${repr(branch_labels)}
|
||||
depends_on = ${repr(depends_on)}
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
${upgrades if upgrades else "pass"}
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
${downgrades if downgrades else "pass"}
|
||||
188
backend/alembic/versions/20260601_0001_initial.py
Normal file
188
backend/alembic/versions/20260601_0001_initial.py
Normal file
@@ -0,0 +1,188 @@
|
||||
"""initial schema
|
||||
|
||||
Revision ID: 20260601_0001
|
||||
Revises:
|
||||
Create Date: 2026-06-01
|
||||
"""
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
revision = "20260601_0001"
|
||||
down_revision = None
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.create_table(
|
||||
"roles",
|
||||
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
|
||||
sa.Column("code", sa.String(64), nullable=False),
|
||||
sa.Column("name", sa.String(64), nullable=False),
|
||||
sa.Column("created_at", sa.DateTime(), nullable=False),
|
||||
)
|
||||
op.create_index("ix_roles_code", "roles", ["code"], unique=True)
|
||||
|
||||
op.create_table(
|
||||
"admin_users",
|
||||
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
|
||||
sa.Column("username", sa.String(64), nullable=False),
|
||||
sa.Column("password_hash", sa.String(255), nullable=False),
|
||||
sa.Column("role_code", sa.String(64), nullable=False),
|
||||
sa.Column("status", sa.String(32), nullable=False),
|
||||
sa.Column("created_at", sa.DateTime(), nullable=False),
|
||||
sa.Column("updated_at", sa.DateTime(), nullable=False),
|
||||
)
|
||||
op.create_index("ix_admin_users_username", "admin_users", ["username"], unique=True)
|
||||
op.create_index("ix_admin_users_role_code", "admin_users", ["role_code"])
|
||||
|
||||
op.create_table(
|
||||
"learners",
|
||||
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
|
||||
sa.Column("phone", sa.String(32), nullable=False),
|
||||
sa.Column("current_name", sa.String(64), nullable=False),
|
||||
sa.Column("id_type", sa.String(32), nullable=True),
|
||||
sa.Column("id_no_encrypted", sa.String(256), nullable=True),
|
||||
sa.Column("id_no_masked", sa.String(64), nullable=True),
|
||||
sa.Column("student_no", sa.String(64), nullable=True),
|
||||
sa.Column("status", sa.String(32), nullable=False),
|
||||
sa.Column("remark", sa.Text(), nullable=True),
|
||||
sa.Column("created_at", sa.DateTime(), nullable=False),
|
||||
sa.Column("updated_at", sa.DateTime(), nullable=False),
|
||||
)
|
||||
op.create_index("ix_learners_phone", "learners", ["phone"], unique=True)
|
||||
op.create_index("ix_learners_current_name", "learners", ["current_name"])
|
||||
|
||||
op.create_table(
|
||||
"learner_name_histories",
|
||||
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
|
||||
sa.Column("learner_id", sa.Integer(), nullable=False),
|
||||
sa.Column("name", sa.String(64), nullable=False),
|
||||
sa.Column("source", sa.String(64), nullable=False),
|
||||
sa.Column("created_at", sa.DateTime(), nullable=False),
|
||||
)
|
||||
op.create_index("ix_learner_name_histories_learner_id", "learner_name_histories", ["learner_id"])
|
||||
|
||||
op.create_table(
|
||||
"learner_phone_histories",
|
||||
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
|
||||
sa.Column("learner_id", sa.Integer(), nullable=False),
|
||||
sa.Column("old_phone", sa.String(32), nullable=False),
|
||||
sa.Column("new_phone", sa.String(32), nullable=False),
|
||||
sa.Column("reason", sa.String(255), nullable=False),
|
||||
sa.Column("changed_by", sa.Integer(), nullable=True),
|
||||
sa.Column("created_at", sa.DateTime(), nullable=False),
|
||||
)
|
||||
op.create_index("ix_learner_phone_histories_learner_id", "learner_phone_histories", ["learner_id"])
|
||||
|
||||
op.create_table(
|
||||
"project_courses",
|
||||
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
|
||||
sa.Column("code", sa.String(16), nullable=False),
|
||||
sa.Column("name", sa.String(128), nullable=False),
|
||||
sa.Column("status", sa.String(32), nullable=False),
|
||||
sa.Column("created_at", sa.DateTime(), nullable=False),
|
||||
sa.Column("updated_at", sa.DateTime(), nullable=False),
|
||||
)
|
||||
op.create_index("ix_project_courses_code", "project_courses", ["code"], unique=True)
|
||||
|
||||
op.create_table(
|
||||
"certificates",
|
||||
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
|
||||
sa.Column("learner_id", sa.Integer(), nullable=False),
|
||||
sa.Column("project_code", sa.String(16), nullable=False),
|
||||
sa.Column("certificate_no", sa.String(64), nullable=False),
|
||||
sa.Column("certificate_name", sa.String(128), nullable=False),
|
||||
sa.Column("class_name", sa.String(128), nullable=True),
|
||||
sa.Column("course_name", sa.String(128), nullable=True),
|
||||
sa.Column("stage_name", sa.String(128), nullable=True),
|
||||
sa.Column("issue_date", sa.Date(), nullable=False),
|
||||
sa.Column("issuer_name", sa.String(128), nullable=False),
|
||||
sa.Column("status", sa.String(32), nullable=False),
|
||||
sa.Column("pdf_status", sa.String(32), nullable=False),
|
||||
sa.Column("pdf_file_path", sa.String(512), nullable=True),
|
||||
sa.Column("public_token_id", sa.Integer(), nullable=True),
|
||||
sa.Column("qr_token_id", sa.Integer(), nullable=True),
|
||||
sa.Column("remark", sa.Text(), nullable=True),
|
||||
sa.Column("created_at", sa.DateTime(), nullable=False),
|
||||
sa.Column("updated_at", sa.DateTime(), nullable=False),
|
||||
)
|
||||
op.create_index("ix_certificates_learner_id", "certificates", ["learner_id"])
|
||||
op.create_index("ix_certificates_project_code", "certificates", ["project_code"])
|
||||
op.create_index("ix_certificates_certificate_no", "certificates", ["certificate_no"], unique=True)
|
||||
|
||||
op.create_table(
|
||||
"certificate_access_tokens",
|
||||
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
|
||||
sa.Column("certificate_id", sa.Integer(), nullable=False),
|
||||
sa.Column("token_hash", sa.String(128), nullable=False),
|
||||
sa.Column("token_value", sa.String(128), nullable=False),
|
||||
sa.Column("token_type", sa.String(32), nullable=False),
|
||||
sa.Column("status", sa.String(32), nullable=False),
|
||||
sa.Column("created_reason", sa.String(128), nullable=False),
|
||||
sa.Column("revoked_at", sa.DateTime(), nullable=True),
|
||||
sa.Column("last_access_at", sa.DateTime(), nullable=True),
|
||||
sa.Column("created_at", sa.DateTime(), nullable=False),
|
||||
)
|
||||
op.create_index("ix_certificate_access_tokens_certificate_id", "certificate_access_tokens", ["certificate_id"])
|
||||
op.create_index("ix_certificate_access_tokens_token_hash", "certificate_access_tokens", ["token_hash"], unique=True)
|
||||
op.create_index("ix_certificate_access_tokens_token_value", "certificate_access_tokens", ["token_value"], unique=True)
|
||||
|
||||
op.create_table(
|
||||
"import_batches",
|
||||
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
|
||||
sa.Column("filename", sa.String(255), nullable=False),
|
||||
sa.Column("file_path", sa.String(512), nullable=False),
|
||||
sa.Column("status", sa.String(32), nullable=False),
|
||||
sa.Column("total_rows", sa.Integer(), nullable=False),
|
||||
sa.Column("valid_rows", sa.Integer(), nullable=False),
|
||||
sa.Column("failed_rows", sa.Integer(), nullable=False),
|
||||
sa.Column("conflict_rows", sa.Integer(), nullable=False),
|
||||
sa.Column("error_report_path", sa.String(512), nullable=True),
|
||||
sa.Column("created_by", sa.Integer(), nullable=True),
|
||||
sa.Column("created_at", sa.DateTime(), nullable=False),
|
||||
sa.Column("updated_at", sa.DateTime(), nullable=False),
|
||||
)
|
||||
op.create_index("ix_import_batches_created_by", "import_batches", ["created_by"])
|
||||
|
||||
op.create_table(
|
||||
"import_batch_rows",
|
||||
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
|
||||
sa.Column("batch_id", sa.Integer(), nullable=False),
|
||||
sa.Column("row_no", sa.Integer(), nullable=False),
|
||||
sa.Column("status", sa.String(32), nullable=False),
|
||||
sa.Column("error_message", sa.Text(), nullable=True),
|
||||
sa.Column("raw_json", sa.Text(), nullable=True),
|
||||
sa.Column("created_at", sa.DateTime(), nullable=False),
|
||||
)
|
||||
op.create_index("ix_import_batch_rows_batch_id", "import_batch_rows", ["batch_id"])
|
||||
|
||||
op.create_table(
|
||||
"operation_logs",
|
||||
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
|
||||
sa.Column("admin_user_id", sa.Integer(), nullable=True),
|
||||
sa.Column("action", sa.String(64), nullable=False),
|
||||
sa.Column("object_type", sa.String(64), nullable=False),
|
||||
sa.Column("object_id", sa.String(64), nullable=True),
|
||||
sa.Column("ip_address", sa.String(64), nullable=True),
|
||||
sa.Column("detail_json", sa.Text(), nullable=True),
|
||||
sa.Column("created_at", sa.DateTime(), nullable=False),
|
||||
)
|
||||
op.create_index("ix_operation_logs_admin_user_id", "operation_logs", ["admin_user_id"])
|
||||
op.create_index("ix_operation_logs_action", "operation_logs", ["action"])
|
||||
op.create_index("ix_operation_logs_object_type", "operation_logs", ["object_type"])
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_table("operation_logs")
|
||||
op.drop_table("import_batch_rows")
|
||||
op.drop_table("import_batches")
|
||||
op.drop_table("certificate_access_tokens")
|
||||
op.drop_table("certificates")
|
||||
op.drop_table("project_courses")
|
||||
op.drop_table("learner_phone_histories")
|
||||
op.drop_table("learner_name_histories")
|
||||
op.drop_table("learners")
|
||||
op.drop_table("admin_users")
|
||||
op.drop_table("roles")
|
||||
34
backend/alembic/versions/20260602_0002_project_defaults.py
Normal file
34
backend/alembic/versions/20260602_0002_project_defaults.py
Normal file
@@ -0,0 +1,34 @@
|
||||
"""add project certificate defaults
|
||||
|
||||
Revision ID: 20260602_0002
|
||||
Revises: 20260601_0001
|
||||
Create Date: 2026-06-02
|
||||
"""
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
revision = "20260602_0002"
|
||||
down_revision = "20260601_0001"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.add_column("project_courses", sa.Column("default_certificate_name", sa.String(length=128), nullable=True))
|
||||
op.add_column("project_courses", sa.Column("default_course_name", sa.String(length=128), nullable=True))
|
||||
op.add_column("project_courses", sa.Column("default_stage_name", sa.String(length=128), nullable=True))
|
||||
op.add_column("project_courses", sa.Column("default_issuer_name", sa.String(length=128), nullable=True))
|
||||
op.execute("update project_courses set default_certificate_name='培训结业证书' where default_certificate_name is null")
|
||||
op.execute("update project_courses set default_course_name=name where default_course_name is null")
|
||||
op.execute("update project_courses set default_issuer_name='本公司' where default_issuer_name is null")
|
||||
op.alter_column("project_courses", "default_certificate_name", nullable=False)
|
||||
op.alter_column("project_courses", "default_issuer_name", nullable=False)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_column("project_courses", "default_issuer_name")
|
||||
op.drop_column("project_courses", "default_stage_name")
|
||||
op.drop_column("project_courses", "default_course_name")
|
||||
op.drop_column("project_courses", "default_certificate_name")
|
||||
Reference in New Issue
Block a user