diff --git a/Makefile b/Makefile index c9331af..13ba396 100644 --- a/Makefile +++ b/Makefile @@ -25,7 +25,6 @@ djlint: venv ## Format the templates pylint: venv ## Runs pylint on the code venv/bin/pylint argos lint: djlint pylint - help: @python3 -c "$$PRINT_HELP_PYSCRIPT" < $(MAKEFILE_LIST) diff --git a/alembic.ini b/alembic.ini new file mode 100644 index 0000000..afb61ac --- /dev/null +++ b/alembic.ini @@ -0,0 +1,41 @@ +[alembic] +script_location = alembic +prepend_sys_path = . + +version_path_separator = os +sqlalchemy.url = sqlite:////tmp/argos.db + +# Logging configuration +[loggers] +keys = root,sqlalchemy,alembic + +[handlers] +keys = console + +[formatters] +keys = generic + +[logger_root] +level = WARN +handlers = console +qualname = + +[logger_sqlalchemy] +level = WARN +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..347ff3f --- /dev/null +++ b/alembic/env.py @@ -0,0 +1,62 @@ +from logging.config import fileConfig + +from alembic import context +from argos.server.models import Base +from sqlalchemy import engine_from_config, pool + +config = context.config + +if config.config_file_name is not None: + fileConfig(config.config_file_name) + +target_metadata = Base.metadata + + +def run_migrations_offline() -> None: + """Run migrations in 'offline' mode. + + This configures the context with just a URL + and not an Engine, though an Engine is acceptable + here as well. By skipping the Engine creation + we don't even need a DBAPI to be available. + + Calls to context.execute() here emit the given string to the + script output. + + """ + url = config.get_main_option("sqlalchemy.url") + context.configure( + url=url, + target_metadata=target_metadata, + literal_binds=True, + dialect_opts={"paramstyle": "named"}, + ) + + with context.begin_transaction(): + context.run_migrations() + + +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. + + """ + connectable = engine_from_config( + config.get_section(config.config_ini_section, {}), + prefix="sqlalchemy.", + poolclass=pool.NullPool, + ) + + 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(): + 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..fbc4b07 --- /dev/null +++ b/alembic/script.py.mako @@ -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"} diff --git a/alembic/versions/7d480e6f1112_initial_migrations.py b/alembic/versions/7d480e6f1112_initial_migrations.py new file mode 100644 index 0000000..87bb415 --- /dev/null +++ b/alembic/versions/7d480e6f1112_initial_migrations.py @@ -0,0 +1,63 @@ +"""Initial migrations + +Revision ID: 7d480e6f1112 +Revises: +Create Date: 2023-12-16 23:33:40.059077 + +""" +from typing import Sequence, Union + +import sqlalchemy as sa + +from alembic import op + +# revision identifiers, used by Alembic. +revision: str = "7d480e6f1112" +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: + op.create_table( + "tasks", + sa.Column("id", sa.Integer(), nullable=False), + sa.Column("url", sa.String(), nullable=False), + sa.Column("domain", sa.String(), nullable=False), + sa.Column("check", sa.String(), nullable=False), + sa.Column("expected", sa.String(), nullable=False), + sa.Column("frequency", sa.Integer(), nullable=False), + sa.Column("selected_by", sa.String(), nullable=True), + sa.Column("selected_at", sa.DateTime(), nullable=True), + sa.Column("completed_at", sa.DateTime(), nullable=True), + sa.Column("next_run", sa.DateTime(), nullable=True), + sa.PrimaryKeyConstraint("id"), + ) + op.create_table( + "results", + sa.Column("id", sa.Integer(), nullable=False), + sa.Column("task_id", sa.Integer(), nullable=False), + sa.Column("agent_id", sa.String(), nullable=True), + sa.Column("submitted_at", sa.DateTime(), nullable=False), + sa.Column( + "status", + sa.Enum("success", "failure", "error", "on-check", name="status"), + nullable=False, + ), + sa.Column( + "severity", + sa.Enum("ok", "warning", "critical", name="severity"), + nullable=False, + ), + sa.Column("context", sa.JSON(), nullable=False), + sa.ForeignKeyConstraint( + ["task_id"], + ["tasks.id"], + ), + sa.PrimaryKeyConstraint("id"), + ) + + +def downgrade() -> None: + op.drop_table("results") + op.drop_table("tasks") diff --git a/docs/developer/migrations.md b/docs/developer/migrations.md new file mode 100644 index 0000000..12746cd --- /dev/null +++ b/docs/developer/migrations.md @@ -0,0 +1,11 @@ +# Adding a database migration + +We are using [Alembic](https://alembic.sqlalchemy.org) to handle the database +migrations. Here is how to proceed in order to add a new migration: + +First, do your changes in the code, change the model, add new tables, etc. Once +you're done, you can create a new migration. + +```bash +venv/bin/alembic revision --autogenerate -m "migration reason" +```