Updates
This commit is contained in:
@@ -2,9 +2,19 @@ from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass, field
|
||||
|
||||
from sqlalchemy import MetaData, inspect, text
|
||||
from sqlalchemy import MetaData, bindparam, inspect, text
|
||||
from sqlalchemy.engine import Engine
|
||||
|
||||
HIDDEN_PRODUCT_CLIENTS = (
|
||||
"Bird Grits",
|
||||
"Chaff",
|
||||
"Hay & Straw",
|
||||
"Hunter Premium Produce",
|
||||
"Straight Grain",
|
||||
"Uncategorized",
|
||||
"Uncategorised",
|
||||
)
|
||||
|
||||
|
||||
TENANT_TABLES = {
|
||||
"client_users": None,
|
||||
@@ -88,6 +98,7 @@ def ensure_tenant_columns(engine: Engine) -> tuple[str, ...]:
|
||||
# introduced on the model. Each entry is (table, column, DDL fragment).
|
||||
_LEGACY_COLUMN_PATCHES: tuple[tuple[str, str, str], ...] = (
|
||||
("users", "password_hash", "VARCHAR(255)"),
|
||||
("products", "visible", "BOOLEAN NOT NULL DEFAULT TRUE"),
|
||||
)
|
||||
|
||||
|
||||
@@ -359,6 +370,24 @@ def sync_tenant_ids(engine: Engine) -> dict[str, int]:
|
||||
return synced_rows
|
||||
|
||||
|
||||
def sync_product_visibility(engine: Engine) -> int:
|
||||
if not _table_exists(engine, "products") or not _has_column(engine, "products", "visible"):
|
||||
return 0
|
||||
|
||||
with engine.begin() as connection:
|
||||
result = connection.execute(
|
||||
text(
|
||||
"""
|
||||
UPDATE products
|
||||
SET visible = FALSE
|
||||
WHERE client_name IN :hidden_clients
|
||||
AND (visible IS NULL OR visible != FALSE)
|
||||
"""
|
||||
).bindparams(bindparam("hidden_clients", value=HIDDEN_PRODUCT_CLIENTS, expanding=True))
|
||||
)
|
||||
return result.rowcount or 0
|
||||
|
||||
|
||||
def bootstrap_schema(engine: Engine, metadata: MetaData) -> MigrationReport:
|
||||
created_tables = ensure_metadata_tables(engine, metadata)
|
||||
added_columns = ensure_tenant_columns(engine) + ensure_legacy_columns(engine)
|
||||
|
||||
Reference in New Issue
Block a user