Decouple per-service Alembic migrations and fix cross-DB queries
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 1m19s

Each service (blog, market, cart, events, federation, account) now owns
its own database schema with independent Alembic migrations. Removes the
monolithic shared/alembic/ that ran all migrations against a single DB.

- Add per-service alembic.ini, env.py, and 0001_initial.py migrations
- Add shared/db/alembic_env.py helper with table-name filtering
- Fix cross-DB FK in blog/models/snippet.py (users lives in db_account)
- Fix cart_impl.py cross-DB queries: fetch products and market_places
  via internal data endpoints instead of direct SQL joins
- Fix blog ghost_sync to fetch page_configs from cart via data endpoint
- Add products-by-ids and page-config-ensure data endpoints
- Update all entrypoint.sh to create own DB and run own migrations
- Cart now uses db_cart instead of db_market
- Add docker-compose.dev.yml, dev.sh for local development
- CI deploys both rose-ash swarm stack and rose-ash-dev compose stack
- Fix Quart namespace package crash (root_path in factory.py)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-26 12:07:24 +00:00
parent bde2fd73b8
commit e65bd41ebe
77 changed files with 2405 additions and 2335 deletions

35
cart/alembic.ini Normal file
View File

@@ -0,0 +1,35 @@
[alembic]
script_location = alembic
sqlalchemy.url =
[loggers]
keys = root,sqlalchemy,alembic
[handlers]
keys = console
[formatters]
keys = generic
[logger_root]
level = WARN
handlers = console
[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

14
cart/alembic/env.py Normal file
View File

@@ -0,0 +1,14 @@
from alembic import context
from shared.db.alembic_env import run_alembic
MODELS = [
"shared.models.market", # CartItem lives here
"shared.models.order",
"shared.models.page_config",
]
TABLES = frozenset({
"cart_items", "orders", "order_items", "page_configs",
})
run_alembic(context.config, MODELS, TABLES)

View File

@@ -0,0 +1,92 @@
"""Initial cart tables
Revision ID: cart_0001
Revises: None
Create Date: 2026-02-26
"""
import sqlalchemy as sa
from alembic import op
revision = "cart_0001"
down_revision = None
branch_labels = None
depends_on = None
def _table_exists(conn, name):
result = conn.execute(sa.text(
"SELECT 1 FROM information_schema.tables WHERE table_schema='public' AND table_name=:t"
), {"t": name})
return result.scalar() is not None
def upgrade():
if _table_exists(op.get_bind(), "orders"):
return
op.create_table(
"page_configs",
sa.Column("id", sa.Integer, primary_key=True, autoincrement=True),
sa.Column("container_type", sa.String(32), nullable=False, server_default="page"),
sa.Column("container_id", sa.Integer, nullable=False),
sa.Column("features", sa.JSON, nullable=False, server_default="{}"),
sa.Column("sumup_merchant_code", sa.String(64), nullable=True),
sa.Column("sumup_api_key", sa.Text, nullable=True),
sa.Column("sumup_checkout_prefix", sa.String(64), nullable=True),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
sa.Column("deleted_at", sa.DateTime(timezone=True), nullable=True),
)
op.create_table(
"cart_items",
sa.Column("id", sa.Integer, primary_key=True, autoincrement=True),
sa.Column("user_id", sa.Integer, nullable=True),
sa.Column("session_id", sa.String(128), nullable=True),
sa.Column("product_id", sa.Integer, nullable=False),
sa.Column("quantity", sa.Integer, nullable=False, server_default="1"),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
sa.Column("market_place_id", sa.Integer, nullable=True, index=True),
sa.Column("deleted_at", sa.DateTime(timezone=True), nullable=True),
)
op.create_index("ix_cart_items_user_product", "cart_items", ["user_id", "product_id"])
op.create_index("ix_cart_items_session_product", "cart_items", ["session_id", "product_id"])
op.create_table(
"orders",
sa.Column("id", sa.Integer, primary_key=True, autoincrement=True),
sa.Column("user_id", sa.Integer, nullable=True),
sa.Column("session_id", sa.String(64), nullable=True, index=True),
sa.Column("page_config_id", sa.Integer, nullable=True, index=True),
sa.Column("status", sa.String(32), nullable=False, server_default="pending"),
sa.Column("currency", sa.String(16), nullable=False),
sa.Column("total_amount", sa.Numeric(12, 2), nullable=False),
sa.Column("description", sa.Text, nullable=True, index=True),
sa.Column("sumup_reference", sa.String(255), nullable=True, index=True),
sa.Column("sumup_checkout_id", sa.String(128), nullable=True, index=True),
sa.Column("sumup_status", sa.String(32), nullable=True),
sa.Column("sumup_hosted_url", sa.Text, nullable=True),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
)
op.create_table(
"order_items",
sa.Column("id", sa.Integer, primary_key=True, autoincrement=True),
sa.Column("order_id", sa.Integer, sa.ForeignKey("orders.id", ondelete="CASCADE"), nullable=False),
sa.Column("product_id", sa.Integer, nullable=False),
sa.Column("product_title", sa.String(512), nullable=True),
sa.Column("quantity", sa.Integer, nullable=False),
sa.Column("unit_price", sa.Numeric(12, 2), nullable=False),
sa.Column("currency", sa.String(16), nullable=False),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
)
def downgrade():
op.drop_table("order_items")
op.drop_table("orders")
op.drop_table("cart_items")
op.drop_table("page_configs")

View File

@@ -42,4 +42,39 @@ def register() -> Blueprint:
_handlers["cart-summary"] = _cart_summary
# --- page-config-ensure ---
async def _page_config_ensure():
"""Get or create a PageConfig for a container_type + container_id."""
from sqlalchemy import select
from shared.models.page_config import PageConfig
container_type = request.args.get("container_type", "page")
container_id = request.args.get("container_id", type=int)
if container_id is None:
return {"error": "container_id required"}, 400
row = (await g.s.execute(
select(PageConfig).where(
PageConfig.container_type == container_type,
PageConfig.container_id == container_id,
)
)).scalar_one_or_none()
if row is None:
row = PageConfig(
container_type=container_type,
container_id=container_id,
features={},
)
g.s.add(row)
await g.s.flush()
return {
"id": row.id,
"container_type": row.container_type,
"container_id": row.container_id,
}
_handlers["page-config-ensure"] = _page_config_ensure
return bp

40
cart/entrypoint.sh Normal file → Executable file
View File

@@ -10,8 +10,34 @@ if [[ -n "${DATABASE_HOST:-}" && -n "${DATABASE_PORT:-}" ]]; then
done
fi
# NOTE: Cart app does NOT run Alembic migrations.
# Migrations are managed by the blog app which owns the shared database schema.
# Create own database + run own migrations
if [[ "${RUN_MIGRATIONS:-}" == "true" && -n "${ALEMBIC_DATABASE_URL:-}" ]]; then
python3 -c "
import os, re
url = os.environ['ALEMBIC_DATABASE_URL']
m = re.match(r'postgresql\+\w+://([^:]+):([^@]+)@([^:]+):(\d+)/(.+)', url)
if not m:
print('Could not parse ALEMBIC_DATABASE_URL, skipping DB creation')
exit(0)
user, password, host, port, dbname = m.groups()
import psycopg
conn = psycopg.connect(
f'postgresql://{user}:{password}@{host}:{port}/postgres',
autocommit=True,
)
cur = conn.execute('SELECT 1 FROM pg_database WHERE datname = %s', (dbname,))
if not cur.fetchone():
conn.execute(f'CREATE DATABASE {dbname}')
print(f'Created database {dbname}')
else:
print(f'Database {dbname} already exists')
conn.close()
" || echo "DB creation failed (non-fatal), continuing..."
echo "Running cart Alembic migrations..."
(cd cart && alembic upgrade head)
fi
# Clear Redis page cache on deploy
if [[ -n "${REDIS_URL:-}" && "${REDIS_URL}" != "no" ]]; then
@@ -25,5 +51,11 @@ print('Redis cache cleared.')
fi
# Start the app
echo "Starting Hypercorn (${APP_MODULE:-app:app})..."
PYTHONUNBUFFERED=1 exec hypercorn "${APP_MODULE:-app:app}" --bind 0.0.0.0:${PORT:-8000} --workers ${WORKERS:-2} --keep-alive 75
RELOAD_FLAG=""
if [[ "${RELOAD:-}" == "true" ]]; then
RELOAD_FLAG="--reload"
echo "Starting Hypercorn (${APP_MODULE:-app:app}) with auto-reload..."
else
echo "Starting Hypercorn (${APP_MODULE:-app:app})..."
fi
PYTHONUNBUFFERED=1 exec hypercorn "${APP_MODULE:-app:app}" --bind 0.0.0.0:${PORT:-8000} --workers ${WORKERS:-2} --keep-alive 75 ${RELOAD_FLAG}