Skip to content

Add updated_at columns and Postgres triggers#379

Open
silentgeckoaudit3801 wants to merge 5 commits into
Quantarq:mainfrom
silentgeckoaudit3801:db/updated-at-triggers-229
Open

Add updated_at columns and Postgres triggers#379
silentgeckoaudit3801 wants to merge 5 commits into
Quantarq:mainfrom
silentgeckoaudit3801:db/updated-at-triggers-229

Conversation

@silentgeckoaudit3801

Copy link
Copy Markdown

Fixes #229.

This makes updated_at consistent for the audit-facing models and moves the timestamp update guarantee into Postgres:

  • adds updated_at columns to User, Position, and AirDrop models
  • adds an Alembic migration that backfills those columns with
    ow() defaults
  • installs a shared set_updated_at() trigger function for every table that exposes updated_at
  • covers the model columns and trigger migration wiring with focused static tests

I did not run local pytest/Alembic commands in this workspace because we are intentionally avoiding dependency installation and project toolchain execution.

def test_core_audit_models_have_updated_at_columns():
for class_name in ("User", "Position", "AirDrop"):
body = _class_body(class_name)
assert "updated_at = Column" in body
Comment thread quantara/web_app/tests/test_updated_at_triggers_static.py Fixed
Comment thread quantara/web_app/tests/test_updated_at_triggers_static.py Fixed
Comment thread quantara/web_app/tests/test_updated_at_triggers_static.py Fixed
for table_name in ("user", "position", "airdrop"):
assert f'op.add_column(\n table_name,' in MIGRATION
assert f'for table_name in ("user", "position", "airdrop"):' in MIGRATION
assert 'op.drop_column(table_name, "updated_at")' in MIGRATION


def test_migration_installs_triggers_for_all_updated_at_tables():
assert "CREATE OR REPLACE FUNCTION set_updated_at()" in MIGRATION

def test_migration_installs_triggers_for_all_updated_at_tables():
assert "CREATE OR REPLACE FUNCTION set_updated_at()" in MIGRATION
assert "NEW.updated_at = NOW();" in MIGRATION
"extra_deposits",
"event_outbox",
):
assert f'"{table_name}"' in MIGRATION
"event_outbox",
):
assert f'"{table_name}"' in MIGRATION
assert "CREATE TRIGGER" in MIGRATION
):
assert f'"{table_name}"' in MIGRATION
assert "CREATE TRIGGER" in MIGRATION
assert "EXECUTE FUNCTION set_updated_at();" in MIGRATION No newline at end of file
Comment thread quantara/web_app/tests/test_updated_at_triggers_static.py Fixed
for class_name in ("User", "Position", "AirDrop"):
body = _class_body(class_name)
assert "updated_at = Column" in body
assert "onupdate=func.now()" in body


def test_migration_adds_missing_updated_at_columns():
assert 'for table_name in ("user", "position", "airdrop"):' in MIGRATION
def test_migration_adds_missing_updated_at_columns():
assert 'for table_name in ("user", "position", "airdrop"):' in MIGRATION
for table_name in ("user", "position", "airdrop"):
assert f'op.add_column(\n table_name,' in MIGRATION
assert 'for table_name in ("user", "position", "airdrop"):' in MIGRATION
for table_name in ("user", "position", "airdrop"):
assert f'op.add_column(\n table_name,' in MIGRATION
assert table_name in MIGRATION
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

updated_at triggers + audit columns

2 participants