budgit/internal/db/migrations/00011_index_audit_metadata_account_id.sql
juancwu da718427bd
All checks were successful
Deploy / build-and-deploy (push) Successful in 1m27s
feat: drop sqlite support
2026-05-04 00:29:45 +00:00

22 lines
912 B
SQL

-- +goose Up
-- +goose StatementBegin
-- The account-scoped activity feeds filter audit rows by metadata->>'account_id'.
-- A partial expression index is the right shape for this access pattern in
-- PostgreSQL 17: it is small (only the rows where the field exists), uses a
-- standard B-tree (cheap equality + ORDER BY created_at), and avoids the bloat
-- of a full GIN over the metadata document.
CREATE INDEX idx_space_audit_logs_account_id
ON space_audit_logs ((metadata->>'account_id'), created_at DESC)
WHERE action LIKE 'account.%';
CREATE INDEX idx_transaction_audit_logs_account_id
ON transaction_audit_logs ((metadata->>'account_id'), created_at DESC)
WHERE metadata ? 'account_id';
-- +goose StatementEnd
-- +goose Down
-- +goose StatementBegin
DROP INDEX IF EXISTS idx_space_audit_logs_account_id;
DROP INDEX IF EXISTS idx_transaction_audit_logs_account_id;
-- +goose StatementEnd