test_pg_columns_set_from_model.py 1.1 KB

1234567891011121314151617181920212223242526
  1. """_migrate_pg_columns_to_model takes each listed column's default from the model.
  2. It runs on PostgreSQL only (covered by scripts/check_postgres_upgrade.py); this
  3. checks that the list still matches the models, which a model change could break
  4. without any PostgreSQL at hand: a column without a server_default would be given
  5. DEFAULT None.
  6. """
  7. from sqlalchemy.dialects import postgresql
  8. from backend.app.core.database import _PG_COLUMNS_SET_FROM_MODEL
  9. from backend.tests.unit.test_code_quality import _load_all_models
  10. def test_every_listed_column_exists_and_has_a_server_default():
  11. tables = _load_all_models().metadata.tables
  12. dialect = postgresql.dialect()
  13. compiler = dialect.ddl_compiler(dialect, None)
  14. missing = []
  15. for table, column in _PG_COLUMNS_SET_FROM_MODEL:
  16. model_column = tables[table].c.get(column) if table in tables else None
  17. if model_column is None:
  18. missing.append(f"{table}.{column}: not in the models")
  19. elif compiler.get_column_default_string(model_column) is None:
  20. missing.append(f"{table}.{column}: no server_default")
  21. assert missing == []