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..36552ec --- /dev/null +++ b/alembic/env.py @@ -0,0 +1,65 @@ +import os +import sys +from logging.config import fileConfig +from pathlib import Path + +from sqlalchemy import create_engine, pool + +from alembic import context + +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 + +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/21f2b1287595_create_init_tables.py b/alembic/versions/21f2b1287595_create_init_tables.py new file mode 100644 index 0000000..68b8aa0 --- /dev/null +++ b/alembic/versions/21f2b1287595_create_init_tables.py @@ -0,0 +1,182 @@ +"""create init tables + +Revision ID: 21f2b1287595 +Revises: +Create Date: 2026-02-13 10:11:39.262884 + +""" + +from typing import Sequence, Union + +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 +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/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/pyproject.toml b/pyproject.toml index 31b1501..f45d7dd 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/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