02: Changed setup for alembic and sqlAlchemy

This commit is contained in:
exolonConfidental
2026-02-06 22:22:59 +05:30
parent 61162e01fb
commit 8fb3b7cf67
14 changed files with 739 additions and 25 deletions

1
alembic/README Normal file
View File

@@ -0,0 +1 @@
Generic single-database configuration.

77
alembic/env.py Normal file
View File

@@ -0,0 +1,77 @@
from logging.config import fileConfig
from sqlalchemy import pool
from sqlalchemy.ext.asyncio import async_engine_from_config
from alembic import context
from dotenv import load_dotenv
import os
load_dotenv()
config = context.config
config.set_main_option(
"sqlalchemy.url",
os.getenv("DATABASE_URL")
)
if config.config_file_name is not None:
fileConfig(config.config_file_name)
from app.db.models.base import Base
from app.db.models.location import Location
from app.db.models.owner import Owner
from app.db.models.property import Property
from app.db.models.location import Location
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()
async def run_migrations_online():
connectable = async_engine_from_config(
config.get_section(config.config_ini_section),
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)
async with connectable.connect() as connection:
def do_run_migrations(connection):
context.configure(
connection=connection,
target_metadata=target_metadata,
)
with context.begin_transaction():
context.run_migrations()
await connection.run_sync(do_run_migrations)
await connectable.dispose()
def run():
if context.is_offline_mode():
run_migrations_offline()
else:
import asyncio
asyncio.run(run_migrations_online())
run()

28
alembic/script.py.mako Normal file
View File

@@ -0,0 +1,28 @@
"""${message}
Revision ID: ${up_revision}
Revises: ${down_revision | comma,n}
Create Date: ${create_date}
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
${imports if imports else ""}
# revision identifiers, used by Alembic.
revision: str = ${repr(up_revision)}
down_revision: Union[str, Sequence[str], None] = ${repr(down_revision)}
branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}
depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)}
def upgrade() -> None:
"""Upgrade schema."""
${upgrades if upgrades else "pass"}
def downgrade() -> None:
"""Downgrade schema."""
${downgrades if downgrades else "pass"}

View File

@@ -0,0 +1,98 @@
"""initial migration
Revision ID: 6cd12cae8c96
Revises:
Create Date: 2026-02-06 22:11:10.583387
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = '6cd12cae8c96'
down_revision: Union[str, Sequence[str], None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
"""Upgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('locations',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('place_id', sa.BigInteger(), nullable=False),
sa.Column('osm_id', sa.BigInteger(), nullable=False),
sa.Column('osm_type', sa.String(length=20), nullable=False),
sa.Column('latitude', sa.Float(), nullable=False),
sa.Column('longitude', sa.Float(), nullable=False),
sa.Column('house_number', sa.String(length=20), nullable=True),
sa.Column('road', sa.String(length=150), nullable=False),
sa.Column('city', sa.String(length=100), nullable=False),
sa.Column('county', sa.String(length=100), nullable=False),
sa.Column('state', sa.String(length=100), nullable=False),
sa.Column('postcode', sa.String(length=20), nullable=False),
sa.Column('country', sa.String(length=100), nullable=False),
sa.Column('country_code', sa.String(length=10), nullable=False),
sa.Column('display_name', sa.String(length=300), nullable=False),
sa.Column('bbox_lat_min', sa.Float(), nullable=False),
sa.Column('bbox_lat_max', sa.Float(), nullable=False),
sa.Column('bbox_lon_min', sa.Float(), nullable=False),
sa.Column('bbox_lon_max', sa.Float(), nullable=False),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_locations_id'), 'locations', ['id'], unique=False)
op.create_index(op.f('ix_locations_place_id'), 'locations', ['place_id'], unique=True)
op.create_table('owners',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('full_name', sa.String(length=150), nullable=False),
sa.Column('phone_number', sa.String(length=20), nullable=False),
sa.Column('email', sa.String(length=150), nullable=False),
sa.Column('occupation', sa.String(length=100), nullable=False),
sa.Column('annual_income_range', sa.String(length=50), nullable=False),
sa.Column('willing_to_rent', sa.Boolean(), nullable=False),
sa.Column('desired_rent_price', sa.Integer(), nullable=True),
sa.Column('willing_to_sell', sa.Boolean(), nullable=False),
sa.Column('desired_sell_price', sa.Integer(), nullable=True),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('email'),
sa.UniqueConstraint('phone_number')
)
op.create_table('properties',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('property_type', sa.String(length=50), nullable=False),
sa.Column('built_up_area', sa.Float(), nullable=False),
sa.Column('bedrooms', sa.Integer(), nullable=False),
sa.Column('bathrooms', sa.Integer(), nullable=False),
sa.Column('purchase_date', sa.Date(), nullable=False),
sa.Column('purchase_price', sa.Float(), nullable=False),
sa.Column('sale_listing_date', sa.Date(), nullable=True),
sa.Column('sale_asking_price', sa.Float(), nullable=True),
sa.Column('last_renovation_date', sa.Date(), nullable=True),
sa.Column('renovation_description', sa.String(length=255), nullable=True),
sa.Column('roof_repair_date', sa.Date(), nullable=True),
sa.Column('roof_condition', sa.String(length=50), nullable=False),
sa.Column('available_for_rent', sa.Boolean(), nullable=False),
sa.Column('expected_rent', sa.Float(), nullable=True),
sa.Column('rental_available_date', sa.Date(), nullable=True),
sa.Column('owner_id', sa.Integer(), nullable=False),
sa.Column('location_id', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['location_id'], ['locations.id'], ondelete='CASCADE'),
sa.ForeignKeyConstraint(['owner_id'], ['owners.id'], ondelete='CASCADE'),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('location_id')
)
# ### end Alembic commands ###
def downgrade() -> None:
"""Downgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('properties')
op.drop_table('owners')
op.drop_index(op.f('ix_locations_place_id'), table_name='locations')
op.drop_index(op.f('ix_locations_id'), table_name='locations')
op.drop_table('locations')
# ### end Alembic commands ###