Phase 7B: per-plugin Alembic chains for bundled plugins
Each of the six bundled plugins (computers, equipment, network,
notifications, printers, usb) now has its own Alembic chain with a
baseline migration. Sister sites adopting one of these plugins can
manage its schema via `flask plugin migrate <name>` instead of relying
on db.create_all to bootstrap everything.
Existing single-site deploys that bootstrap via db.create_all continue
to work unchanged. The chains coexist; the bootstrap path stays the
operator's choice.
Framework
- shopdb/plugins/alembic_template.py: shared env.py logic + helpers.
PLUGIN_TABLE_OWNERS pins which tables belong to which plugin (explicit
registry, not import-side-effect). _get_plugin_metadata filters
db.metadata to only the named plugin's tables. create_plugin_tables /
drop_plugin_tables emit DDL via SQLAlchemy CreateTable so the table
definitions stay sourced from the models, not duplicated.
- shopdb/plugins/__init__.py: PluginManager.upgrade_all_plugins() runs
pending migrations across every discovered plugin and returns a status
dict. Idempotent (Alembic skips applied revisions).
CLI
- `flask plugin upgrade-all` runs pending migrations for every plugin.
Used on a fresh deploy after the core schema is in place.
Per-plugin scaffolding
- plugins/{computers,equipment,network,notifications,printers,usb}/
migrations/{alembic.ini, env.py, script.py.mako, versions/0001_baseline.py}
- Each env.py is a 5-line shim that sets PLUGIN_NAME and delegates to
the shared template. Each 0001_baseline calls create_plugin_tables(name)
/ drop_plugin_tables(name); no duplication of column definitions.
Tests
- tests/test_plugin_migrations.py (18 cases): every bundled plugin has
an entry in PLUGIN_TABLE_OWNERS, has the on-disk Alembic scaffolding,
and the filtered MetaData contains every owned table (catches drift
between the template's table list and what the models declare).
- 129 tests pass.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
30
plugins/computers/migrations/alembic.ini
Normal file
30
plugins/computers/migrations/alembic.ini
Normal file
@@ -0,0 +1,30 @@
|
||||
[alembic]
|
||||
script_location = .
|
||||
prepend_sys_path = .
|
||||
file_template = %%(rev)s_%%(slug)s
|
||||
|
||||
[logging]
|
||||
keys = root
|
||||
|
||||
[loggers]
|
||||
keys = root
|
||||
|
||||
[handlers]
|
||||
keys = console
|
||||
|
||||
[formatters]
|
||||
keys = generic
|
||||
|
||||
[logger_root]
|
||||
level = INFO
|
||||
handlers = console
|
||||
qualname =
|
||||
|
||||
[handler_console]
|
||||
class = StreamHandler
|
||||
args = (sys.stderr,)
|
||||
level = INFO
|
||||
formatter = generic
|
||||
|
||||
[formatter_generic]
|
||||
format = %%(levelname)-5.5s [%%(name)s] %%(message)s
|
||||
12
plugins/computers/migrations/env.py
Normal file
12
plugins/computers/migrations/env.py
Normal file
@@ -0,0 +1,12 @@
|
||||
"""Alembic env.py for the computers plugin.
|
||||
|
||||
Thin shim that sets PLUGIN_NAME then delegates to the shared template at
|
||||
shopdb.plugins.alembic_template, which filters MetaData to only this
|
||||
plugin's tables and runs Alembic against the Flask app's engine.
|
||||
"""
|
||||
import os
|
||||
os.environ['PLUGIN_NAME'] = 'computers'
|
||||
|
||||
from shopdb.plugins.alembic_template import run_migrations
|
||||
|
||||
run_migrations()
|
||||
23
plugins/computers/migrations/script.py.mako
Normal file
23
plugins/computers/migrations/script.py.mako
Normal file
@@ -0,0 +1,23 @@
|
||||
"""${message}
|
||||
|
||||
Revision ID: ${up_revision}
|
||||
Revises: ${down_revision | comma,n}
|
||||
Create Date: ${create_date}
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
${imports if imports else ""}
|
||||
|
||||
revision = ${repr(up_revision)}
|
||||
down_revision = ${repr(down_revision)}
|
||||
branch_labels = ${repr(branch_labels)}
|
||||
depends_on = ${repr(depends_on)}
|
||||
|
||||
|
||||
def upgrade():
|
||||
${upgrades if upgrades else "pass"}
|
||||
|
||||
|
||||
def downgrade():
|
||||
${downgrades if downgrades else "pass"}
|
||||
27
plugins/computers/migrations/versions/0001_baseline.py
Normal file
27
plugins/computers/migrations/versions/0001_baseline.py
Normal file
@@ -0,0 +1,27 @@
|
||||
"""computers plugin: baseline schema
|
||||
|
||||
Creates every table owned by the computers plugin per
|
||||
shopdb.plugins.alembic_template.PLUGIN_TABLE_OWNERS. The table definitions
|
||||
are derived from the SQLAlchemy models at migration runtime so this stays
|
||||
in lockstep with the model layer without duplication.
|
||||
|
||||
Revision ID: 0001_baseline_computers
|
||||
Revises:
|
||||
Create Date: 2026-05-30
|
||||
|
||||
"""
|
||||
from shopdb.plugins.alembic_template import create_plugin_tables, drop_plugin_tables
|
||||
|
||||
|
||||
revision = '0001_baseline_computers'
|
||||
down_revision = None
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
create_plugin_tables('computers')
|
||||
|
||||
|
||||
def downgrade():
|
||||
drop_plugin_tables('computers')
|
||||
Reference in New Issue
Block a user