From a1ab4f287407af316f0bc362334f362e0549934b Mon Sep 17 00:00:00 2001 From: Alexandra Ioan Date: Tue, 10 Feb 2026 15:52:43 +0100 Subject: [PATCH 1/7] add alembic migration --- alembic.ini | 152 ++++++++++++++++++ alembic/env.py | 67 ++++++++ alembic/script.py.mako | 30 ++++ .../c84b85ae4bed_initial_migration.py | 92 +++++++++++ dev_requirements.txt | 1 + requirements.txt | 1 + simdb.db | Bin 0 -> 53248 bytes 7 files changed, 343 insertions(+) create mode 100644 alembic.ini create mode 100644 alembic/env.py create mode 100644 alembic/script.py.mako create mode 100644 alembic/versions/c84b85ae4bed_initial_migration.py create mode 100644 simdb.db diff --git a/alembic.ini b/alembic.ini new file mode 100644 index 0000000..5aca437 --- /dev/null +++ b/alembic.ini @@ -0,0 +1,152 @@ +# A generic, single database configuration. + +[alembic] +# path to migration scripts. +# this is typically a path given in POSIX (e.g. forward slashes) +# format, relative to the token %(here)s which refers to the location of this +# ini file +script_location = %(here)s/alembic + +# template used to generate migration file names; The default value is %%(rev)s_%%(slug)s +# Uncomment the line below if you want the files to be prepended with date and time +# see https://alembic.sqlalchemy.org/en/latest/tutorial.html#editing-the-ini-file +# for all available tokens +# file_template = %%(year)d_%%(month).2d_%%(day).2d_%%(hour).2d%%(minute).2d-%%(rev)s_%%(slug)s +# Or organize into date-based subdirectories (requires recursive_version_locations = true) +# file_template = %%(year)d/%%(month).2d/%%(day).2d_%%(hour).2d%%(minute).2d_%%(second).2d_%%(rev)s_%%(slug)s + +# sys.path path, will be prepended to sys.path if present. +# defaults to the current working directory. for multiple paths, the path separator +# is defined by "path_separator" below. +prepend_sys_path = . + + +# timezone to use when rendering the date within the migration file +# as well as the filename. +# If specified, requires the tzdata library which can be installed by adding +# `alembic[tz]` to the pip requirements. +# string value is passed to ZoneInfo() +# leave blank for localtime +# timezone = + +# max length of characters to apply to the "slug" field +# truncate_slug_length = 40 + +# set to 'true' to run the environment during +# the 'revision' command, regardless of autogenerate +# revision_environment = false + +# set to 'true' to allow .pyc and .pyo files without +# a source .py file to be detected as revisions in the +# versions/ directory +# sourceless = false + +# version location specification; This defaults +# to /versions. When using multiple version +# directories, initial revisions must be specified with --version-path. +# The path separator used here should be the separator specified by "path_separator" +# below. +# version_locations = %(here)s/bar:%(here)s/bat:%(here)s/alembic/versions + +# path_separator; This indicates what character is used to split lists of file +# paths, including version_locations and prepend_sys_path within configparser +# files such as alembic.ini. +# The default rendered in new alembic.ini files is "os", which uses os.pathsep +# to provide os-dependent path splitting. +# +# Note that in order to support legacy alembic.ini files, this default does NOT +# take place if path_separator is not present in alembic.ini. If this +# option is omitted entirely, fallback logic is as follows: +# +# 1. Parsing of the version_locations option falls back to using the legacy +# "version_path_separator" key, which if absent then falls back to the legacy +# behavior of splitting on spaces and/or commas. +# 2. Parsing of the prepend_sys_path option falls back to the legacy +# behavior of splitting on spaces, commas, or colons. +# +# Valid values for path_separator are: +# +# path_separator = : +# path_separator = ; +# path_separator = space +# path_separator = newline +# +# Use os.pathsep. Default configuration used for new projects. +path_separator = os + +# set to 'true' to search source files recursively +# in each "version_locations" directory +# new in Alembic version 1.10 +# recursive_version_locations = false + +# the output encoding used when revision files +# are written from script.py.mako +# output_encoding = utf-8 + +# database URL. This is consumed by the user-maintained env.py script only. +# other means of configuring database URLs may be customized within the env.py +# file. +# NOTE: The actual database URL should be set via the DATABASE_URL environment variable +# or it can be set here for development purposes +# Example: postgresql+psycopg2://user:password@localhost/dbname +sqlalchemy.url = sqlite:///simdb.db + + +[post_write_hooks] +# post_write_hooks defines scripts or Python functions that are run +# on newly generated revision scripts. See the documentation for further +# detail and examples + +# format using "black" - use the console_scripts runner, against the "black" entrypoint +# hooks = black +# black.type = console_scripts +# black.entrypoint = black +# black.options = -l 79 REVISION_SCRIPT_FILENAME + +# lint with attempts to fix using "ruff" - use the module runner, against the "ruff" module +# hooks = ruff +# ruff.type = module +# ruff.module = ruff +# ruff.options = check --fix REVISION_SCRIPT_FILENAME + +# Alternatively, use the exec runner to execute a binary found on your PATH +# hooks = ruff +# ruff.type = exec +# ruff.executable = ruff +# ruff.options = check --fix REVISION_SCRIPT_FILENAME + +# Logging configuration. This is also consumed by the user-maintained +# env.py script only. +[loggers] +keys = root,sqlalchemy,alembic + +[handlers] +keys = console + +[formatters] +keys = generic + +[logger_root] +level = WARNING +handlers = console +qualname = + +[logger_sqlalchemy] +level = WARNING +handlers = +qualname = sqlalchemy.engine + +[logger_alembic] +level = INFO +handlers = +qualname = alembic + +[handler_console] +class = StreamHandler +args = (sys.stderr,) +level = NOTSET +formatter = generic + +[formatter_generic] +format = %(levelname)-5.5s [%(name)s] %(message)s +datefmt = %H:%M:%S diff --git a/alembic/env.py b/alembic/env.py new file mode 100644 index 0000000..ccfed6f --- /dev/null +++ b/alembic/env.py @@ -0,0 +1,67 @@ +from logging.config import fileConfig +import os +import sys +from pathlib import Path + +from alembic import context +from sqlalchemy import create_engine, pool + +config = context.config + +if config.config_file_name: + fileConfig(config.config_file_name) + +SRC_PATH = Path(__file__).resolve().parents[1] / "src" +sys.path.insert(0, str(SRC_PATH)) + +from simdb.database.models import Base # noqa +from simdb.database.models import ( # noqa + file, + metadata, + simulation, + watcher, +) +from simdb.database.models import types # noqa + +target_metadata = Base.metadata + +def get_database_url() -> str: + url = os.getenv("DATABASE_URL") + if not url: + raise RuntimeError("DATABASE_URL is not set") + return url + +def run_migrations_offline() -> None: + context.configure( + url=get_database_url(), + target_metadata=target_metadata, + literal_binds=True, + dialect_opts={"paramstyle": "named"}, + compare_type=True, + compare_server_default=True, + ) + + with context.begin_transaction(): + context.run_migrations() + +def run_migrations_online() -> None: + engine = create_engine( + get_database_url(), + poolclass=pool.NullPool, + ) + + with engine.connect() as connection: + context.configure( + connection=connection, + target_metadata=target_metadata, + compare_type=True, + compare_server_default=True, + ) + + with context.begin_transaction(): + context.run_migrations() + +if context.is_offline_mode(): + run_migrations_offline() +else: + run_migrations_online() diff --git a/alembic/script.py.mako b/alembic/script.py.mako new file mode 100644 index 0000000..c0865ef --- /dev/null +++ b/alembic/script.py.mako @@ -0,0 +1,30 @@ +"""${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 +from simdb.database.models import types + +${imports if imports else ""} + +# revision identifiers, used by Alembic. +revision: str = ${repr(up_revision)} +down_revision: Union[str, Sequence[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: + """Upgrade schema.""" + ${upgrades if upgrades else "pass"} + + +def downgrade() -> None: + """Downgrade schema.""" + ${downgrades if downgrades else "pass"} diff --git a/alembic/versions/c84b85ae4bed_initial_migration.py b/alembic/versions/c84b85ae4bed_initial_migration.py new file mode 100644 index 0000000..d992f3b --- /dev/null +++ b/alembic/versions/c84b85ae4bed_initial_migration.py @@ -0,0 +1,92 @@ +"""initial migration + +Revision ID: c84b85ae4bed +Revises: +Create Date: 2026-02-10 15:24:11.323077 + +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa +from simdb.database.models.types import UUID, ChoiceType +from simdb.notifications import Notification + +# revision identifiers, used by Alembic. +revision: str = 'c84b85ae4bed' +down_revision: Union[str, Sequence[str], None] = None +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + +# Define notification choices +NOTIFICATION_CHOICES = { + Notification.VALIDATION: "V", + Notification.REVISION: "R", + Notification.OBSOLESCENCE: "O", + Notification.ALL: "A", +} + +def upgrade() -> None: + """Upgrade schema.""" + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('simulations', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('uuid', UUID(), nullable=False), + sa.Column('alias', sa.String(length=250), nullable=True), + sa.Column('datetime', sa.DateTime(), nullable=False), + sa.PrimaryKeyConstraint('id') + ) + op.create_index(op.f('ix_simulations_alias'), 'simulations', ['alias'], unique=True) + op.create_index(op.f('ix_simulations_uuid'), 'simulations', ['uuid'], unique=True) + op.create_table('watchers', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('username', sa.String(length=250), nullable=True), + sa.Column('email', sa.String(length=1000), nullable=True), + sa.Column('notification', ChoiceType(choices=NOTIFICATION_CHOICES, length=1, enum_type=Notification), nullable=True), + sa.PrimaryKeyConstraint('id') + ) + op.create_table('metadata', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('sim_id', sa.Integer(), nullable=True), + sa.Column('element', sa.String(length=250), nullable=False), + sa.Column('value', sa.PickleType(), nullable=True), + sa.ForeignKeyConstraint(['sim_id'], ['simulations.id'], ), + sa.PrimaryKeyConstraint('id') + ) + op.create_index(op.f('ix_metadata_sim_id'), 'metadata', ['sim_id'], unique=False) + op.create_index('metadata_index', 'metadata', ['sim_id', 'element'], unique=True) + op.create_table('simulation_input_files', + sa.Column('simulation_id', sa.Integer(), nullable=True), + sa.Column('file_id', sa.Integer(), nullable=True), + sa.ForeignKeyConstraint(['file_id'], ['files.id'], ), + sa.ForeignKeyConstraint(['simulation_id'], ['simulations.id'], ) + ) + op.create_table('simulation_output_files', + sa.Column('simulation_id', sa.Integer(), nullable=True), + sa.Column('file_id', sa.Integer(), nullable=True), + sa.ForeignKeyConstraint(['file_id'], ['files.id'], ), + sa.ForeignKeyConstraint(['simulation_id'], ['simulations.id'], ) + ) + op.create_table('simulation_watchers', + sa.Column('simulation_id', sa.Integer(), nullable=True), + sa.Column('watcher_id', sa.Integer(), nullable=True), + sa.ForeignKeyConstraint(['simulation_id'], ['simulations.id'], ), + sa.ForeignKeyConstraint(['watcher_id'], ['watchers.id'], ) + ) + # ### end Alembic commands ### + + +def downgrade() -> None: + """Downgrade schema.""" + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('simulation_watchers') + op.drop_table('simulation_output_files') + op.drop_table('simulation_input_files') + op.drop_index('metadata_index', table_name='metadata') + op.drop_index(op.f('ix_metadata_sim_id'), table_name='metadata') + op.drop_table('metadata') + op.drop_table('watchers') + op.drop_index(op.f('ix_simulations_uuid'), table_name='simulations') + op.drop_index(op.f('ix_simulations_alias'), table_name='simulations') + op.drop_table('simulations') + # ### end Alembic commands ### diff --git a/dev_requirements.txt b/dev_requirements.txt index 6fc6c7f..031eeb9 100644 --- a/dev_requirements.txt +++ b/dev_requirements.txt @@ -5,6 +5,7 @@ numpy>=1.14.0 python-dateutil>=2.6 PyYAML>=3.13 SQLAlchemy~=1.4 +alembic~=1.13 urllib3~=1.23 requests~=2.27 pytest~=6.0 diff --git a/requirements.txt b/requirements.txt index 36993c4..26bef65 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,6 +5,7 @@ numpy>=1.14.0 python-dateutil>=2.6 PyYAML>=3.13 SQLAlchemy~=1.4 +alembic~=1.13 urllib3~=1.23 requests~=2.27 pytest>=6.0 diff --git a/simdb.db b/simdb.db new file mode 100644 index 0000000000000000000000000000000000000000..8b2bf6941c3c42559345fda3d7e2473f60f949d1 GIT binary patch literal 53248 zcmeI(U2oe|7{GD6?ds-j*`2Q{9aK%k>V(>5MVkf^Y3G$dmQXhZwj0Y$T+LcuMq-a~ zfl#_n!pGoq@FBS6k{cwBow&8#q;QF>ztt-7iOs(&fvtyfC5QvL02xAI5j=ko8R zze~TCJ}Vv)^r`wm%?3RV{I`K`UwD!tF^Xhy zc~$VqgKh-~TfZgx#mA`tq5&<9I5A4;ahsKlds5W&Cr{MNU$Sv~qh(5BGQs}-Iv^C2bA$VC+5+lF=YWy7jHJlHc*Ro*#+z!itB&SBWVH&*jS z!+IH>s;j1?#QC?m`0?gEs*j?m;xjaU)lyN#rME;COx?-I8M>(;!qK`z#~WOg?(gmG zMOTf{*!M2HUUaP!QPlfUc{aGB@%oOuLhh(DEl%$BDL1u)!0RXWHSaB1F-Uf@kxD~q z#4G1l7|L9<{xCg_=EF(OvM@pNbaZ`BX6~81r*xYy%+%DonU9&FYWnW3dRd4D?#NT{ z-0Ruz Date: Tue, 10 Feb 2026 16:13:17 +0100 Subject: [PATCH 2/7] add alembic to pyproject, delete db and adjust migration --- .../c84b85ae4bed_initial_migration.py | 111 ++++++++++-------- pyproject.toml | 1 + simdb.db | Bin 53248 -> 0 bytes 3 files changed, 66 insertions(+), 46 deletions(-) delete mode 100644 simdb.db diff --git a/alembic/versions/c84b85ae4bed_initial_migration.py b/alembic/versions/c84b85ae4bed_initial_migration.py index d992f3b..e0193b1 100644 --- a/alembic/versions/c84b85ae4bed_initial_migration.py +++ b/alembic/versions/c84b85ae4bed_initial_migration.py @@ -28,52 +28,71 @@ def upgrade() -> None: """Upgrade schema.""" - # ### commands auto generated by Alembic - please adjust! ### - op.create_table('simulations', - sa.Column('id', sa.Integer(), nullable=False), - sa.Column('uuid', UUID(), nullable=False), - sa.Column('alias', sa.String(length=250), nullable=True), - sa.Column('datetime', sa.DateTime(), nullable=False), - sa.PrimaryKeyConstraint('id') - ) - op.create_index(op.f('ix_simulations_alias'), 'simulations', ['alias'], unique=True) - op.create_index(op.f('ix_simulations_uuid'), 'simulations', ['uuid'], unique=True) - op.create_table('watchers', - sa.Column('id', sa.Integer(), nullable=False), - sa.Column('username', sa.String(length=250), nullable=True), - sa.Column('email', sa.String(length=1000), nullable=True), - sa.Column('notification', ChoiceType(choices=NOTIFICATION_CHOICES, length=1, enum_type=Notification), nullable=True), - sa.PrimaryKeyConstraint('id') - ) - op.create_table('metadata', - sa.Column('id', sa.Integer(), nullable=False), - sa.Column('sim_id', sa.Integer(), nullable=True), - sa.Column('element', sa.String(length=250), nullable=False), - sa.Column('value', sa.PickleType(), nullable=True), - sa.ForeignKeyConstraint(['sim_id'], ['simulations.id'], ), - sa.PrimaryKeyConstraint('id') - ) - op.create_index(op.f('ix_metadata_sim_id'), 'metadata', ['sim_id'], unique=False) - op.create_index('metadata_index', 'metadata', ['sim_id', 'element'], unique=True) - op.create_table('simulation_input_files', - sa.Column('simulation_id', sa.Integer(), nullable=True), - sa.Column('file_id', sa.Integer(), nullable=True), - sa.ForeignKeyConstraint(['file_id'], ['files.id'], ), - sa.ForeignKeyConstraint(['simulation_id'], ['simulations.id'], ) - ) - op.create_table('simulation_output_files', - sa.Column('simulation_id', sa.Integer(), nullable=True), - sa.Column('file_id', sa.Integer(), nullable=True), - sa.ForeignKeyConstraint(['file_id'], ['files.id'], ), - sa.ForeignKeyConstraint(['simulation_id'], ['simulations.id'], ) - ) - op.create_table('simulation_watchers', - sa.Column('simulation_id', sa.Integer(), nullable=True), - sa.Column('watcher_id', sa.Integer(), nullable=True), - sa.ForeignKeyConstraint(['simulation_id'], ['simulations.id'], ), - sa.ForeignKeyConstraint(['watcher_id'], ['watchers.id'], ) - ) - # ### end Alembic commands ### + # Get connection to check for existing tables + conn = op.get_bind() + inspector = sa.inspect(conn) + existing_tables = inspector.get_table_names() + # Create simulations table if it doesn't exist + if 'simulations' not in existing_tables: + op.create_table('simulations', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('uuid', UUID(), nullable=False), + sa.Column('alias', sa.String(length=250), nullable=True), + sa.Column('datetime', sa.DateTime(), nullable=False), + sa.PrimaryKeyConstraint('id') + ) + op.create_index(op.f('ix_simulations_alias'), 'simulations', ['alias'], unique=True) + op.create_index(op.f('ix_simulations_uuid'), 'simulations', ['uuid'], unique=True) + + # Create watchers table if it doesn't exist + if 'watchers' not in existing_tables: + op.create_table('watchers', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('username', sa.String(length=250), nullable=True), + sa.Column('email', sa.String(length=1000), nullable=True), + sa.Column('notification', ChoiceType(choices=NOTIFICATION_CHOICES, length=1, enum_type=Notification), nullable=True), + sa.PrimaryKeyConstraint('id') + ) + + # Create metadata table if it doesn't exist + if 'metadata' not in existing_tables: + op.create_table('metadata', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('sim_id', sa.Integer(), nullable=True), + sa.Column('element', sa.String(length=250), nullable=False), + sa.Column('value', sa.PickleType(), nullable=True), + sa.ForeignKeyConstraint(['sim_id'], ['simulations.id'], ), + sa.PrimaryKeyConstraint('id') + ) + op.create_index(op.f('ix_metadata_sim_id'), 'metadata', ['sim_id'], unique=False) + op.create_index('metadata_index', 'metadata', ['sim_id', 'element'], unique=True) + + # Create simulation_input_files table if it doesn't exist + if 'simulation_input_files' not in existing_tables: + op.create_table('simulation_input_files', + sa.Column('simulation_id', sa.Integer(), nullable=True), + sa.Column('file_id', sa.Integer(), nullable=True), + sa.ForeignKeyConstraint(['file_id'], ['files.id'], ), + sa.ForeignKeyConstraint(['simulation_id'], ['simulations.id'], ) + ) + + # Create simulation_output_files table if it doesn't exist + if 'simulation_output_files' not in existing_tables: + op.create_table('simulation_output_files', + sa.Column('simulation_id', sa.Integer(), nullable=True), + sa.Column('file_id', sa.Integer(), nullable=True), + sa.ForeignKeyConstraint(['file_id'], ['files.id'], ), + sa.ForeignKeyConstraint(['simulation_id'], ['simulations.id'], ) + ) + + # Create simulation_watchers table if it doesn't exist + if 'simulation_watchers' not in existing_tables: + op.create_table('simulation_watchers', + sa.Column('simulation_id', sa.Integer(), nullable=True), + sa.Column('watcher_id', sa.Integer(), nullable=True), + sa.ForeignKeyConstraint(['simulation_id'], ['simulations.id'], ), + sa.ForeignKeyConstraint(['watcher_id'], ['watchers.id'], ) + ) def downgrade() -> None: diff --git a/pyproject.toml b/pyproject.toml index 344e3e5..37032be 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -48,6 +48,7 @@ dependencies = [ "requests>=2.27.0", "semantic-version>=2.8", "sqlalchemy>=1.2.12,<2.0", + "alembic~=1.13", "urllib3>=1.26", ] diff --git a/simdb.db b/simdb.db deleted file mode 100644 index 8b2bf6941c3c42559345fda3d7e2473f60f949d1..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 53248 zcmeI(U2oe|7{GD6?ds-j*`2Q{9aK%k>V(>5MVkf^Y3G$dmQXhZwj0Y$T+LcuMq-a~ zfl#_n!pGoq@FBS6k{cwBow&8#q;QF>ztt-7iOs(&fvtyfC5QvL02xAI5j=ko8R zze~TCJ}Vv)^r`wm%?3RV{I`K`UwD!tF^Xhy zc~$VqgKh-~TfZgx#mA`tq5&<9I5A4;ahsKlds5W&Cr{MNU$Sv~qh(5BGQs}-Iv^C2bA$VC+5+lF=YWy7jHJlHc*Ro*#+z!itB&SBWVH&*jS z!+IH>s;j1?#QC?m`0?gEs*j?m;xjaU)lyN#rME;COx?-I8M>(;!qK`z#~WOg?(gmG zMOTf{*!M2HUUaP!QPlfUc{aGB@%oOuLhh(DEl%$BDL1u)!0RXWHSaB1F-Uf@kxD~q z#4G1l7|L9<{xCg_=EF(OvM@pNbaZ`BX6~81r*xYy%+%DonU9&FYWnW3dRd4D?#NT{ z-0Ruz Date: Wed, 11 Feb 2026 13:11:17 +0100 Subject: [PATCH 3/7] format --- alembic/env.py | 4 + .../c84b85ae4bed_initial_migration.py | 165 +++++++++++------- 2 files changed, 108 insertions(+), 61 deletions(-) diff --git a/alembic/env.py b/alembic/env.py index ccfed6f..3e64a98 100644 --- a/alembic/env.py +++ b/alembic/env.py @@ -25,12 +25,14 @@ target_metadata = Base.metadata + def get_database_url() -> str: url = os.getenv("DATABASE_URL") if not url: raise RuntimeError("DATABASE_URL is not set") return url + def run_migrations_offline() -> None: context.configure( url=get_database_url(), @@ -44,6 +46,7 @@ def run_migrations_offline() -> None: with context.begin_transaction(): context.run_migrations() + def run_migrations_online() -> None: engine = create_engine( get_database_url(), @@ -61,6 +64,7 @@ def run_migrations_online() -> None: with context.begin_transaction(): context.run_migrations() + if context.is_offline_mode(): run_migrations_offline() else: diff --git a/alembic/versions/c84b85ae4bed_initial_migration.py b/alembic/versions/c84b85ae4bed_initial_migration.py index e0193b1..24425c7 100644 --- a/alembic/versions/c84b85ae4bed_initial_migration.py +++ b/alembic/versions/c84b85ae4bed_initial_migration.py @@ -1,10 +1,11 @@ """initial migration Revision ID: c84b85ae4bed -Revises: +Revises: Create Date: 2026-02-10 15:24:11.323077 """ + from typing import Sequence, Union from alembic import op @@ -13,7 +14,7 @@ from simdb.notifications import Notification # revision identifiers, used by Alembic. -revision: str = 'c84b85ae4bed' +revision: str = "c84b85ae4bed" down_revision: Union[str, Sequence[str], None] = None branch_labels: Union[str, Sequence[str], None] = None depends_on: Union[str, Sequence[str], None] = None @@ -26,6 +27,7 @@ Notification.ALL: "A", } + def upgrade() -> None: """Upgrade schema.""" # Get connection to check for existing tables @@ -33,79 +35,120 @@ def upgrade() -> None: inspector = sa.inspect(conn) existing_tables = inspector.get_table_names() # Create simulations table if it doesn't exist - if 'simulations' not in existing_tables: - op.create_table('simulations', - sa.Column('id', sa.Integer(), nullable=False), - sa.Column('uuid', UUID(), nullable=False), - sa.Column('alias', sa.String(length=250), nullable=True), - sa.Column('datetime', sa.DateTime(), nullable=False), - sa.PrimaryKeyConstraint('id') + if "simulations" not in existing_tables: + op.create_table( + "simulations", + sa.Column("id", sa.Integer(), nullable=False), + sa.Column("uuid", UUID(), nullable=False), + sa.Column("alias", sa.String(length=250), nullable=True), + sa.Column("datetime", sa.DateTime(), nullable=False), + sa.PrimaryKeyConstraint("id"), + ) + op.create_index( + op.f("ix_simulations_alias"), "simulations", ["alias"], unique=True ) - op.create_index(op.f('ix_simulations_alias'), 'simulations', ['alias'], unique=True) - op.create_index(op.f('ix_simulations_uuid'), 'simulations', ['uuid'], unique=True) - + op.create_index( + op.f("ix_simulations_uuid"), "simulations", ["uuid"], unique=True + ) + # Create watchers table if it doesn't exist - if 'watchers' not in existing_tables: - op.create_table('watchers', - sa.Column('id', sa.Integer(), nullable=False), - sa.Column('username', sa.String(length=250), nullable=True), - sa.Column('email', sa.String(length=1000), nullable=True), - sa.Column('notification', ChoiceType(choices=NOTIFICATION_CHOICES, length=1, enum_type=Notification), nullable=True), - sa.PrimaryKeyConstraint('id') + if "watchers" not in existing_tables: + op.create_table( + "watchers", + sa.Column("id", sa.Integer(), nullable=False), + sa.Column("username", sa.String(length=250), nullable=True), + sa.Column("email", sa.String(length=1000), nullable=True), + sa.Column( + "notification", + ChoiceType( + choices=NOTIFICATION_CHOICES, length=1, enum_type=Notification + ), + nullable=True, + ), + sa.PrimaryKeyConstraint("id"), ) - + # Create metadata table if it doesn't exist - if 'metadata' not in existing_tables: - op.create_table('metadata', - sa.Column('id', sa.Integer(), nullable=False), - sa.Column('sim_id', sa.Integer(), nullable=True), - sa.Column('element', sa.String(length=250), nullable=False), - sa.Column('value', sa.PickleType(), nullable=True), - sa.ForeignKeyConstraint(['sim_id'], ['simulations.id'], ), - sa.PrimaryKeyConstraint('id') + if "metadata" not in existing_tables: + op.create_table( + "metadata", + sa.Column("id", sa.Integer(), nullable=False), + sa.Column("sim_id", sa.Integer(), nullable=True), + sa.Column("element", sa.String(length=250), nullable=False), + sa.Column("value", sa.PickleType(), nullable=True), + sa.ForeignKeyConstraint( + ["sim_id"], + ["simulations.id"], + ), + sa.PrimaryKeyConstraint("id"), ) - op.create_index(op.f('ix_metadata_sim_id'), 'metadata', ['sim_id'], unique=False) - op.create_index('metadata_index', 'metadata', ['sim_id', 'element'], unique=True) - + op.create_index( + op.f("ix_metadata_sim_id"), "metadata", ["sim_id"], unique=False + ) + op.create_index( + "metadata_index", "metadata", ["sim_id", "element"], unique=True + ) + # Create simulation_input_files table if it doesn't exist - if 'simulation_input_files' not in existing_tables: - op.create_table('simulation_input_files', - sa.Column('simulation_id', sa.Integer(), nullable=True), - sa.Column('file_id', sa.Integer(), nullable=True), - sa.ForeignKeyConstraint(['file_id'], ['files.id'], ), - sa.ForeignKeyConstraint(['simulation_id'], ['simulations.id'], ) + if "simulation_input_files" not in existing_tables: + op.create_table( + "simulation_input_files", + sa.Column("simulation_id", sa.Integer(), nullable=True), + sa.Column("file_id", sa.Integer(), nullable=True), + sa.ForeignKeyConstraint( + ["file_id"], + ["files.id"], + ), + sa.ForeignKeyConstraint( + ["simulation_id"], + ["simulations.id"], + ), ) - + # Create simulation_output_files table if it doesn't exist - if 'simulation_output_files' not in existing_tables: - op.create_table('simulation_output_files', - sa.Column('simulation_id', sa.Integer(), nullable=True), - sa.Column('file_id', sa.Integer(), nullable=True), - sa.ForeignKeyConstraint(['file_id'], ['files.id'], ), - sa.ForeignKeyConstraint(['simulation_id'], ['simulations.id'], ) + if "simulation_output_files" not in existing_tables: + op.create_table( + "simulation_output_files", + sa.Column("simulation_id", sa.Integer(), nullable=True), + sa.Column("file_id", sa.Integer(), nullable=True), + sa.ForeignKeyConstraint( + ["file_id"], + ["files.id"], + ), + sa.ForeignKeyConstraint( + ["simulation_id"], + ["simulations.id"], + ), ) - + # Create simulation_watchers table if it doesn't exist - if 'simulation_watchers' not in existing_tables: - op.create_table('simulation_watchers', - sa.Column('simulation_id', sa.Integer(), nullable=True), - sa.Column('watcher_id', sa.Integer(), nullable=True), - sa.ForeignKeyConstraint(['simulation_id'], ['simulations.id'], ), - sa.ForeignKeyConstraint(['watcher_id'], ['watchers.id'], ) + if "simulation_watchers" not in existing_tables: + op.create_table( + "simulation_watchers", + sa.Column("simulation_id", sa.Integer(), nullable=True), + sa.Column("watcher_id", sa.Integer(), nullable=True), + sa.ForeignKeyConstraint( + ["simulation_id"], + ["simulations.id"], + ), + sa.ForeignKeyConstraint( + ["watcher_id"], + ["watchers.id"], + ), ) def downgrade() -> None: """Downgrade schema.""" # ### commands auto generated by Alembic - please adjust! ### - op.drop_table('simulation_watchers') - op.drop_table('simulation_output_files') - op.drop_table('simulation_input_files') - op.drop_index('metadata_index', table_name='metadata') - op.drop_index(op.f('ix_metadata_sim_id'), table_name='metadata') - op.drop_table('metadata') - op.drop_table('watchers') - op.drop_index(op.f('ix_simulations_uuid'), table_name='simulations') - op.drop_index(op.f('ix_simulations_alias'), table_name='simulations') - op.drop_table('simulations') + op.drop_table("simulation_watchers") + op.drop_table("simulation_output_files") + op.drop_table("simulation_input_files") + op.drop_index("metadata_index", table_name="metadata") + op.drop_index(op.f("ix_metadata_sim_id"), table_name="metadata") + op.drop_table("metadata") + op.drop_table("watchers") + op.drop_index(op.f("ix_simulations_uuid"), table_name="simulations") + op.drop_index(op.f("ix_simulations_alias"), table_name="simulations") + op.drop_table("simulations") # ### end Alembic commands ### From 5bad0881386d071890ba816691a94c7d754d5ff0 Mon Sep 17 00:00:00 2001 From: Alexandra Ioan Date: Wed, 11 Feb 2026 13:15:51 +0100 Subject: [PATCH 4/7] lint --- alembic/env.py | 5 +++-- alembic/versions/c84b85ae4bed_initial_migration.py | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/alembic/env.py b/alembic/env.py index 3e64a98..0778898 100644 --- a/alembic/env.py +++ b/alembic/env.py @@ -1,11 +1,12 @@ -from logging.config import fileConfig import os import sys +from logging.config import fileConfig from pathlib import Path -from alembic import context from sqlalchemy import create_engine, pool +from alembic import context + config = context.config if config.config_file_name: diff --git a/alembic/versions/c84b85ae4bed_initial_migration.py b/alembic/versions/c84b85ae4bed_initial_migration.py index 24425c7..d6e8220 100644 --- a/alembic/versions/c84b85ae4bed_initial_migration.py +++ b/alembic/versions/c84b85ae4bed_initial_migration.py @@ -8,8 +8,9 @@ from typing import Sequence, Union -from alembic import op import sqlalchemy as sa + +from alembic import op from simdb.database.models.types import UUID, ChoiceType from simdb.notifications import Notification From 78462656d6d9a9c79f118799daf2798316424140 Mon Sep 17 00:00:00 2001 From: Alexandra Ioan Date: Fri, 13 Feb 2026 10:27:54 +0100 Subject: [PATCH 5/7] redo autogenerate alembic to add files --- .../21f2b1287595_create_init_tables.py | 139 ++++++++++++++++ .../c84b85ae4bed_initial_migration.py | 155 ------------------ 2 files changed, 139 insertions(+), 155 deletions(-) create mode 100644 alembic/versions/21f2b1287595_create_init_tables.py delete mode 100644 alembic/versions/c84b85ae4bed_initial_migration.py diff --git a/alembic/versions/21f2b1287595_create_init_tables.py b/alembic/versions/21f2b1287595_create_init_tables.py new file mode 100644 index 0000000..b0398f7 --- /dev/null +++ b/alembic/versions/21f2b1287595_create_init_tables.py @@ -0,0 +1,139 @@ +"""create init tables + +Revision ID: 21f2b1287595 +Revises: +Create Date: 2026-02-13 10:11:39.262884 + +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa +from simdb.database.models.types import URI, UUID, ChoiceType +from simdb.notifications import Notification + + + +# revision identifiers, used by Alembic. +revision: str = '21f2b1287595' +down_revision: Union[str, Sequence[str], None] = None +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + +# Define notification choices +NOTIFICATION_CHOICES = { + Notification.VALIDATION: "V", + Notification.REVISION: "R", + Notification.OBSOLESCENCE: "O", + Notification.ALL: "A", +} + +def upgrade() -> None: + """Upgrade schema.""" + # Get connection to inspect existing database schema + conn = op.get_bind() + inspector = sa.inspect(conn) + existing_tables = inspector.get_table_names() + + # Create files table if it doesn't exist + if 'files' not in existing_tables: + op.create_table('files', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('uuid', UUID(), nullable=False), + sa.Column('usage', sa.String(length=250), nullable=True), + sa.Column('uri', URI(length=1024), nullable=True), + sa.Column('checksum', sa.String(length=64), nullable=True), + sa.Column('type', sa.Enum('UNKNOWN', 'UUID', 'FILE', 'IMAS', 'UDA', name='type'), nullable=True), + sa.Column('purpose', sa.String(length=250), nullable=True), + sa.Column('sensitivity', sa.String(length=20), nullable=True), + sa.Column('access', sa.String(length=20), nullable=True), + sa.Column('embargo', sa.String(length=20), nullable=True), + sa.Column('datetime', sa.DateTime(), nullable=False), + sa.PrimaryKeyConstraint('id') + ) + op.create_index(op.f('ix_files_uuid'), 'files', ['uuid'], unique=True) + + # Create simulations table if it doesn't exist + if 'simulations' not in existing_tables: + op.create_table('simulations', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('uuid', UUID(), nullable=False), + sa.Column('alias', sa.String(length=250), nullable=True), + sa.Column('datetime', sa.DateTime(), nullable=False), + sa.PrimaryKeyConstraint('id') + ) + op.create_index(op.f('ix_simulations_alias'), 'simulations', ['alias'], unique=True) + op.create_index(op.f('ix_simulations_uuid'), 'simulations', ['uuid'], unique=True) + + # Create watchers table if it doesn't exist + if 'watchers' not in existing_tables: + op.create_table('watchers', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('username', sa.String(length=250), nullable=True), + sa.Column('email', sa.String(length=1000), nullable=True), + sa.Column( + "notification", + ChoiceType(choices=NOTIFICATION_CHOICES, length=1, enum_type=Notification), + nullable=True, + ), + sa.PrimaryKeyConstraint('id') + ) + + # Create metadata table if it doesn't exist + if 'metadata' not in existing_tables: + op.create_table('metadata', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('sim_id', sa.Integer(), nullable=True), + sa.Column('element', sa.String(length=250), nullable=False), + sa.Column('value', sa.PickleType(), nullable=True), + sa.ForeignKeyConstraint(['sim_id'], ['simulations.id'], ), + sa.PrimaryKeyConstraint('id') + ) + op.create_index(op.f('ix_metadata_sim_id'), 'metadata', ['sim_id'], unique=False) + op.create_index('metadata_index', 'metadata', ['sim_id', 'element'], unique=True) + + # Create simulation_input_files table if it doesn't exist + if 'simulation_input_files' not in existing_tables: + op.create_table('simulation_input_files', + sa.Column('simulation_id', sa.Integer(), nullable=True), + sa.Column('file_id', sa.Integer(), nullable=True), + sa.ForeignKeyConstraint(['file_id'], ['files.id'], ), + sa.ForeignKeyConstraint(['simulation_id'], ['simulations.id'], ) + ) + + # Create simulation_output_files table if it doesn't exist + if 'simulation_output_files' not in existing_tables: + op.create_table('simulation_output_files', + sa.Column('simulation_id', sa.Integer(), nullable=True), + sa.Column('file_id', sa.Integer(), nullable=True), + sa.ForeignKeyConstraint(['file_id'], ['files.id'], ), + sa.ForeignKeyConstraint(['simulation_id'], ['simulations.id'], ) + ) + + # Create simulation_watchers table if it doesn't exist + if 'simulation_watchers' not in existing_tables: + op.create_table('simulation_watchers', + sa.Column('simulation_id', sa.Integer(), nullable=True), + sa.Column('watcher_id', sa.Integer(), nullable=True), + sa.ForeignKeyConstraint(['simulation_id'], ['simulations.id'], ), + sa.ForeignKeyConstraint(['watcher_id'], ['watchers.id'], ) + ) + # ### end Alembic commands ### + + +def downgrade() -> None: + """Downgrade schema.""" + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('simulation_watchers') + op.drop_table('simulation_output_files') + op.drop_table('simulation_input_files') + op.drop_index('metadata_index', table_name='metadata') + op.drop_index(op.f('ix_metadata_sim_id'), table_name='metadata') + op.drop_table('metadata') + op.drop_table('watchers') + op.drop_index(op.f('ix_simulations_uuid'), table_name='simulations') + op.drop_index(op.f('ix_simulations_alias'), table_name='simulations') + op.drop_table('simulations') + op.drop_index(op.f('ix_files_uuid'), table_name='files') + op.drop_table('files') + # ### end Alembic commands ### diff --git a/alembic/versions/c84b85ae4bed_initial_migration.py b/alembic/versions/c84b85ae4bed_initial_migration.py deleted file mode 100644 index d6e8220..0000000 --- a/alembic/versions/c84b85ae4bed_initial_migration.py +++ /dev/null @@ -1,155 +0,0 @@ -"""initial migration - -Revision ID: c84b85ae4bed -Revises: -Create Date: 2026-02-10 15:24:11.323077 - -""" - -from typing import Sequence, Union - -import sqlalchemy as sa - -from alembic import op -from simdb.database.models.types import UUID, ChoiceType -from simdb.notifications import Notification - -# revision identifiers, used by Alembic. -revision: str = "c84b85ae4bed" -down_revision: Union[str, Sequence[str], None] = None -branch_labels: Union[str, Sequence[str], None] = None -depends_on: Union[str, Sequence[str], None] = None - -# Define notification choices -NOTIFICATION_CHOICES = { - Notification.VALIDATION: "V", - Notification.REVISION: "R", - Notification.OBSOLESCENCE: "O", - Notification.ALL: "A", -} - - -def upgrade() -> None: - """Upgrade schema.""" - # Get connection to check for existing tables - conn = op.get_bind() - inspector = sa.inspect(conn) - existing_tables = inspector.get_table_names() - # Create simulations table if it doesn't exist - if "simulations" not in existing_tables: - op.create_table( - "simulations", - sa.Column("id", sa.Integer(), nullable=False), - sa.Column("uuid", UUID(), nullable=False), - sa.Column("alias", sa.String(length=250), nullable=True), - sa.Column("datetime", sa.DateTime(), nullable=False), - sa.PrimaryKeyConstraint("id"), - ) - op.create_index( - op.f("ix_simulations_alias"), "simulations", ["alias"], unique=True - ) - op.create_index( - op.f("ix_simulations_uuid"), "simulations", ["uuid"], unique=True - ) - - # Create watchers table if it doesn't exist - if "watchers" not in existing_tables: - op.create_table( - "watchers", - sa.Column("id", sa.Integer(), nullable=False), - sa.Column("username", sa.String(length=250), nullable=True), - sa.Column("email", sa.String(length=1000), nullable=True), - sa.Column( - "notification", - ChoiceType( - choices=NOTIFICATION_CHOICES, length=1, enum_type=Notification - ), - nullable=True, - ), - sa.PrimaryKeyConstraint("id"), - ) - - # Create metadata table if it doesn't exist - if "metadata" not in existing_tables: - op.create_table( - "metadata", - sa.Column("id", sa.Integer(), nullable=False), - sa.Column("sim_id", sa.Integer(), nullable=True), - sa.Column("element", sa.String(length=250), nullable=False), - sa.Column("value", sa.PickleType(), nullable=True), - sa.ForeignKeyConstraint( - ["sim_id"], - ["simulations.id"], - ), - sa.PrimaryKeyConstraint("id"), - ) - op.create_index( - op.f("ix_metadata_sim_id"), "metadata", ["sim_id"], unique=False - ) - op.create_index( - "metadata_index", "metadata", ["sim_id", "element"], unique=True - ) - - # Create simulation_input_files table if it doesn't exist - if "simulation_input_files" not in existing_tables: - op.create_table( - "simulation_input_files", - sa.Column("simulation_id", sa.Integer(), nullable=True), - sa.Column("file_id", sa.Integer(), nullable=True), - sa.ForeignKeyConstraint( - ["file_id"], - ["files.id"], - ), - sa.ForeignKeyConstraint( - ["simulation_id"], - ["simulations.id"], - ), - ) - - # Create simulation_output_files table if it doesn't exist - if "simulation_output_files" not in existing_tables: - op.create_table( - "simulation_output_files", - sa.Column("simulation_id", sa.Integer(), nullable=True), - sa.Column("file_id", sa.Integer(), nullable=True), - sa.ForeignKeyConstraint( - ["file_id"], - ["files.id"], - ), - sa.ForeignKeyConstraint( - ["simulation_id"], - ["simulations.id"], - ), - ) - - # Create simulation_watchers table if it doesn't exist - if "simulation_watchers" not in existing_tables: - op.create_table( - "simulation_watchers", - sa.Column("simulation_id", sa.Integer(), nullable=True), - sa.Column("watcher_id", sa.Integer(), nullable=True), - sa.ForeignKeyConstraint( - ["simulation_id"], - ["simulations.id"], - ), - sa.ForeignKeyConstraint( - ["watcher_id"], - ["watchers.id"], - ), - ) - - -def downgrade() -> None: - """Downgrade schema.""" - # ### commands auto generated by Alembic - please adjust! ### - op.drop_table("simulation_watchers") - op.drop_table("simulation_output_files") - op.drop_table("simulation_input_files") - op.drop_index("metadata_index", table_name="metadata") - op.drop_index(op.f("ix_metadata_sim_id"), table_name="metadata") - op.drop_table("metadata") - op.drop_table("watchers") - op.drop_index(op.f("ix_simulations_uuid"), table_name="simulations") - op.drop_index(op.f("ix_simulations_alias"), table_name="simulations") - op.drop_table("simulations") - # ### end Alembic commands ### From 3712954c702c30bdf4184ad724a5cdf7e3998be9 Mon Sep 17 00:00:00 2001 From: Alexandra Ioan Date: Fri, 13 Feb 2026 10:28:38 +0100 Subject: [PATCH 6/7] lint --- .../21f2b1287595_create_init_tables.py | 195 +++++++++++------- 1 file changed, 119 insertions(+), 76 deletions(-) diff --git a/alembic/versions/21f2b1287595_create_init_tables.py b/alembic/versions/21f2b1287595_create_init_tables.py index b0398f7..0cf80b9 100644 --- a/alembic/versions/21f2b1287595_create_init_tables.py +++ b/alembic/versions/21f2b1287595_create_init_tables.py @@ -1,10 +1,11 @@ """create init tables Revision ID: 21f2b1287595 -Revises: +Revises: Create Date: 2026-02-13 10:11:39.262884 """ + from typing import Sequence, Union from alembic import op @@ -13,9 +14,8 @@ from simdb.notifications import Notification - # revision identifiers, used by Alembic. -revision: str = '21f2b1287595' +revision: str = "21f2b1287595" down_revision: Union[str, Sequence[str], None] = None branch_labels: Union[str, Sequence[str], None] = None depends_on: Union[str, Sequence[str], None] = None @@ -28,95 +28,138 @@ Notification.ALL: "A", } + def upgrade() -> None: """Upgrade schema.""" # Get connection to inspect existing database schema conn = op.get_bind() inspector = sa.inspect(conn) existing_tables = inspector.get_table_names() - + # Create files table if it doesn't exist - if 'files' not in existing_tables: - op.create_table('files', - sa.Column('id', sa.Integer(), nullable=False), - sa.Column('uuid', UUID(), nullable=False), - sa.Column('usage', sa.String(length=250), nullable=True), - sa.Column('uri', URI(length=1024), nullable=True), - sa.Column('checksum', sa.String(length=64), nullable=True), - sa.Column('type', sa.Enum('UNKNOWN', 'UUID', 'FILE', 'IMAS', 'UDA', name='type'), nullable=True), - sa.Column('purpose', sa.String(length=250), nullable=True), - sa.Column('sensitivity', sa.String(length=20), nullable=True), - sa.Column('access', sa.String(length=20), nullable=True), - sa.Column('embargo', sa.String(length=20), nullable=True), - sa.Column('datetime', sa.DateTime(), nullable=False), - sa.PrimaryKeyConstraint('id') + if "files" not in existing_tables: + op.create_table( + "files", + sa.Column("id", sa.Integer(), nullable=False), + sa.Column("uuid", UUID(), nullable=False), + sa.Column("usage", sa.String(length=250), nullable=True), + sa.Column("uri", URI(length=1024), nullable=True), + sa.Column("checksum", sa.String(length=64), nullable=True), + sa.Column( + "type", + sa.Enum("UNKNOWN", "UUID", "FILE", "IMAS", "UDA", name="type"), + nullable=True, + ), + sa.Column("purpose", sa.String(length=250), nullable=True), + sa.Column("sensitivity", sa.String(length=20), nullable=True), + sa.Column("access", sa.String(length=20), nullable=True), + sa.Column("embargo", sa.String(length=20), nullable=True), + sa.Column("datetime", sa.DateTime(), nullable=False), + sa.PrimaryKeyConstraint("id"), ) - op.create_index(op.f('ix_files_uuid'), 'files', ['uuid'], unique=True) + op.create_index(op.f("ix_files_uuid"), "files", ["uuid"], unique=True) # Create simulations table if it doesn't exist - if 'simulations' not in existing_tables: - op.create_table('simulations', - sa.Column('id', sa.Integer(), nullable=False), - sa.Column('uuid', UUID(), nullable=False), - sa.Column('alias', sa.String(length=250), nullable=True), - sa.Column('datetime', sa.DateTime(), nullable=False), - sa.PrimaryKeyConstraint('id') + if "simulations" not in existing_tables: + op.create_table( + "simulations", + sa.Column("id", sa.Integer(), nullable=False), + sa.Column("uuid", UUID(), nullable=False), + sa.Column("alias", sa.String(length=250), nullable=True), + sa.Column("datetime", sa.DateTime(), nullable=False), + sa.PrimaryKeyConstraint("id"), + ) + op.create_index( + op.f("ix_simulations_alias"), "simulations", ["alias"], unique=True + ) + op.create_index( + op.f("ix_simulations_uuid"), "simulations", ["uuid"], unique=True ) - op.create_index(op.f('ix_simulations_alias'), 'simulations', ['alias'], unique=True) - op.create_index(op.f('ix_simulations_uuid'), 'simulations', ['uuid'], unique=True) # Create watchers table if it doesn't exist - if 'watchers' not in existing_tables: - op.create_table('watchers', - sa.Column('id', sa.Integer(), nullable=False), - sa.Column('username', sa.String(length=250), nullable=True), - sa.Column('email', sa.String(length=1000), nullable=True), + if "watchers" not in existing_tables: + op.create_table( + "watchers", + sa.Column("id", sa.Integer(), nullable=False), + sa.Column("username", sa.String(length=250), nullable=True), + sa.Column("email", sa.String(length=1000), nullable=True), sa.Column( "notification", - ChoiceType(choices=NOTIFICATION_CHOICES, length=1, enum_type=Notification), + ChoiceType( + choices=NOTIFICATION_CHOICES, length=1, enum_type=Notification + ), nullable=True, ), - sa.PrimaryKeyConstraint('id') + sa.PrimaryKeyConstraint("id"), ) # Create metadata table if it doesn't exist - if 'metadata' not in existing_tables: - op.create_table('metadata', - sa.Column('id', sa.Integer(), nullable=False), - sa.Column('sim_id', sa.Integer(), nullable=True), - sa.Column('element', sa.String(length=250), nullable=False), - sa.Column('value', sa.PickleType(), nullable=True), - sa.ForeignKeyConstraint(['sim_id'], ['simulations.id'], ), - sa.PrimaryKeyConstraint('id') + if "metadata" not in existing_tables: + op.create_table( + "metadata", + sa.Column("id", sa.Integer(), nullable=False), + sa.Column("sim_id", sa.Integer(), nullable=True), + sa.Column("element", sa.String(length=250), nullable=False), + sa.Column("value", sa.PickleType(), nullable=True), + sa.ForeignKeyConstraint( + ["sim_id"], + ["simulations.id"], + ), + sa.PrimaryKeyConstraint("id"), ) - op.create_index(op.f('ix_metadata_sim_id'), 'metadata', ['sim_id'], unique=False) - op.create_index('metadata_index', 'metadata', ['sim_id', 'element'], unique=True) - + op.create_index( + op.f("ix_metadata_sim_id"), "metadata", ["sim_id"], unique=False + ) + op.create_index( + "metadata_index", "metadata", ["sim_id", "element"], unique=True + ) + # Create simulation_input_files table if it doesn't exist - if 'simulation_input_files' not in existing_tables: - op.create_table('simulation_input_files', - sa.Column('simulation_id', sa.Integer(), nullable=True), - sa.Column('file_id', sa.Integer(), nullable=True), - sa.ForeignKeyConstraint(['file_id'], ['files.id'], ), - sa.ForeignKeyConstraint(['simulation_id'], ['simulations.id'], ) + if "simulation_input_files" not in existing_tables: + op.create_table( + "simulation_input_files", + sa.Column("simulation_id", sa.Integer(), nullable=True), + sa.Column("file_id", sa.Integer(), nullable=True), + sa.ForeignKeyConstraint( + ["file_id"], + ["files.id"], + ), + sa.ForeignKeyConstraint( + ["simulation_id"], + ["simulations.id"], + ), ) # Create simulation_output_files table if it doesn't exist - if 'simulation_output_files' not in existing_tables: - op.create_table('simulation_output_files', - sa.Column('simulation_id', sa.Integer(), nullable=True), - sa.Column('file_id', sa.Integer(), nullable=True), - sa.ForeignKeyConstraint(['file_id'], ['files.id'], ), - sa.ForeignKeyConstraint(['simulation_id'], ['simulations.id'], ) + if "simulation_output_files" not in existing_tables: + op.create_table( + "simulation_output_files", + sa.Column("simulation_id", sa.Integer(), nullable=True), + sa.Column("file_id", sa.Integer(), nullable=True), + sa.ForeignKeyConstraint( + ["file_id"], + ["files.id"], + ), + sa.ForeignKeyConstraint( + ["simulation_id"], + ["simulations.id"], + ), ) # Create simulation_watchers table if it doesn't exist - if 'simulation_watchers' not in existing_tables: - op.create_table('simulation_watchers', - sa.Column('simulation_id', sa.Integer(), nullable=True), - sa.Column('watcher_id', sa.Integer(), nullable=True), - sa.ForeignKeyConstraint(['simulation_id'], ['simulations.id'], ), - sa.ForeignKeyConstraint(['watcher_id'], ['watchers.id'], ) + if "simulation_watchers" not in existing_tables: + op.create_table( + "simulation_watchers", + sa.Column("simulation_id", sa.Integer(), nullable=True), + sa.Column("watcher_id", sa.Integer(), nullable=True), + sa.ForeignKeyConstraint( + ["simulation_id"], + ["simulations.id"], + ), + sa.ForeignKeyConstraint( + ["watcher_id"], + ["watchers.id"], + ), ) # ### end Alembic commands ### @@ -124,16 +167,16 @@ def upgrade() -> None: def downgrade() -> None: """Downgrade schema.""" # ### commands auto generated by Alembic - please adjust! ### - op.drop_table('simulation_watchers') - op.drop_table('simulation_output_files') - op.drop_table('simulation_input_files') - op.drop_index('metadata_index', table_name='metadata') - op.drop_index(op.f('ix_metadata_sim_id'), table_name='metadata') - op.drop_table('metadata') - op.drop_table('watchers') - op.drop_index(op.f('ix_simulations_uuid'), table_name='simulations') - op.drop_index(op.f('ix_simulations_alias'), table_name='simulations') - op.drop_table('simulations') - op.drop_index(op.f('ix_files_uuid'), table_name='files') - op.drop_table('files') + op.drop_table("simulation_watchers") + op.drop_table("simulation_output_files") + op.drop_table("simulation_input_files") + op.drop_index("metadata_index", table_name="metadata") + op.drop_index(op.f("ix_metadata_sim_id"), table_name="metadata") + op.drop_table("metadata") + op.drop_table("watchers") + op.drop_index(op.f("ix_simulations_uuid"), table_name="simulations") + op.drop_index(op.f("ix_simulations_alias"), table_name="simulations") + op.drop_table("simulations") + op.drop_index(op.f("ix_files_uuid"), table_name="files") + op.drop_table("files") # ### end Alembic commands ### From dbc96bccd46fb8fd892acc764568ae4cd6b0ebfa Mon Sep 17 00:00:00 2001 From: Alexandra Ioan Date: Fri, 13 Feb 2026 10:35:43 +0100 Subject: [PATCH 7/7] let Base.metadata handle all models --- alembic/env.py | 7 ------- alembic/versions/21f2b1287595_create_init_tables.py | 4 ++-- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/alembic/env.py b/alembic/env.py index 0778898..36552ec 100644 --- a/alembic/env.py +++ b/alembic/env.py @@ -16,13 +16,6 @@ sys.path.insert(0, str(SRC_PATH)) from simdb.database.models import Base # noqa -from simdb.database.models import ( # noqa - file, - metadata, - simulation, - watcher, -) -from simdb.database.models import types # noqa target_metadata = Base.metadata diff --git a/alembic/versions/21f2b1287595_create_init_tables.py b/alembic/versions/21f2b1287595_create_init_tables.py index 0cf80b9..68b8aa0 100644 --- a/alembic/versions/21f2b1287595_create_init_tables.py +++ b/alembic/versions/21f2b1287595_create_init_tables.py @@ -8,12 +8,12 @@ from typing import Sequence, Union -from alembic import op import sqlalchemy as sa + +from alembic import op from simdb.database.models.types import URI, UUID, ChoiceType from simdb.notifications import Notification - # revision identifiers, used by Alembic. revision: str = "21f2b1287595" down_revision: Union[str, Sequence[str], None] = None