feat(cloud): Alembic, license token v2 with modules, plan presets, module editor

Platform v2 step 1.7 (APPS_PLATFORM D-3, D-20, D-21, D-22, §5.2).
- Alembic replaces create_all + _run_migrations. 0001_cloud_baseline is
  exactly the v1 production schema (incl. the dead menu_hours_en/_gr), checked
  against a frozen fixture; schema.py refuses an unstamped v1 DB, so prod is
  stamped once at cutover (DEPLOYMENT_GUIDE Part 6)
- 0002_v2_licensing: sites.modules (JSON), sites.module_catalog(_at),
  plan_presets; existing sites get every module v1 had (KDS unlimited); the
  same map is seeded as preset "Everything (v1 sites)"; dead columns dropped
- license token v: 2 with a normalized `modules` map
- heartbeat v2: request {version, local_ip, module_catalog} (catalogue stored
  per site); response {license_token, latest_version, waiter_domain,
  site_numeric_id} - the unsigned v1 license fields are gone
- sites API: create with a preset, PUT /{id}/modules (validated),
  POST /{id}/apply-preset/{preset}; /api/presets CRUD
- sysadmin panel: Licensed modules card per site (built from the reported
  catalogue, fallback list), preset picker on register, Presets page
- .dockerignore: local *.db, .env and tests no longer baked into the image
- tests (new): baseline = v1, unstamped refused, stamp+upgrade of a v1 DB,
  drift, token v2 shape/signature, module edits, presets; CL-2/CL-7 rules

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-12 19:27:47 +02:00
co-authored by Claude Opus 5.5
parent 048b46af41
commit 48fd10df77
30 changed files with 1306 additions and 61 deletions
+42
View File
@@ -0,0 +1,42 @@
"""Alembic environment for cloud_backend (APPS_PLATFORM D-22).
Agent notes: target metadata = models/all.py; render_as_batch for SQLite; a caller may
pass a connection in config.attributes["connection"] (schema.py, tests); otherwise
config.settings.DATABASE_URL is used.
"""
import sys
from pathlib import Path
from alembic import context
from sqlalchemy import create_engine
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
from database import Base # noqa: E402
import models.all # noqa: E402,F401
target_metadata = Base.metadata
def _run(connection):
context.configure(connection=connection, target_metadata=target_metadata,
render_as_batch=True, compare_type=True)
with context.begin_transaction():
context.run_migrations()
def run_migrations_online() -> None:
connection = context.config.attributes.get("connection")
if connection is not None:
_run(connection)
return
from config import settings
engine = create_engine(settings.DATABASE_URL)
with engine.connect() as conn:
_run(conn)
engine.dispose()
if context.is_offline_mode():
raise SystemExit("Offline (--sql) mode is not supported.")
run_migrations_online()
+22
View File
@@ -0,0 +1,22 @@
"""${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() -> None:
${upgrades if upgrades else "pass"}
def downgrade() -> None:
${downgrades if downgrades else "pass"}
@@ -0,0 +1,191 @@
"""cloud v1 baseline: exactly the schema the v1 production cloud has (D-22)
Autogenerated from the models, then hand-edited so it equals what v1's create_all +
_run_migrations() produce: sites also has menu_hours_en / menu_hours_gr (v1 superseded
them by menu_hours but never dropped them; 0002 drops them).
The production DB is never upgraded THROUGH this revision: it is stamped to it at
cutover, so this file must describe production, not an ideal schema.
tests/test_schema.py compares it with a database built by v1's code.
Revision ID: 0001_cloud_baseline
Revises:
Create Date: 2026-11-12 19:18:38.281895
"""
from alembic import op
import sqlalchemy as sa
revision = '0001_cloud_baseline'
down_revision = None
branch_labels = None
depends_on = None
def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('admins',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('username', sa.String(), nullable=False),
sa.Column('password_hash', sa.String(), nullable=False),
sa.Column('role', sa.String(), nullable=True),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('username')
)
with op.batch_alter_table('admins', schema=None) as batch_op:
batch_op.create_index(batch_op.f('ix_admins_id'), ['id'], unique=False)
op.create_table('manager_accounts',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('email', sa.String(), nullable=False),
sa.Column('password_hash', sa.String(), nullable=False),
sa.Column('full_name', sa.String(), nullable=True),
sa.Column('is_active', sa.Integer(), nullable=False),
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=True),
sa.PrimaryKeyConstraint('id')
)
with op.batch_alter_table('manager_accounts', schema=None) as batch_op:
batch_op.create_index(batch_op.f('ix_manager_accounts_email'), ['email'], unique=True)
batch_op.create_index(batch_op.f('ix_manager_accounts_id'), ['id'], unique=False)
op.create_table('sites',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('site_id', sa.String(), nullable=False),
sa.Column('name', sa.String(), nullable=False),
sa.Column('owner_name', sa.String(), nullable=False),
sa.Column('contact_email', sa.String(), nullable=False),
sa.Column('secret_key_hash', sa.String(), nullable=False),
sa.Column('is_active', sa.Boolean(), nullable=True),
sa.Column('is_locked', sa.Boolean(), nullable=True),
sa.Column('lock_reason', sa.String(), nullable=True),
sa.Column('license_expires_at', sa.DateTime(timezone=True), nullable=False),
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=True),
sa.Column('last_seen_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('last_seen_ip', sa.String(), nullable=True),
sa.Column('last_seen_local_ip', sa.String(), nullable=True),
sa.Column('waiter_domain', sa.String(), nullable=True),
sa.Column('order_counter', sa.Integer(), nullable=False),
sa.Column('menu_mode', sa.String(), nullable=False),
sa.Column('menu_display_name', sa.String(), nullable=True),
sa.Column('menu_tagline_en', sa.String(), nullable=True),
sa.Column('menu_tagline_gr', sa.String(), nullable=True),
sa.Column('menu_blurb_en', sa.String(), nullable=True),
sa.Column('menu_blurb_gr', sa.String(), nullable=True),
sa.Column('menu_hours', sa.String(), nullable=True),
sa.Column('menu_hours_en', sa.String(), nullable=True),
sa.Column('menu_hours_gr', sa.String(), nullable=True),
sa.Column('menu_header_image_url', sa.String(), nullable=True),
sa.PrimaryKeyConstraint('id')
)
with op.batch_alter_table('sites', schema=None) as batch_op:
batch_op.create_index(batch_op.f('ix_sites_id'), ['id'], unique=False)
batch_op.create_index(batch_op.f('ix_sites_site_id'), ['site_id'], unique=True)
op.create_table('manager_site_access',
sa.Column('manager_id', sa.Integer(), nullable=False),
sa.Column('site_id', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['manager_id'], ['manager_accounts.id'], ),
sa.ForeignKeyConstraint(['site_id'], ['sites.id'], ),
sa.PrimaryKeyConstraint('manager_id', 'site_id')
)
op.create_table('menu_snapshots',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('site_id', sa.Integer(), nullable=False),
sa.Column('snapshot_json', sa.Text(), nullable=False),
sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=True),
sa.ForeignKeyConstraint(['site_id'], ['sites.id'], ),
sa.PrimaryKeyConstraint('id')
)
with op.batch_alter_table('menu_snapshots', schema=None) as batch_op:
batch_op.create_index(batch_op.f('ix_menu_snapshots_id'), ['id'], unique=False)
batch_op.create_index(batch_op.f('ix_menu_snapshots_site_id'), ['site_id'], unique=True)
op.create_table('online_orders',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('site_id', sa.Integer(), nullable=False),
sa.Column('public_ref', sa.String(), nullable=False),
sa.Column('order_type', sa.String(), nullable=False),
sa.Column('customer_name', sa.String(), nullable=False),
sa.Column('customer_phone', sa.String(), nullable=True),
sa.Column('customer_address', sa.Text(), nullable=True),
sa.Column('customer_notes', sa.Text(), nullable=True),
sa.Column('items_json', sa.Text(), nullable=False),
sa.Column('subtotal', sa.Float(), nullable=False),
sa.Column('delivery_fee', sa.Float(), nullable=False),
sa.Column('total', sa.Float(), nullable=False),
sa.Column('status', sa.String(), nullable=False),
sa.Column('rejection_reason', sa.String(), nullable=True),
sa.Column('synced_to_local', sa.Integer(), nullable=False),
sa.Column('local_order_id', sa.Integer(), nullable=True),
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=True),
sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=True),
sa.ForeignKeyConstraint(['site_id'], ['sites.id'], ),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('public_ref')
)
with op.batch_alter_table('online_orders', schema=None) as batch_op:
batch_op.create_index(batch_op.f('ix_online_orders_id'), ['id'], unique=False)
batch_op.create_index(batch_op.f('ix_online_orders_site_id'), ['site_id'], unique=False)
batch_op.create_index(batch_op.f('ix_online_orders_status'), ['status'], unique=False)
op.create_table('product_images',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('site_id', sa.Integer(), nullable=False),
sa.Column('product_id', sa.Integer(), nullable=False),
sa.Column('image_url', sa.String(), nullable=False),
sa.Column('image_hash', sa.String(), nullable=False),
sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=True),
sa.ForeignKeyConstraint(['site_id'], ['sites.id'], ),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('site_id', 'product_id', name='uq_product_images_site_product')
)
with op.batch_alter_table('product_images', schema=None) as batch_op:
batch_op.create_index(batch_op.f('ix_product_images_id'), ['id'], unique=False)
batch_op.create_index(batch_op.f('ix_product_images_site_id'), ['site_id'], unique=False)
op.create_table('stats_snapshots',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('site_id', sa.Integer(), nullable=False),
sa.Column('snapshot_json', sa.Text(), nullable=False),
sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=True),
sa.ForeignKeyConstraint(['site_id'], ['sites.id'], ),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('site_id')
)
# ### end Alembic commands ###
def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('stats_snapshots')
with op.batch_alter_table('product_images', schema=None) as batch_op:
batch_op.drop_index(batch_op.f('ix_product_images_site_id'))
batch_op.drop_index(batch_op.f('ix_product_images_id'))
op.drop_table('product_images')
with op.batch_alter_table('online_orders', schema=None) as batch_op:
batch_op.drop_index(batch_op.f('ix_online_orders_status'))
batch_op.drop_index(batch_op.f('ix_online_orders_site_id'))
batch_op.drop_index(batch_op.f('ix_online_orders_id'))
op.drop_table('online_orders')
with op.batch_alter_table('menu_snapshots', schema=None) as batch_op:
batch_op.drop_index(batch_op.f('ix_menu_snapshots_site_id'))
batch_op.drop_index(batch_op.f('ix_menu_snapshots_id'))
op.drop_table('menu_snapshots')
op.drop_table('manager_site_access')
with op.batch_alter_table('sites', schema=None) as batch_op:
batch_op.drop_index(batch_op.f('ix_sites_site_id'))
batch_op.drop_index(batch_op.f('ix_sites_id'))
op.drop_table('sites')
with op.batch_alter_table('manager_accounts', schema=None) as batch_op:
batch_op.drop_index(batch_op.f('ix_manager_accounts_id'))
batch_op.drop_index(batch_op.f('ix_manager_accounts_email'))
op.drop_table('manager_accounts')
with op.batch_alter_table('admins', schema=None) as batch_op:
batch_op.drop_index(batch_op.f('ix_admins_id'))
op.drop_table('admins')
# ### end Alembic commands ###
@@ -0,0 +1,70 @@
"""v2 licensing: site modules, catalogue, plan presets (D-3, D-20, D-21, §5.2)
Hand-edited:
- existing sites get V1_SITE_MODULES: every module a v1 site already had, with no
limits tighter than v1 (KDS screens unlimited). At cutover each site keeps doing what
it did; the owner can then change sites in the sysadmin panel. Frozen here on purpose.
- the same map is seeded as the preset "Everything (v1 sites)".
- menu_hours_en / menu_hours_gr (superseded in v1) are dropped.
Revision ID: 0002_v2_licensing
Revises: 0001_cloud_baseline
Create Date: 2026-11-12 19:19:08.073899
"""
import json
from alembic import op
import sqlalchemy as sa
V1_SITE_MODULES = {
"ordering": {}, "waiter_app": {}, "history": {}, "reports": {}, "pricing": {}, "tabs": {},
"crm": {}, "expenses": {}, "inventory": {}, "staff_planning": {}, "notes": {}, "chat": {},
"reservations": {}, "kds": {"limits": {"devices": None}}, "connect_menu": {},
"connect_orders": {}, "connect_remote": {}, "caller_id": {}, "fiscal": {},
}
revision = '0002_v2_licensing'
down_revision = '0001_cloud_baseline'
branch_labels = None
depends_on = None
def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('plan_presets',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(), nullable=False),
sa.Column('description', sa.String(), nullable=True),
sa.Column('modules', sa.JSON(), nullable=False),
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=True),
sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=True),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('name')
)
with op.batch_alter_table('sites', schema=None) as batch_op:
batch_op.add_column(sa.Column('modules', sa.JSON(), nullable=False, server_default=sa.text("'{}'")))
batch_op.add_column(sa.Column('module_catalog', sa.JSON(), nullable=True))
batch_op.add_column(sa.Column('module_catalog_at', sa.DateTime(timezone=True), nullable=True))
batch_op.drop_column('menu_hours_en')
batch_op.drop_column('menu_hours_gr')
# ### end Alembic commands ###
conn = op.get_bind()
conn.execute(sa.text("update sites set modules = :m"), {"m": json.dumps(V1_SITE_MODULES)})
conn.execute(sa.text("insert into plan_presets (name, description, modules) values (:n, :d, :m)"),
{"n": "Everything (v1 sites)", "d": "Every module v1 sites had, no extra limits",
"m": json.dumps(V1_SITE_MODULES)})
def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('sites', schema=None) as batch_op:
batch_op.add_column(sa.Column('menu_hours_gr', sa.VARCHAR(), nullable=True))
batch_op.add_column(sa.Column('menu_hours_en', sa.VARCHAR(), nullable=True))
batch_op.drop_column('module_catalog_at')
batch_op.drop_column('module_catalog')
batch_op.drop_column('modules')
op.drop_table('plan_presets')
# ### end Alembic commands ###