Files
sibuti/transport/backend/alembic/versions/001_initial.py

76 lines
2.6 KiB
Python
Raw Normal View History

2025-12-18 21:13:49 +03:00
"""Initial migration
Revision ID: 001
Revises:
Create Date: 2025-12-18
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
# revision identifiers, used by Alembic.
revision: str = '001'
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
# Create vehicles table
op.create_table(
'vehicles',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(100), nullable=False),
sa.Column('type', sa.String(50), nullable=True, default='car'),
sa.Column('created_at', sa.DateTime(), nullable=True),
sa.PrimaryKeyConstraint('id')
)
# Create positions table
op.create_table(
'positions',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('vehicle_id', sa.Integer(), nullable=False),
sa.Column('timestamp', sa.DateTime(), nullable=False),
sa.Column('lat', sa.Float(), nullable=False),
sa.Column('lon', sa.Float(), nullable=False),
sa.Column('speed', sa.Float(), nullable=True, default=0.0),
sa.Column('heading', sa.Float(), nullable=True, default=0.0),
sa.ForeignKeyConstraint(['vehicle_id'], ['vehicles.id'], ondelete='CASCADE'),
sa.PrimaryKeyConstraint('id')
)
op.create_index('idx_positions_vehicle_ts', 'positions', ['vehicle_id', 'timestamp'])
# Create events table
op.create_table(
'events',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('vehicle_id', sa.Integer(), nullable=False),
sa.Column('timestamp', sa.DateTime(), nullable=False),
sa.Column('type', sa.String(50), nullable=False),
sa.Column('payload', postgresql.JSONB(), nullable=True, default={}),
sa.ForeignKeyConstraint(['vehicle_id'], ['vehicles.id'], ondelete='CASCADE'),
sa.PrimaryKeyConstraint('id')
)
op.create_index('idx_events_vehicle_ts', 'events', ['vehicle_id', 'timestamp'])
op.create_index('idx_events_type', 'events', ['type'])
# Insert demo vehicles
op.execute("""
INSERT INTO vehicles (name, type, created_at) VALUES
('Автобус А-101', 'bus', NOW()),
('Автобус А-102', 'bus', NOW()),
('Грузовик Г-201', 'truck', NOW()),
('Легковой Л-301', 'car', NOW()),
('Легковой Л-302', 'car', NOW())
""")
def downgrade() -> None:
op.drop_table('events')
op.drop_table('positions')
op.drop_table('vehicles')