init commit

This commit is contained in:
2025-09-25 08:05:25 +09:00
commit 4d7551d4f1
56 changed files with 5977 additions and 0 deletions

89
alembic/env.py Normal file
View File

@@ -0,0 +1,89 @@
from logging.config import fileConfig
from sqlalchemy import engine_from_config
from sqlalchemy import pool
from alembic import context
import asyncio
from sqlalchemy.ext.asyncio import AsyncEngine
# Import all models to ensure they are registered
from shared.database import Base
from services.user_service.models import User
from services.emergency_service.models import EmergencyAlert, EmergencyResponse
from services.location_service.models import UserLocation, LocationHistory
from services.calendar_service.models import CalendarEntry, CycleData, HealthInsights
# 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
# for 'autogenerate' support
target_metadata = Base.metadata
# other values from the config, defined by the needs of env.py,
# can be acquired:
# my_important_option = config.get_main_option("my_important_option")
# ... etc.
def run_migrations_offline() -> None:
"""Run migrations in 'offline' mode.
This configures the context with just a URL
and not an Engine, though an Engine is acceptable
here as well. By skipping the Engine creation
we don't even need a DBAPI to be available.
Calls to context.execute() here emit the given string to the
script output.
"""
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 do_run_migrations(connection):
context.configure(connection=connection, target_metadata=target_metadata)
with context.begin_transaction():
context.run_migrations()
async def run_migrations_online() -> None:
"""Run migrations in 'online' mode.
In this scenario we need to create an Engine
and associate a connection with the context.
"""
connectable = AsyncEngine(
engine_from_config(
config.get_section(config.config_ini_section),
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)
)
async with connectable.connect() as connection:
await connection.run_sync(do_run_migrations)
await connectable.dispose()
if context.is_offline_mode():
run_migrations_offline()
else:
asyncio.run(run_migrations_online())

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

@@ -0,0 +1,24 @@
"""${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 identifiers, used by Alembic.
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"}

View File

@@ -0,0 +1,235 @@
"""Initial migration with all models
Revision ID: 050c22851c2d
Revises:
Create Date: 2025-09-25 06:56:09.204691
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '050c22851c2d'
down_revision = None
branch_labels = None
depends_on = None
def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('users',
sa.Column('uuid', sa.UUID(), nullable=True),
sa.Column('email', sa.String(), nullable=False),
sa.Column('phone', sa.String(), nullable=True),
sa.Column('password_hash', sa.String(), nullable=False),
sa.Column('first_name', sa.String(length=50), nullable=False),
sa.Column('last_name', sa.String(length=50), nullable=False),
sa.Column('date_of_birth', sa.Date(), nullable=True),
sa.Column('avatar_url', sa.String(), nullable=True),
sa.Column('bio', sa.Text(), nullable=True),
sa.Column('emergency_contact_1_name', sa.String(length=100), nullable=True),
sa.Column('emergency_contact_1_phone', sa.String(length=20), nullable=True),
sa.Column('emergency_contact_2_name', sa.String(length=100), nullable=True),
sa.Column('emergency_contact_2_phone', sa.String(length=20), nullable=True),
sa.Column('location_sharing_enabled', sa.Boolean(), nullable=True),
sa.Column('emergency_notifications_enabled', sa.Boolean(), nullable=True),
sa.Column('push_notifications_enabled', sa.Boolean(), nullable=True),
sa.Column('email_verified', sa.Boolean(), nullable=True),
sa.Column('phone_verified', sa.Boolean(), nullable=True),
sa.Column('is_blocked', sa.Boolean(), nullable=True),
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True),
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('is_active', sa.Boolean(), nullable=True),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_users_email'), 'users', ['email'], unique=True)
op.create_index(op.f('ix_users_id'), 'users', ['id'], unique=False)
op.create_index(op.f('ix_users_phone'), 'users', ['phone'], unique=True)
op.create_index(op.f('ix_users_uuid'), 'users', ['uuid'], unique=True)
op.create_table('calendar_entries',
sa.Column('uuid', sa.UUID(), nullable=True),
sa.Column('user_id', sa.Integer(), nullable=False),
sa.Column('entry_date', sa.Date(), nullable=False),
sa.Column('entry_type', sa.String(length=50), nullable=False),
sa.Column('flow_intensity', sa.String(length=20), nullable=True),
sa.Column('period_symptoms', sa.Text(), nullable=True),
sa.Column('mood', sa.String(length=20), nullable=True),
sa.Column('energy_level', sa.Integer(), nullable=True),
sa.Column('sleep_hours', sa.Integer(), nullable=True),
sa.Column('symptoms', sa.Text(), nullable=True),
sa.Column('medications', sa.Text(), nullable=True),
sa.Column('notes', sa.Text(), nullable=True),
sa.Column('is_predicted', sa.Boolean(), nullable=True),
sa.Column('confidence_score', sa.Integer(), nullable=True),
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True),
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('is_active', sa.Boolean(), nullable=True),
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_calendar_entries_entry_date'), 'calendar_entries', ['entry_date'], unique=False)
op.create_index(op.f('ix_calendar_entries_id'), 'calendar_entries', ['id'], unique=False)
op.create_index(op.f('ix_calendar_entries_user_id'), 'calendar_entries', ['user_id'], unique=False)
op.create_index(op.f('ix_calendar_entries_uuid'), 'calendar_entries', ['uuid'], unique=True)
op.create_table('cycle_data',
sa.Column('user_id', sa.Integer(), nullable=False),
sa.Column('cycle_start_date', sa.Date(), nullable=False),
sa.Column('cycle_length', sa.Integer(), nullable=True),
sa.Column('period_length', sa.Integer(), nullable=True),
sa.Column('ovulation_date', sa.Date(), nullable=True),
sa.Column('fertile_window_start', sa.Date(), nullable=True),
sa.Column('fertile_window_end', sa.Date(), nullable=True),
sa.Column('next_period_predicted', sa.Date(), nullable=True),
sa.Column('cycle_regularity_score', sa.Integer(), nullable=True),
sa.Column('avg_cycle_length', sa.Integer(), nullable=True),
sa.Column('avg_period_length', sa.Integer(), nullable=True),
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True),
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('is_active', sa.Boolean(), nullable=True),
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_cycle_data_id'), 'cycle_data', ['id'], unique=False)
op.create_index(op.f('ix_cycle_data_user_id'), 'cycle_data', ['user_id'], unique=False)
op.create_table('emergency_alerts',
sa.Column('uuid', sa.UUID(), nullable=True),
sa.Column('user_id', sa.Integer(), nullable=False),
sa.Column('latitude', sa.Float(), nullable=False),
sa.Column('longitude', sa.Float(), nullable=False),
sa.Column('address', sa.String(length=500), nullable=True),
sa.Column('alert_type', sa.String(length=50), nullable=True),
sa.Column('message', sa.Text(), nullable=True),
sa.Column('is_resolved', sa.Boolean(), nullable=True),
sa.Column('resolved_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('resolved_by', sa.Integer(), nullable=True),
sa.Column('notified_users_count', sa.Integer(), nullable=True),
sa.Column('responded_users_count', sa.Integer(), nullable=True),
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True),
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('is_active', sa.Boolean(), nullable=True),
sa.ForeignKeyConstraint(['resolved_by'], ['users.id'], ),
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_emergency_alerts_id'), 'emergency_alerts', ['id'], unique=False)
op.create_index(op.f('ix_emergency_alerts_user_id'), 'emergency_alerts', ['user_id'], unique=False)
op.create_index(op.f('ix_emergency_alerts_uuid'), 'emergency_alerts', ['uuid'], unique=True)
op.create_table('health_insights',
sa.Column('user_id', sa.Integer(), nullable=False),
sa.Column('insight_type', sa.String(length=50), nullable=False),
sa.Column('title', sa.String(length=200), nullable=False),
sa.Column('description', sa.Text(), nullable=False),
sa.Column('recommendation', sa.Text(), nullable=True),
sa.Column('confidence_level', sa.String(length=20), nullable=True),
sa.Column('data_points_used', sa.Integer(), nullable=True),
sa.Column('is_dismissed', sa.Boolean(), nullable=True),
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True),
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('is_active', sa.Boolean(), nullable=True),
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_health_insights_id'), 'health_insights', ['id'], unique=False)
op.create_index(op.f('ix_health_insights_user_id'), 'health_insights', ['user_id'], unique=False)
op.create_table('location_history',
sa.Column('user_id', sa.Integer(), nullable=False),
sa.Column('latitude', sa.Float(), nullable=False),
sa.Column('longitude', sa.Float(), nullable=False),
sa.Column('accuracy', sa.Float(), nullable=True),
sa.Column('recorded_at', sa.DateTime(timezone=True), nullable=False),
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True),
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('is_active', sa.Boolean(), nullable=True),
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_index('idx_history_coords_date', 'location_history', ['latitude', 'longitude', 'recorded_at'], unique=False)
op.create_index('idx_history_user_date', 'location_history', ['user_id', 'recorded_at'], unique=False)
op.create_index(op.f('ix_location_history_id'), 'location_history', ['id'], unique=False)
op.create_index(op.f('ix_location_history_user_id'), 'location_history', ['user_id'], unique=False)
op.create_table('user_locations',
sa.Column('uuid', sa.UUID(), nullable=True),
sa.Column('user_id', sa.Integer(), nullable=False),
sa.Column('latitude', sa.Float(), nullable=False),
sa.Column('longitude', sa.Float(), nullable=False),
sa.Column('accuracy', sa.Float(), nullable=True),
sa.Column('altitude', sa.Float(), nullable=True),
sa.Column('speed', sa.Float(), nullable=True),
sa.Column('heading', sa.Float(), nullable=True),
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True),
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('is_active', sa.Boolean(), nullable=True),
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_index('idx_location_coords', 'user_locations', ['latitude', 'longitude'], unique=False)
op.create_index('idx_location_user_time', 'user_locations', ['user_id', 'created_at'], unique=False)
op.create_index(op.f('ix_user_locations_id'), 'user_locations', ['id'], unique=False)
op.create_index(op.f('ix_user_locations_user_id'), 'user_locations', ['user_id'], unique=False)
op.create_index(op.f('ix_user_locations_uuid'), 'user_locations', ['uuid'], unique=True)
op.create_table('emergency_responses',
sa.Column('alert_id', sa.Integer(), nullable=False),
sa.Column('responder_id', sa.Integer(), nullable=False),
sa.Column('response_type', sa.String(length=50), nullable=True),
sa.Column('message', sa.Text(), nullable=True),
sa.Column('eta_minutes', sa.Integer(), nullable=True),
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True),
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('is_active', sa.Boolean(), nullable=True),
sa.ForeignKeyConstraint(['alert_id'], ['emergency_alerts.id'], ),
sa.ForeignKeyConstraint(['responder_id'], ['users.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_emergency_responses_alert_id'), 'emergency_responses', ['alert_id'], unique=False)
op.create_index(op.f('ix_emergency_responses_id'), 'emergency_responses', ['id'], unique=False)
op.create_index(op.f('ix_emergency_responses_responder_id'), 'emergency_responses', ['responder_id'], unique=False)
# ### end Alembic commands ###
def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index(op.f('ix_emergency_responses_responder_id'), table_name='emergency_responses')
op.drop_index(op.f('ix_emergency_responses_id'), table_name='emergency_responses')
op.drop_index(op.f('ix_emergency_responses_alert_id'), table_name='emergency_responses')
op.drop_table('emergency_responses')
op.drop_index(op.f('ix_user_locations_uuid'), table_name='user_locations')
op.drop_index(op.f('ix_user_locations_user_id'), table_name='user_locations')
op.drop_index(op.f('ix_user_locations_id'), table_name='user_locations')
op.drop_index('idx_location_user_time', table_name='user_locations')
op.drop_index('idx_location_coords', table_name='user_locations')
op.drop_table('user_locations')
op.drop_index(op.f('ix_location_history_user_id'), table_name='location_history')
op.drop_index(op.f('ix_location_history_id'), table_name='location_history')
op.drop_index('idx_history_user_date', table_name='location_history')
op.drop_index('idx_history_coords_date', table_name='location_history')
op.drop_table('location_history')
op.drop_index(op.f('ix_health_insights_user_id'), table_name='health_insights')
op.drop_index(op.f('ix_health_insights_id'), table_name='health_insights')
op.drop_table('health_insights')
op.drop_index(op.f('ix_emergency_alerts_uuid'), table_name='emergency_alerts')
op.drop_index(op.f('ix_emergency_alerts_user_id'), table_name='emergency_alerts')
op.drop_index(op.f('ix_emergency_alerts_id'), table_name='emergency_alerts')
op.drop_table('emergency_alerts')
op.drop_index(op.f('ix_cycle_data_user_id'), table_name='cycle_data')
op.drop_index(op.f('ix_cycle_data_id'), table_name='cycle_data')
op.drop_table('cycle_data')
op.drop_index(op.f('ix_calendar_entries_uuid'), table_name='calendar_entries')
op.drop_index(op.f('ix_calendar_entries_user_id'), table_name='calendar_entries')
op.drop_index(op.f('ix_calendar_entries_id'), table_name='calendar_entries')
op.drop_index(op.f('ix_calendar_entries_entry_date'), table_name='calendar_entries')
op.drop_table('calendar_entries')
op.drop_index(op.f('ix_users_uuid'), table_name='users')
op.drop_index(op.f('ix_users_phone'), table_name='users')
op.drop_index(op.f('ix_users_id'), table_name='users')
op.drop_index(op.f('ix_users_email'), table_name='users')
op.drop_table('users')
# ### end Alembic commands ###