Spaces:
Runtime error
Runtime error
Greg Claude Sonnet 4.6 commited on
Commit ·
96577c8
1
Parent(s): 70a7dc0
fix(seed): scope demo seeder to is_demo accounts; never touch customers
Browse files- Adds Account.is_demo column (migration 022, backfilled for existing demo)
- seed_demo.py only operates on accounts where is_demo=True
- Existing demo accounts are fully idempotent (no field overwrites)
- Removes _clear_demo helper (used MySQL-specific syntax, was dead code)
- Integration test guards against future regressions
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
backend/alembic/versions/022_add_is_demo_to_accounts.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""add is_demo to accounts
|
| 2 |
+
|
| 3 |
+
Revision ID: 022
|
| 4 |
+
Revises: 021
|
| 5 |
+
Create Date: 2026-05-31
|
| 6 |
+
|
| 7 |
+
"""
|
| 8 |
+
from alembic import op
|
| 9 |
+
import sqlalchemy as sa
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
revision = "022"
|
| 13 |
+
down_revision = "021"
|
| 14 |
+
branch_labels = None
|
| 15 |
+
depends_on = None
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def upgrade() -> None:
|
| 19 |
+
op.add_column(
|
| 20 |
+
"accounts",
|
| 21 |
+
sa.Column("is_demo", sa.Boolean(), nullable=False, server_default=sa.text("false")),
|
| 22 |
+
)
|
| 23 |
+
# Backfill: mark the existing demo account (created by seed_demo.py) as is_demo=True
|
| 24 |
+
op.execute(
|
| 25 |
+
"UPDATE accounts SET is_demo = TRUE "
|
| 26 |
+
"WHERE organization_name = 'U.E. Colegio Bolivariano Talos'"
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def downgrade() -> None:
|
| 31 |
+
op.drop_column("accounts", "is_demo")
|
backend/app/models/account.py
CHANGED
|
@@ -16,5 +16,6 @@ class Account(Base):
|
|
| 16 |
checkin_mode = Column(String(20), nullable=False, server_default="manual")
|
| 17 |
self_checkin_enabled = Column(BigInteger, nullable=False, server_default="0")
|
| 18 |
is_active = Column(Boolean, nullable=False, server_default="1")
|
|
|
|
| 19 |
trial_end_date = Column(DateTime, nullable=True)
|
| 20 |
created_at = Column(DateTime, server_default=func.now())
|
|
|
|
| 16 |
checkin_mode = Column(String(20), nullable=False, server_default="manual")
|
| 17 |
self_checkin_enabled = Column(BigInteger, nullable=False, server_default="0")
|
| 18 |
is_active = Column(Boolean, nullable=False, server_default="1")
|
| 19 |
+
is_demo = Column(Boolean, nullable=False, server_default="false")
|
| 20 |
trial_end_date = Column(DateTime, nullable=True)
|
| 21 |
created_at = Column(DateTime, server_default=func.now())
|
backend/seed_demo.py
CHANGED
|
@@ -29,7 +29,6 @@ from datetime import datetime, date, time, timedelta
|
|
| 29 |
|
| 30 |
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
| 31 |
|
| 32 |
-
from sqlalchemy import text
|
| 33 |
from app.core.database import SessionLocal, engine, Base
|
| 34 |
from app.core.security import hash_password
|
| 35 |
from app.models import (
|
|
@@ -161,30 +160,34 @@ def _ensure_base(db):
|
|
| 161 |
|
| 162 |
pro = db.query(Plan).filter(Plan.name == "pro").first()
|
| 163 |
|
| 164 |
-
|
|
|
|
|
|
|
| 165 |
if not acc:
|
| 166 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
db.add(acc)
|
| 168 |
db.commit()
|
| 169 |
db.refresh(acc)
|
|
|
|
|
|
|
| 170 |
|
| 171 |
-
#
|
| 172 |
-
acc.organization_name = "U.E. Colegio Bolivariano Talos"
|
| 173 |
-
acc.address = "Av. Bolívar, Caracas, Venezuela"
|
| 174 |
-
acc.director_name = "Lcda. Carmen Oropeza"
|
| 175 |
-
acc.plan_id = pro.id
|
| 176 |
-
acc.tolerance = 15
|
| 177 |
-
acc.checkin_mode = "facial"
|
| 178 |
-
acc.self_checkin_enabled = 0
|
| 179 |
-
acc.is_active = True
|
| 180 |
-
# Horario de jornada (Configuración → Horario por Día). Alimenta la detección
|
| 181 |
-
# en vivo de entrada tardía / salida temprana/tardía en el check-in del kiosko.
|
| 182 |
-
acc.schedule = {
|
| 183 |
-
d: {"entry": "07:00", "exit": "14:00"}
|
| 184 |
-
for d in ["monday", "tuesday", "wednesday", "thursday", "friday"]
|
| 185 |
-
}
|
| 186 |
-
db.commit()
|
| 187 |
-
|
| 188 |
if db.query(User).filter(User.email == "admin@talos.com").count() == 0:
|
| 189 |
db.add(User(account_id=acc.id, name="Admin", lastname="Sistema", email="admin@talos.com",
|
| 190 |
password_hash=hash_password("admin123"), role="admin", is_active="1"))
|
|
@@ -195,38 +198,18 @@ def _ensure_base(db):
|
|
| 195 |
return acc
|
| 196 |
|
| 197 |
|
| 198 |
-
def
|
| 199 |
-
db.execute(text("SET FOREIGN_KEY_CHECKS = 0"))
|
| 200 |
-
for tbl in [Attendance, EntranceLog, SectionSchedule, Enrollment, TeacherSubject,
|
| 201 |
-
TeacherSection, SectionSubject, Holiday, Section, Subject]:
|
| 202 |
-
db.query(tbl).filter(tbl.account_id == account_id).delete(synchronize_session=False)
|
| 203 |
-
db.query(User).filter(
|
| 204 |
-
User.account_id == account_id, ~User.id.in_(preserve_ids)
|
| 205 |
-
).delete(synchronize_session=False)
|
| 206 |
-
db.execute(text("SET FOREIGN_KEY_CHECKS = 1"))
|
| 207 |
-
db.commit()
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
def seed(reset=False):
|
| 211 |
db = SessionLocal()
|
| 212 |
try:
|
| 213 |
Base.metadata.create_all(bind=engine)
|
| 214 |
acc = _ensure_base(db)
|
| 215 |
account_id = acc.id
|
| 216 |
|
| 217 |
-
admin = db.query(User).filter(User.account_id == account_id, User.role == "admin").first()
|
| 218 |
-
dev = db.query(User).filter(User.account_id == account_id, User.role == "developer").first()
|
| 219 |
-
preserve_ids = [u.id for u in (admin, dev) if u]
|
| 220 |
-
|
| 221 |
student_count = db.query(User).filter(User.account_id == account_id, User.role == "student").count()
|
| 222 |
-
if student_count > 0
|
| 223 |
-
print(f"Demo ya sembrado ({student_count} estudiantes).
|
| 224 |
return
|
| 225 |
|
| 226 |
-
if reset:
|
| 227 |
-
_clear_demo(db, account_id, preserve_ids)
|
| 228 |
-
print("✓ Datos demo previos eliminados.")
|
| 229 |
-
|
| 230 |
test_hash = hash_password("test123")
|
| 231 |
|
| 232 |
# --- Subjects ---
|
|
@@ -488,5 +471,4 @@ def seed(reset=False):
|
|
| 488 |
|
| 489 |
|
| 490 |
if __name__ == "__main__":
|
| 491 |
-
|
| 492 |
-
seed(reset=force)
|
|
|
|
| 29 |
|
| 30 |
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
| 31 |
|
|
|
|
| 32 |
from app.core.database import SessionLocal, engine, Base
|
| 33 |
from app.core.security import hash_password
|
| 34 |
from app.models import (
|
|
|
|
| 160 |
|
| 161 |
pro = db.query(Plan).filter(Plan.name == "pro").first()
|
| 162 |
|
| 163 |
+
# Find the demo account by its explicit marker (added in migration 022).
|
| 164 |
+
acc = db.query(Account).filter(Account.is_demo.is_(True)).first()
|
| 165 |
+
|
| 166 |
if not acc:
|
| 167 |
+
# First boot: create the demo account from scratch with all canned values.
|
| 168 |
+
acc = Account(
|
| 169 |
+
organization_name="U.E. Colegio Bolivariano Talos",
|
| 170 |
+
address="Av. Bolívar, Caracas, Venezuela",
|
| 171 |
+
director_name="Lcda. Carmen Oropeza",
|
| 172 |
+
category="school",
|
| 173 |
+
plan_id=pro.id,
|
| 174 |
+
tolerance=15,
|
| 175 |
+
checkin_mode="facial",
|
| 176 |
+
self_checkin_enabled=0,
|
| 177 |
+
is_active=True,
|
| 178 |
+
is_demo=True,
|
| 179 |
+
schedule={
|
| 180 |
+
d: {"entry": "07:00", "exit": "14:00"}
|
| 181 |
+
for d in ["monday", "tuesday", "wednesday", "thursday", "friday"]
|
| 182 |
+
},
|
| 183 |
+
)
|
| 184 |
db.add(acc)
|
| 185 |
db.commit()
|
| 186 |
db.refresh(acc)
|
| 187 |
+
# else: demo account already exists — DO NOT overwrite any field.
|
| 188 |
+
# Admins may have customized values via the UI; respect them.
|
| 189 |
|
| 190 |
+
# User creation (unchanged — these have their own existence checks per email):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
if db.query(User).filter(User.email == "admin@talos.com").count() == 0:
|
| 192 |
db.add(User(account_id=acc.id, name="Admin", lastname="Sistema", email="admin@talos.com",
|
| 193 |
password_hash=hash_password("admin123"), role="admin", is_active="1"))
|
|
|
|
| 198 |
return acc
|
| 199 |
|
| 200 |
|
| 201 |
+
def seed():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
db = SessionLocal()
|
| 203 |
try:
|
| 204 |
Base.metadata.create_all(bind=engine)
|
| 205 |
acc = _ensure_base(db)
|
| 206 |
account_id = acc.id
|
| 207 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
student_count = db.query(User).filter(User.account_id == account_id, User.role == "student").count()
|
| 209 |
+
if student_count > 0:
|
| 210 |
+
print(f"Demo ya sembrado ({student_count} estudiantes). Sin cambios.")
|
| 211 |
return
|
| 212 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
test_hash = hash_password("test123")
|
| 214 |
|
| 215 |
# --- Subjects ---
|
|
|
|
| 471 |
|
| 472 |
|
| 473 |
if __name__ == "__main__":
|
| 474 |
+
seed()
|
|
|
backend/tests/test_seed_demo_scoping.py
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Integration test: seed_demo.py only touches accounts with is_demo=True."""
|
| 2 |
+
import os
|
| 3 |
+
import pytest
|
| 4 |
+
from sqlalchemy.orm import Session
|
| 5 |
+
|
| 6 |
+
# These imports require the DATABASE_URL to point at a live Postgres (docker-compose)
|
| 7 |
+
from app.core.database import engine, SessionLocal, Base
|
| 8 |
+
from app.models.account import Account
|
| 9 |
+
from app.models.plan import Plan
|
| 10 |
+
from app.models.user import User
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
@pytest.fixture
|
| 14 |
+
def db():
|
| 15 |
+
Base.metadata.create_all(bind=engine)
|
| 16 |
+
session = SessionLocal()
|
| 17 |
+
yield session
|
| 18 |
+
session.close()
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def test_seed_does_not_touch_non_demo_account(db: Session):
|
| 22 |
+
"""A non-demo account's fields must survive a seed_demo run unchanged."""
|
| 23 |
+
# Ensure a plan exists (seed_demo needs one)
|
| 24 |
+
plan = db.query(Plan).filter(Plan.name == "pro").first()
|
| 25 |
+
if not plan:
|
| 26 |
+
pytest.skip("plan 'pro' not present — seed must run once first")
|
| 27 |
+
|
| 28 |
+
# Create a fake customer account
|
| 29 |
+
customer = Account(
|
| 30 |
+
organization_name="Colegio San Mateo",
|
| 31 |
+
address="Av. Real, Maracaibo",
|
| 32 |
+
director_name="Lic. Pedro Ramirez",
|
| 33 |
+
category="school",
|
| 34 |
+
plan_id=plan.id,
|
| 35 |
+
tolerance=30,
|
| 36 |
+
checkin_mode="manual",
|
| 37 |
+
is_demo=False,
|
| 38 |
+
)
|
| 39 |
+
db.add(customer)
|
| 40 |
+
db.commit()
|
| 41 |
+
db.refresh(customer)
|
| 42 |
+
customer_id = customer.id
|
| 43 |
+
|
| 44 |
+
# Snapshot the customer's fields
|
| 45 |
+
expected = {
|
| 46 |
+
"organization_name": "Colegio San Mateo",
|
| 47 |
+
"address": "Av. Real, Maracaibo",
|
| 48 |
+
"director_name": "Lic. Pedro Ramirez",
|
| 49 |
+
"tolerance": 30,
|
| 50 |
+
"checkin_mode": "manual",
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
# Run the seeder (the production code path)
|
| 54 |
+
from seed_demo import seed
|
| 55 |
+
seed()
|
| 56 |
+
|
| 57 |
+
# Re-fetch and assert nothing changed on the customer
|
| 58 |
+
db.expire_all()
|
| 59 |
+
after = db.query(Account).filter(Account.id == customer_id).one()
|
| 60 |
+
for key, value in expected.items():
|
| 61 |
+
assert getattr(after, key) == value, (
|
| 62 |
+
f"seed_demo touched a non-demo account! field={key} "
|
| 63 |
+
f"expected={value!r} got={getattr(after, key)!r}"
|
| 64 |
+
)
|
| 65 |
+
assert after.is_demo is False
|
| 66 |
+
|
| 67 |
+
# Cleanup
|
| 68 |
+
db.query(Account).filter(Account.id == customer_id).delete()
|
| 69 |
+
db.commit()
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def test_seed_creates_demo_with_is_demo_flag(db: Session):
|
| 73 |
+
"""After seed runs, exactly one account has is_demo=True."""
|
| 74 |
+
from seed_demo import seed
|
| 75 |
+
seed()
|
| 76 |
+
|
| 77 |
+
demos = db.query(Account).filter(Account.is_demo.is_(True)).all()
|
| 78 |
+
assert len(demos) == 1, f"expected exactly 1 demo account, found {len(demos)}"
|
| 79 |
+
assert demos[0].organization_name == "U.E. Colegio Bolivariano Talos"
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
def test_seed_is_fully_idempotent_on_demo(db: Session):
|
| 83 |
+
"""Running seed twice should not change anything (no field overwrites)."""
|
| 84 |
+
from seed_demo import seed
|
| 85 |
+
seed()
|
| 86 |
+
demo = db.query(Account).filter(Account.is_demo.is_(True)).first()
|
| 87 |
+
assert demo is not None
|
| 88 |
+
|
| 89 |
+
# Mutate a field on the demo manually (simulates an admin changing it)
|
| 90 |
+
demo.tolerance = 99
|
| 91 |
+
db.commit()
|
| 92 |
+
db.refresh(demo)
|
| 93 |
+
|
| 94 |
+
# Run seed again
|
| 95 |
+
seed()
|
| 96 |
+
|
| 97 |
+
# The mutation should persist (seed must NOT overwrite)
|
| 98 |
+
db.expire_all()
|
| 99 |
+
demo_after = db.query(Account).filter(Account.is_demo.is_(True)).first()
|
| 100 |
+
assert demo_after.tolerance == 99, (
|
| 101 |
+
"seed_demo overwrote a field on an existing demo account — must be fully idempotent"
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
# Restore for next test runs
|
| 105 |
+
demo_after.tolerance = 15
|
| 106 |
+
db.commit()
|