Files
personal-portfolio/scripts/db/init_schema.py
l3ocho 62d1a52eed
Some checks failed
CI / lint-and-test (pull_request) Has been cancelled
refactor: multi-dashboard structural migration
- Rename dbt project from toronto_housing to portfolio
- Restructure dbt models into domain subdirectories:
  - shared/ for cross-domain dimensions (dim_time)
  - staging/toronto/, intermediate/toronto/, marts/toronto/
- Update SQLAlchemy models for raw_toronto schema
- Add explicit cross-schema FK relationships for FactRentals
- Namespace figure factories under figures/toronto/
- Namespace notebooks under notebooks/toronto/
- Update Makefile with domain-specific targets and env loading
- Update all documentation for multi-dashboard structure

This enables adding new dashboard projects (e.g., /football, /energy)
without structural conflicts or naming collisions.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-01 19:08:20 -05:00

70 lines
1.9 KiB
Python

#!/usr/bin/env python3
"""Initialize database schema.
Usage:
python scripts/db/init_schema.py
This script creates all SQLAlchemy tables in the database.
Run this after docker-compose up to initialize the schema.
"""
import sys
from pathlib import Path
# Add project root to path
sys.path.insert(0, str(Path(__file__).parent.parent.parent))
from portfolio_app.toronto.models import create_tables, get_engine # noqa: E402
from portfolio_app.toronto.models.dimensions import RAW_TORONTO_SCHEMA # noqa: E402
def main() -> int:
"""Initialize the database schema."""
print("Initializing database schema...")
try:
engine = get_engine()
# Test connection
from sqlalchemy import text
with engine.connect() as conn:
result = conn.execute(text("SELECT 1"))
result.fetchone()
print("Database connection successful")
# Create domain-specific schemas
with engine.connect() as conn:
conn.execute(text(f"CREATE SCHEMA IF NOT EXISTS {RAW_TORONTO_SCHEMA}"))
conn.commit()
print(f"Created schema: {RAW_TORONTO_SCHEMA}")
# Create all tables
create_tables()
print("Schema created successfully")
# List created tables by schema
from sqlalchemy import inspect
inspector = inspect(engine)
# Public schema tables
public_tables = inspector.get_table_names(schema="public")
if public_tables:
print(f"Public schema tables: {', '.join(public_tables)}")
# raw_toronto schema tables
toronto_tables = inspector.get_table_names(schema=RAW_TORONTO_SCHEMA)
if toronto_tables:
print(f"{RAW_TORONTO_SCHEMA} schema tables: {', '.join(toronto_tables)}")
return 0
except Exception as e:
print(f"Error: {e}", file=sys.stderr)
return 1
if __name__ == "__main__":
sys.exit(main())