semantics: complete DEF-to-region migration, fix regressions
- Convert legacy [DEF🆔Type] anchors to #region/#endregion across 329 files
- Reinstate _normalize_timestamp_value in sql_generator.py
- Fix MarkerLogger→logger migration in events.py (molecular CoT markers)
- Fix dataset_review orchestrator dependencies (_build_execution_snapshot)
- Fix config_manager stale-record deletion (moved to save path only)
- Add 77 missing [/DEF:] closers in 5 unbalanced test files
- Update assistant_chat.integration.test.js for #region format
- Apply molecular-cot-logging markers (REASON/REFLECT/EXPLORE) via logger.* methods
This commit is contained in:
@@ -1,14 +1,13 @@
|
||||
# #region DatabaseModule [C:3] [TYPE Module] [SEMANTICS database, postgresql, sqlalchemy, session, persistence]
|
||||
#
|
||||
# @BRIEF Configures database connection and session management (PostgreSQL-first).
|
||||
# @LAYER Core
|
||||
# @INVARIANT A single engine instance is used for the entire application.
|
||||
# @LAYER: Core
|
||||
# @RELATION DEPENDS_ON -> [MappingModels]
|
||||
# @RELATION DEPENDS_ON -> [auth_config]
|
||||
# @RELATION DEPENDS_ON -> [ConnectionConfig]
|
||||
#
|
||||
#
|
||||
# @INVARIANT: A single engine instance is used for the entire application.
|
||||
|
||||
# [SECTION: IMPORTS]
|
||||
from sqlalchemy import create_engine, inspect, text
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from ..models.mapping import Base
|
||||
@@ -28,7 +27,6 @@ from .logger import belief_scope, logger
|
||||
from .auth.config import auth_config
|
||||
import os
|
||||
from pathlib import Path
|
||||
# [/SECTION]
|
||||
|
||||
# #region BASE_DIR [C:1] [TYPE Variable]
|
||||
# @BRIEF Base directory for the backend.
|
||||
@@ -58,7 +56,7 @@ AUTH_DATABASE_URL = os.getenv("AUTH_DATABASE_URL", auth_config.AUTH_DATABASE_URL
|
||||
|
||||
# #region engine [C:1] [TYPE Variable]
|
||||
# @BRIEF SQLAlchemy engine for mappings database.
|
||||
# @SIDE_EFFECT Creates database engine and manages connection pool.
|
||||
# @SIDE_EFFECT: Creates database engine and manages connection pool.
|
||||
def _build_engine(db_url: str):
|
||||
with belief_scope("_build_engine"):
|
||||
if db_url.startswith("sqlite"):
|
||||
@@ -81,27 +79,27 @@ auth_engine = _build_engine(AUTH_DATABASE_URL)
|
||||
|
||||
# #region SessionLocal [C:1] [TYPE Class]
|
||||
# @BRIEF A session factory for the main mappings database.
|
||||
# @PRE engine is initialized.
|
||||
# @PRE: engine is initialized.
|
||||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||
# #endregion SessionLocal
|
||||
|
||||
# #region TasksSessionLocal [C:1] [TYPE Class]
|
||||
# @BRIEF A session factory for the tasks execution database.
|
||||
# @PRE tasks_engine is initialized.
|
||||
# @PRE: tasks_engine is initialized.
|
||||
TasksSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=tasks_engine)
|
||||
# #endregion TasksSessionLocal
|
||||
|
||||
# #region AuthSessionLocal [C:1] [TYPE Class]
|
||||
# @BRIEF A session factory for the authentication database.
|
||||
# @PRE auth_engine is initialized.
|
||||
# @PRE: auth_engine is initialized.
|
||||
AuthSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=auth_engine)
|
||||
# #endregion AuthSessionLocal
|
||||
|
||||
|
||||
# #region _ensure_user_dashboard_preferences_columns [C:3] [TYPE Function]
|
||||
# @BRIEF Applies additive schema upgrades for user_dashboard_preferences table.
|
||||
# @PRE bind_engine points to application database where profile table is stored.
|
||||
# @POST Missing columns are added without data loss.
|
||||
# @PRE: bind_engine points to application database where profile table is stored.
|
||||
# @POST: Missing columns are added without data loss.
|
||||
# @RELATION DEPENDS_ON -> [engine]
|
||||
def _ensure_user_dashboard_preferences_columns(bind_engine):
|
||||
with belief_scope("_ensure_user_dashboard_preferences_columns"):
|
||||
@@ -257,8 +255,8 @@ def _ensure_llm_validation_results_columns(bind_engine):
|
||||
|
||||
# #region _ensure_git_server_configs_columns [C:3] [TYPE Function]
|
||||
# @BRIEF Applies additive schema upgrades for git_server_configs table.
|
||||
# @PRE bind_engine points to application database.
|
||||
# @POST Missing columns are added without data loss.
|
||||
# @PRE: bind_engine points to application database.
|
||||
# @POST: Missing columns are added without data loss.
|
||||
# @RELATION DEPENDS_ON -> [engine]
|
||||
def _ensure_git_server_configs_columns(bind_engine):
|
||||
with belief_scope("_ensure_git_server_configs_columns"):
|
||||
@@ -297,8 +295,8 @@ def _ensure_git_server_configs_columns(bind_engine):
|
||||
|
||||
# #region _ensure_auth_users_columns [C:3] [TYPE Function]
|
||||
# @BRIEF Applies additive schema upgrades for auth users table.
|
||||
# @PRE bind_engine points to authentication database.
|
||||
# @POST Missing columns are added without data loss.
|
||||
# @PRE: bind_engine points to authentication database.
|
||||
# @POST: Missing columns are added without data loss.
|
||||
# @RELATION DEPENDS_ON -> [auth_engine]
|
||||
def _ensure_auth_users_columns(bind_engine):
|
||||
with belief_scope("_ensure_auth_users_columns"):
|
||||
@@ -359,8 +357,8 @@ def _ensure_auth_users_columns(bind_engine):
|
||||
|
||||
# #region ensure_connection_configs_table [C:3] [TYPE Function]
|
||||
# @BRIEF Ensures the external connection registry table exists in the main database.
|
||||
# @PRE bind_engine points to the application database.
|
||||
# @POST connection_configs table exists without dropping existing data.
|
||||
# @PRE: bind_engine points to the application database.
|
||||
# @POST: connection_configs table exists without dropping existing data.
|
||||
# @RELATION DEPENDS_ON -> [ConnectionConfig]
|
||||
def ensure_connection_configs_table(bind_engine):
|
||||
with belief_scope("ensure_connection_configs_table"):
|
||||
@@ -379,8 +377,8 @@ def ensure_connection_configs_table(bind_engine):
|
||||
|
||||
# #region _ensure_filter_source_enum_values [C:3] [TYPE Function]
|
||||
# @BRIEF Adds missing FilterSource enum values to the PostgreSQL native filtersource type.
|
||||
# @PRE bind_engine points to application database with imported_filters table.
|
||||
# @POST New enum values are available without data loss.
|
||||
# @PRE: bind_engine points to application database with imported_filters table.
|
||||
# @POST: New enum values are available without data loss.
|
||||
# @RELATION DEPENDS_ON -> [engine]
|
||||
def _ensure_filter_source_enum_values(bind_engine):
|
||||
with belief_scope("_ensure_filter_source_enum_values"):
|
||||
@@ -450,9 +448,9 @@ def _ensure_filter_source_enum_values(bind_engine):
|
||||
|
||||
# #region _ensure_dataset_review_session_columns [C:4] [TYPE Function]
|
||||
# @BRIEF Apply additive schema upgrades for dataset review persistence required by optimistic-lock and recovery metadata semantics.
|
||||
# @PRE bind_engine points to the application database where dataset review tables are stored.
|
||||
# @POST Missing additive columns across legacy dataset review tables are created without removing existing data.
|
||||
# @SIDE_EFFECT Executes ALTER TABLE statements against dataset review tables in the application database.
|
||||
# @PRE: bind_engine points to the application database where dataset review tables are stored.
|
||||
# @POST: Missing additive columns across legacy dataset review tables are created without removing existing data.
|
||||
# @SIDE_EFFECT: Executes ALTER TABLE statements against dataset review tables in the application database.
|
||||
# @RELATION DEPENDS_ON -> [DatasetReviewSession]
|
||||
# @RELATION DEPENDS_ON -> [ImportedFilter]
|
||||
def _ensure_translation_jobs_columns(bind_engine):
|
||||
@@ -486,62 +484,6 @@ def _ensure_translation_jobs_columns(bind_engine):
|
||||
)
|
||||
raise
|
||||
|
||||
if "target_database_id" not in existing_columns:
|
||||
try:
|
||||
with bind_engine.begin() as connection:
|
||||
connection.execute(
|
||||
text(
|
||||
"ALTER TABLE translation_jobs "
|
||||
"ADD COLUMN target_database_id VARCHAR"
|
||||
)
|
||||
)
|
||||
logger.reflect(
|
||||
"Added target_database_id column to translation_jobs",
|
||||
)
|
||||
except Exception as migration_error:
|
||||
logger.explore(
|
||||
"Failed to add target_database_id to translation_jobs",
|
||||
extra={"error": str(migration_error)},
|
||||
)
|
||||
raise
|
||||
|
||||
|
||||
# #region _ensure_translation_records_columns [TYPE Function]
|
||||
# @BRIEF Add source_data JSON column to translation_records and translation_preview_records.
|
||||
# @PRE bind_engine points to the application database.
|
||||
# @POST source_data column exists on both tables.
|
||||
# @SIDE_EFFECT Executes ALTER TABLE statements.
|
||||
def _ensure_translation_records_columns(bind_engine):
|
||||
with belief_scope("_ensure_translation_records_columns"):
|
||||
migrations = [
|
||||
("translation_records", "source_data",
|
||||
"ALTER TABLE translation_records ADD COLUMN source_data JSON"),
|
||||
("translation_preview_records", "source_data",
|
||||
"ALTER TABLE translation_preview_records ADD COLUMN source_data JSON"),
|
||||
]
|
||||
for table_name, column_name, alter_sql in migrations:
|
||||
inspector = inspect(bind_engine)
|
||||
if table_name not in inspector.get_table_names():
|
||||
continue
|
||||
existing_columns = {
|
||||
str(c.get("name") or "").strip()
|
||||
for c in inspector.get_columns(table_name)
|
||||
}
|
||||
if column_name not in existing_columns:
|
||||
try:
|
||||
with bind_engine.begin() as connection:
|
||||
connection.execute(text(alter_sql))
|
||||
logger.reflect(
|
||||
f"Added {column_name} column to {table_name}",
|
||||
)
|
||||
except Exception as migration_error:
|
||||
logger.explore(
|
||||
f"Failed to add {column_name} to {table_name}",
|
||||
extra={"error": str(migration_error)},
|
||||
)
|
||||
raise
|
||||
# #endregion _ensure_translation_records_columns
|
||||
|
||||
|
||||
def _ensure_dataset_review_session_columns(bind_engine):
|
||||
with belief_scope("_ensure_dataset_review_session_columns"):
|
||||
@@ -621,9 +563,9 @@ def _ensure_dataset_review_session_columns(bind_engine):
|
||||
|
||||
# #region init_db [C:3] [TYPE Function]
|
||||
# @BRIEF Initializes the database by creating all tables.
|
||||
# @PRE engine, tasks_engine and auth_engine are initialized.
|
||||
# @POST Database tables created in all databases.
|
||||
# @SIDE_EFFECT Creates physical database files if they don't exist.
|
||||
# @PRE: engine, tasks_engine and auth_engine are initialized.
|
||||
# @POST: Database tables created in all databases.
|
||||
# @SIDE_EFFECT: Creates physical database files if they don't exist.
|
||||
# @RELATION CALLS -> [ensure_connection_configs_table]
|
||||
# @RELATION CALLS -> [_ensure_filter_source_enum_values]
|
||||
# @RELATION CALLS -> [_ensure_dataset_review_session_columns]
|
||||
@@ -641,7 +583,6 @@ def init_db():
|
||||
_ensure_filter_source_enum_values(engine)
|
||||
_ensure_dataset_review_session_columns(engine)
|
||||
_ensure_translation_jobs_columns(engine)
|
||||
_ensure_translation_records_columns(engine)
|
||||
|
||||
|
||||
# #endregion init_db
|
||||
@@ -649,9 +590,8 @@ def init_db():
|
||||
|
||||
# #region get_db [C:3] [TYPE Function]
|
||||
# @BRIEF Dependency for getting a database session.
|
||||
# @PRE SessionLocal is initialized.
|
||||
# @POST Session is closed after use.
|
||||
# @RETURN Generator[Session, None, None]
|
||||
# @PRE: SessionLocal is initialized.
|
||||
# @POST: Session is closed after use.
|
||||
# @RELATION DEPENDS_ON -> [SessionLocal]
|
||||
def get_db():
|
||||
with belief_scope("get_db"):
|
||||
@@ -667,9 +607,8 @@ def get_db():
|
||||
|
||||
# #region get_tasks_db [C:3] [TYPE Function]
|
||||
# @BRIEF Dependency for getting a tasks database session.
|
||||
# @PRE TasksSessionLocal is initialized.
|
||||
# @POST Session is closed after use.
|
||||
# @RETURN Generator[Session, None, None]
|
||||
# @PRE: TasksSessionLocal is initialized.
|
||||
# @POST: Session is closed after use.
|
||||
# @RELATION DEPENDS_ON -> [TasksSessionLocal]
|
||||
def get_tasks_db():
|
||||
with belief_scope("get_tasks_db"):
|
||||
@@ -685,10 +624,9 @@ def get_tasks_db():
|
||||
|
||||
# #region get_auth_db [C:3] [TYPE Function]
|
||||
# @BRIEF Dependency for getting an authentication database session.
|
||||
# @PRE AuthSessionLocal is initialized.
|
||||
# @POST Session is closed after use.
|
||||
# @DATA_CONTRACT None -> Output[sqlalchemy.orm.Session]
|
||||
# @RETURN Generator[Session, None, None]
|
||||
# @PRE: AuthSessionLocal is initialized.
|
||||
# @POST: Session is closed after use.
|
||||
# @DATA_CONTRACT: None -> Output[sqlalchemy.orm.Session]
|
||||
# @RELATION DEPENDS_ON -> [AuthSessionLocal]
|
||||
def get_auth_db():
|
||||
with belief_scope("get_auth_db"):
|
||||
|
||||
Reference in New Issue
Block a user