Prepare monorepo

This commit is contained in:
Oliver Traber 2025-03-13 22:11:20 +01:00
parent a1ddb43ed0
commit 938582155d
Signed by: Bluemedia
GPG key ID: C0674B105057136C
61 changed files with 5 additions and 5 deletions

62
backend/alembic/env.py Normal file
View file

@ -0,0 +1,62 @@
from logging.config import fileConfig
import os
from dotenv import load_dotenv
from sqlalchemy import create_engine
from alembic import context
load_dotenv()
# Import models for autogenerate support
from app.database import Base
from app.models import *
# 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
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
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_online() -> None:
"""Run migrations in 'online' mode.
In this scenario we need to create an Engine
and associate a connection with the context.
"""
SQLALCHEMY_DATABASE_URL = os.getenv("CS_DATABASE_URL", "sqlite:///./simple-ocpp-cs.db")
if SQLALCHEMY_DATABASE_URL.startswith("sqlite"):
connectable = create_engine(
SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}
)
else:
connectable = create_engine(SQLALCHEMY_DATABASE_URL)
with connectable.connect() as connection:
context.configure(
connection=connection, target_metadata=target_metadata
)
with context.begin_transaction():
context.run_migrations()
if context.is_offline_mode():
raise NotImplementedError("Offline migration is not supported")
else:
run_migrations_online()

View file

@ -0,0 +1,26 @@
"""${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, 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:
${upgrades if upgrades else "pass"}
def downgrade() -> None:
${downgrades if downgrades else "pass"}

View file

@ -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 ###

View file

@ -0,0 +1,59 @@
"""Add user authentication
Revision ID: c7f72154c90b
Revises: 097d427dfa07
Create Date: 2025-03-13 14:57:05.805469+00:00
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = 'c7f72154c90b'
down_revision: Union[str, None] = '097d427dfa07'
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('sessions',
sa.Column('id', sa.Uuid(), nullable=False),
sa.Column('name', sa.String(), nullable=True),
sa.Column('refresh_token', sa.String(), nullable=False),
sa.Column('last_used', sa.DateTime(timezone=True), nullable=True),
sa.Column('user_id', sa.Uuid(), nullable=False),
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_sessions_refresh_token'), 'sessions', ['refresh_token'], unique=True)
op.create_index(op.f('ix_sessions_user_id'), 'sessions', ['user_id'], unique=False)
op.add_column('users', sa.Column('email', sa.String(), nullable=True))
op.add_column('users', sa.Column('password', sa.String(), nullable=True))
op.add_column('users', sa.Column('role', sa.Enum('MEMBER', 'ADMINISTRATOR', name='role'), nullable=True))
op.execute('UPDATE users SET email = id || \'@example.com\'')
op.execute('UPDATE users SET password = \'invalid\'')
op.execute('UPDATE users SET role = \'MEMBER\'')
with op.batch_alter_table('users', schema=None) as batch_op:
batch_op.alter_column('email', nullable=False)
batch_op.alter_column('password', nullable=False)
batch_op.alter_column('role', nullable=False)
op.drop_index('ix_users_friendly_name', table_name='users')
op.create_index(op.f('ix_users_email'), 'users', ['email'], unique=True)
# ### end Alembic commands ###
def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index(op.f('ix_users_email'), table_name='users')
op.create_index('ix_users_friendly_name', 'users', ['friendly_name'], unique=1)
op.drop_column('users', 'role')
op.drop_column('users', 'password')
op.drop_column('users', 'email')
op.drop_index(op.f('ix_sessions_user_id'), table_name='sessions')
op.drop_index(op.f('ix_sessions_refresh_token'), table_name='sessions')
op.drop_table('sessions')
# ### end Alembic commands ###