initial commit

This commit is contained in:
kiaro37
2026-01-26 13:33:54 +03:00
commit 21cb493267
21 changed files with 1028 additions and 0 deletions

56
app/alembic/env.py Normal file
View File

@@ -0,0 +1,56 @@
from logging.config import fileConfig
from sqlalchemy import engine_from_config, pool
from alembic import context
import os
import sys
# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config
# Interpret the config file for Python logging.
# This line sets up loggers basically.
if config.config_file_name is not None:
fileConfig(config.config_file_name)
# add your model's MetaData object here
sys.path.append(os.path.join(os.path.dirname(__file__), "..", ".."))
from app.db import Base # type: ignore
from app.models import * # noqa
target_metadata = Base.metadata
def run_migrations_offline():
url = config.get_main_option("sqlalchemy.url")
context.configure(
url=url,
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
)
with context.begin_transaction():
context.run_migrations()
def run_migrations_online():
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()

View File

@@ -0,0 +1,97 @@
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
# revision identifiers, used by Alembic.
revision = '2024_01_01_000001'
down_revision = None
branch_labels = None
depends_on = None
def upgrade() -> None:
op.execute("CREATE EXTENSION IF NOT EXISTS pg_trgm;")
op.create_table(
'users',
sa.Column('id', sa.Integer(), primary_key=True),
sa.Column('email', sa.String(length=255), nullable=False),
sa.Column('password_hash', sa.String(length=255), nullable=False),
sa.Column('role', sa.Enum('user', 'admin', name='userrole'), nullable=False, server_default='user'),
sa.Column('fcm_token', sa.String(length=512), nullable=True),
sa.Column('is_active', sa.Boolean(), server_default=sa.text('true')),
sa.Column('created_at', sa.DateTime(), server_default=sa.text('now()')),
)
op.create_index('ix_users_email', 'users', ['email'], unique=True)
op.create_table(
'cemeteries',
sa.Column('id', sa.Integer(), primary_key=True),
sa.Column('name', sa.String(length=255), nullable=False),
sa.Column('description', sa.Text(), nullable=True),
sa.Column('center_lat', sa.Float(), nullable=False),
sa.Column('center_lon', sa.Float(), nullable=False),
sa.Column('zoom', sa.Integer(), server_default='15'),
sa.Column('created_at', sa.DateTime(), server_default=sa.text('now()')),
)
op.create_table(
'graves',
sa.Column('id', sa.Integer(), primary_key=True),
sa.Column('cemetery_id', sa.Integer(), sa.ForeignKey('cemeteries.id', ondelete='CASCADE'), nullable=False),
sa.Column('lat', sa.Float(), nullable=False),
sa.Column('lon', sa.Float(), nullable=False),
sa.Column('full_name', sa.String(length=255), nullable=False),
sa.Column('birth_date', sa.String(length=32), nullable=True),
sa.Column('death_date', sa.String(length=32), nullable=True),
sa.Column('temp_photo_path', sa.String(length=512), nullable=True),
sa.Column('photo_url', sa.String(length=512), nullable=True),
sa.Column('status', sa.Enum('pending', 'approved', 'rejected', name='gravestatus'), server_default='pending', nullable=False),
sa.Column('created_by', sa.Integer(), sa.ForeignKey('users.id', ondelete='SET NULL'), nullable=True),
sa.Column('created_at', sa.DateTime(), server_default=sa.text('now()')),
sa.Column('updated_at', sa.DateTime(), server_default=sa.text('now()')),
)
op.create_index('ix_graves_full_name', 'graves', ['full_name'])
op.create_table(
'grave_histories',
sa.Column('id', sa.Integer(), primary_key=True),
sa.Column('grave_id', sa.Integer(), sa.ForeignKey('graves.id', ondelete='CASCADE'), nullable=False),
sa.Column('action', sa.String(length=64), nullable=False),
sa.Column('changes', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('actor_id', sa.Integer(), sa.ForeignKey('users.id', ondelete='SET NULL'), nullable=True),
sa.Column('created_at', sa.DateTime(), server_default=sa.text('now()')),
)
op.create_table(
'favorites',
sa.Column('id', sa.Integer(), primary_key=True),
sa.Column('user_id', sa.Integer(), sa.ForeignKey('users.id', ondelete='CASCADE'), nullable=False),
sa.Column('grave_id', sa.Integer(), sa.ForeignKey('graves.id', ondelete='CASCADE'), nullable=False),
sa.Column('created_at', sa.DateTime(), server_default=sa.text('now()')),
sa.UniqueConstraint('user_id', 'grave_id', name='uq_user_grave'),
)
op.create_table(
'app_logs',
sa.Column('id', sa.Integer(), primary_key=True),
sa.Column('level', sa.String(length=32), nullable=False),
sa.Column('message', sa.Text(), nullable=False),
sa.Column('context', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('created_at', sa.DateTime(), server_default=sa.text('now()')),
)
def downgrade() -> None:
op.drop_table('app_logs')
op.drop_table('favorites')
op.drop_table('grave_histories')
op.drop_index('ix_graves_full_name', table_name='graves')
op.drop_table('graves')
op.drop_table('cemeteries')
op.drop_index('ix_users_email', table_name='users')
op.drop_table('users')
op.execute("DROP TYPE IF EXISTS userrole;")
op.execute("DROP TYPE IF EXISTS gravestatus;")