From cf4d3788bdd9ffabb89fd4f9443ca97a59e42e92 Mon Sep 17 00:00:00 2001 From: BluemediaGER Date: Sat, 20 Apr 2024 13:33:42 +0200 Subject: [PATCH] Use alembic for migrations --- Dockerfile | 3 +- ...20240420_097d427dfa07-initial_migration.py | 146 ++++++++++++++++++ app/main.py | 5 - start.sh | 5 + 4 files changed, 153 insertions(+), 6 deletions(-) create mode 100644 alembic/versions/20240420_097d427dfa07-initial_migration.py create mode 100755 start.sh diff --git a/Dockerfile b/Dockerfile index 1510179..e914d2f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -55,7 +55,8 @@ FROM python-base as production ENV FASTAPI_ENV=production COPY --from=builder-base $PYSETUP_PATH $PYSETUP_PATH COPY alembic.ini /usr/src/ +COPY start.sh /usr/src/ COPY ./alembic /usr/src/alembic COPY ./app /usr/src/app WORKDIR /usr/src -CMD ["gunicorn", "-k", "uvicorn.workers.UvicornWorker", "--bind", "0.0.0.0:8000", "app.main:app"] \ No newline at end of file +CMD ["bash", "start.sh"] \ No newline at end of file diff --git a/alembic/versions/20240420_097d427dfa07-initial_migration.py b/alembic/versions/20240420_097d427dfa07-initial_migration.py new file mode 100644 index 0000000..bccb254 --- /dev/null +++ b/alembic/versions/20240420_097d427dfa07-initial_migration.py @@ -0,0 +1,146 @@ +"""Initial migration + +Revision ID: 097d427dfa07 +Revises: +Create Date: 2024-04-20 11:30:32.425878+00:00 + +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision: str = '097d427dfa07' +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: + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('users', + sa.Column('id', sa.Uuid(), nullable=False), + sa.Column('friendly_name', sa.String(), nullable=True), + sa.Column('is_active', sa.Boolean(), nullable=True), + sa.PrimaryKeyConstraint('id') + ) + op.create_index(op.f('ix_users_friendly_name'), 'users', ['friendly_name'], unique=True) + op.create_table('chargepoints', + sa.Column('id', sa.Uuid(), nullable=False), + sa.Column('identity', sa.String(), nullable=True), + sa.Column('is_active', sa.Boolean(), nullable=True), + sa.Column('password', sa.String(), nullable=True), + sa.Column('price', sa.Numeric(precision=10, scale=2), nullable=True), + sa.Column('last_seen', sa.DateTime(), nullable=True), + sa.Column('vendor_name', sa.String(), nullable=True), + sa.Column('model', sa.String(), nullable=True), + sa.Column('serial_number', sa.String(), nullable=True), + sa.Column('firmware_version', sa.String(), nullable=True), + sa.Column('learn_user_id', sa.Uuid(), nullable=True), + sa.Column('learn_until', sa.DateTime(), nullable=True), + sa.ForeignKeyConstraint(['learn_user_id'], ['users.id'], ), + sa.PrimaryKeyConstraint('id') + ) + op.create_index(op.f('ix_chargepoints_identity'), 'chargepoints', ['identity'], unique=True) + op.create_table('id_tokens', + sa.Column('id', sa.Uuid(), nullable=False), + sa.Column('friendly_name', sa.String(), nullable=True), + sa.Column('is_active', sa.Boolean(), nullable=True), + sa.Column('token', sa.String(), nullable=True), + sa.Column('owner_id', sa.Uuid(), nullable=True), + sa.ForeignKeyConstraint(['owner_id'], ['users.id'], ), + sa.PrimaryKeyConstraint('id') + ) + op.create_index(op.f('ix_id_tokens_token'), 'id_tokens', ['token'], unique=False) + op.create_table('chargepoint_variables', + sa.Column('id', sa.Uuid(), nullable=False), + sa.Column('name', sa.String(), nullable=True), + sa.Column('type', sa.Enum('ACTUAL', 'TARGET', 'MIN_SET', 'MAX_SET', name='attributetype'), nullable=True), + sa.Column('value', sa.String(), nullable=True), + sa.Column('mutability', sa.Enum('READ_ONLY', 'WRITE_ONLY', 'READ_WRITE', name='mutabilitytype'), nullable=True), + sa.Column('persistent', sa.Boolean(), nullable=True), + sa.Column('constant', sa.Boolean(), nullable=True), + sa.Column('unit', sa.String(), nullable=True), + sa.Column('data_type', sa.Enum('STRING', 'DECIMAL', 'INTEGER', 'DATETIME', 'BOOLEAN', 'OPTION_LIST', 'SEQUENCE_LIST', 'MEMBER_LIST', name='datatype'), nullable=True), + sa.Column('min_limit', sa.Numeric(), nullable=True), + sa.Column('max_limit', sa.Numeric(), nullable=True), + sa.Column('values_list', sa.String(), nullable=True), + sa.Column('component_name', sa.String(), nullable=True), + sa.Column('component_instance', sa.String(), nullable=True), + sa.Column('evse', sa.Integer(), nullable=True), + sa.Column('connector_id', sa.Integer(), nullable=True), + sa.Column('chargepoint_id', sa.Uuid(), nullable=True), + sa.ForeignKeyConstraint(['chargepoint_id'], ['chargepoints.id'], ), + sa.PrimaryKeyConstraint('id') + ) + op.create_index(op.f('ix_chargepoint_variables_chargepoint_id'), 'chargepoint_variables', ['chargepoint_id'], unique=False) + op.create_table('connectors', + sa.Column('id', sa.Uuid(), nullable=False), + sa.Column('evse', sa.Integer(), nullable=True), + sa.Column('index', sa.Integer(), nullable=True), + sa.Column('status', sa.Enum('AVAILABLE', 'OCCUPIED', 'RESERVED', 'UNAVAILABLE', 'FAULTED', name='connectorstatus'), nullable=True), + sa.Column('chargepoint_id', sa.Uuid(), nullable=True), + sa.ForeignKeyConstraint(['chargepoint_id'], ['chargepoints.id'], ), + sa.PrimaryKeyConstraint('id') + ) + op.create_table('transactions', + sa.Column('id', sa.String(), nullable=False), + sa.Column('status', sa.Enum('ONGOING', 'ENDED', name='transactionstatus'), nullable=True), + sa.Column('started_at', sa.DateTime(), nullable=True), + sa.Column('ended_at', sa.DateTime(), nullable=True), + sa.Column('meter_start', sa.Numeric(precision=10, scale=2), nullable=True), + sa.Column('meter_end', sa.Numeric(precision=10, scale=2), nullable=True), + sa.Column('end_reason', sa.Enum('AUTHORIZED', 'CABLE_PLUGGED_IN', 'CHARGING_RATE_CHANGED', 'CHARGING_STATE_CHANGED', 'DEAUTHORIZED', 'ENERGY_LIMIT_REACHED', 'EV_COMMUNICATION_LOST', 'EV_CONNECT_TIMEOUT', 'METER_VALUE_CLOCK', 'METER_VALUE_PERIODIC', 'TIME_LIMIT_REACHED', 'TRIGGER', 'UNLOCK_COMMAND', 'STOP_AUTHORIZED', 'EV_DEPARTED', 'EV_DETECTED', 'REMOTE_STOP', 'REMOTE_START', 'ABNORMAL_CONDITION', 'SIGNED_DATA_RECEIVED', 'RESET_COMMAND', name='transactioneventtriggerreason'), nullable=True), + sa.Column('price', sa.Numeric(precision=10, scale=2), nullable=True), + sa.Column('user_id', sa.Uuid(), nullable=True), + sa.Column('chargepoint_id', sa.Uuid(), nullable=True), + sa.ForeignKeyConstraint(['chargepoint_id'], ['chargepoints.id'], ), + sa.ForeignKeyConstraint(['user_id'], ['users.id'], ), + sa.PrimaryKeyConstraint('id') + ) + op.create_index(op.f('ix_transactions_chargepoint_id'), 'transactions', ['chargepoint_id'], unique=False) + op.create_index(op.f('ix_transactions_ended_at'), 'transactions', ['ended_at'], unique=False) + op.create_index(op.f('ix_transactions_started_at'), 'transactions', ['started_at'], unique=False) + op.create_index(op.f('ix_transactions_status'), 'transactions', ['status'], unique=False) + op.create_index(op.f('ix_transactions_user_id'), 'transactions', ['user_id'], unique=False) + op.create_table('meter_values', + sa.Column('id', sa.Uuid(), nullable=False), + sa.Column('timestamp', sa.DateTime(), nullable=True), + sa.Column('measurand', sa.Enum('CURRENT_EXPORT', 'CURRENT_IMPORT', 'CURRENT_OFFERED', 'ENERGY_ACTIVE_NET', 'ENERGY_ACTIVE_EXPORT_REGISTER', 'ENERGY_ACTIVE_IMPORT_REGISTER', 'ENERGY_ACTIVE_EXPORT_INTERVAL', 'ENERGY_ACTIVE_IMPORT_INTERVAL', 'ENERGY_REACTIVE_NET', 'ENERGY_REACTIVE_EXPORT_REGISTER', 'ENERGY_REACTIVE_IMPORT_REGISTER', 'ENERGY_REACTIVE_EXPORT_INTERVAL', 'ENERGY_REACTIVE_IMPORT_INTERVAL', 'ENERGY_APPARENT_NET', 'ENERGY_APPARENT_IMPORT', 'ENERGY_APPARENT_EXPORT', 'FREQUENCY', 'POWER_ACTIVE_EXPORT', 'POWER_ACTIVE_IMPORT', 'POWER_FACTOR', 'POWER_OFFERED', 'POWER_REACTIVE_EXPORT', 'POWER_REACTIVE_IMPORT', 'SOC', 'VOLTAGE', name='measurand'), nullable=True), + sa.Column('phase_type', sa.Enum('L1', 'L2', 'L3', 'N', 'L1_N', 'L2_N', 'L3_N', 'L1_L2', 'L2_L3', 'L3_L1', name='phasetype'), nullable=True), + sa.Column('unit', sa.String(), nullable=True), + sa.Column('value', sa.Float(), nullable=True), + sa.Column('transaction_id', sa.String(), nullable=True), + sa.ForeignKeyConstraint(['transaction_id'], ['transactions.id'], ), + sa.PrimaryKeyConstraint('id') + ) + op.create_index(op.f('ix_meter_values_measurand'), 'meter_values', ['measurand'], unique=False) + op.create_index(op.f('ix_meter_values_timestamp'), 'meter_values', ['timestamp'], unique=False) + op.create_index(op.f('ix_meter_values_transaction_id'), 'meter_values', ['transaction_id'], unique=False) + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.drop_index(op.f('ix_meter_values_transaction_id'), table_name='meter_values') + op.drop_index(op.f('ix_meter_values_timestamp'), table_name='meter_values') + op.drop_index(op.f('ix_meter_values_measurand'), table_name='meter_values') + op.drop_table('meter_values') + op.drop_index(op.f('ix_transactions_user_id'), table_name='transactions') + op.drop_index(op.f('ix_transactions_status'), table_name='transactions') + op.drop_index(op.f('ix_transactions_started_at'), table_name='transactions') + op.drop_index(op.f('ix_transactions_ended_at'), table_name='transactions') + op.drop_index(op.f('ix_transactions_chargepoint_id'), table_name='transactions') + op.drop_table('transactions') + op.drop_table('connectors') + op.drop_index(op.f('ix_chargepoint_variables_chargepoint_id'), table_name='chargepoint_variables') + op.drop_table('chargepoint_variables') + op.drop_index(op.f('ix_id_tokens_token'), table_name='id_tokens') + op.drop_table('id_tokens') + op.drop_index(op.f('ix_chargepoints_identity'), table_name='chargepoints') + op.drop_table('chargepoints') + op.drop_index(op.f('ix_users_friendly_name'), table_name='users') + op.drop_table('users') + # ### end Alembic commands ### diff --git a/app/main.py b/app/main.py index c4cfbce..a2bb98a 100644 --- a/app/main.py +++ b/app/main.py @@ -4,9 +4,6 @@ from starlette.middleware.authentication import AuthenticationMiddleware load_dotenv() -from app.database import engine, Base -from app.models import * - from app.routers import ( chargepoint_v1, id_token_v1, @@ -17,8 +14,6 @@ from app.routers import ( ) from app.util.websocket_auth_backend import BasicAuthBackend -Base.metadata.create_all(bind=engine) - def create_ocpp_app(): app_ocpp = FastAPI( responses={404: {"description": "Not found"}}, diff --git a/start.sh b/start.sh new file mode 100755 index 0000000..d173339 --- /dev/null +++ b/start.sh @@ -0,0 +1,5 @@ +#!/bin/bash +set -e + +alembic upgrade head +gunicorn -k "uvicorn.workers.UvicornWorker" --bind "0.0.0.0:8000" app.main:app \ No newline at end of file