From 948dac34fd87215429d01d63b90edd34e593f2b6 Mon Sep 17 00:00:00 2001 From: Denozordec Date: Fri, 12 Jun 2026 13:19:27 +0700 Subject: [PATCH] feat(db): add maintenance_policy migration 000025 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Добавлены таблицы maintenance_policy и maintenance_policy_config_audit для postgres и sqlite; в postgres_maintenance_audit — колонка policy_id. Co-authored-by: Cursor --- .../000025_maintenance_policies.down.sql | 7 +++ .../000025_maintenance_policies.up.sql | 47 +++++++++++++++++++ .../000025_maintenance_policies.down.sql | 5 ++ .../sqlite/000025_maintenance_policies.up.sql | 29 ++++++++++++ 4 files changed, 88 insertions(+) create mode 100644 migrations/postgres/000025_maintenance_policies.down.sql create mode 100644 migrations/postgres/000025_maintenance_policies.up.sql create mode 100644 migrations/sqlite/000025_maintenance_policies.down.sql create mode 100644 migrations/sqlite/000025_maintenance_policies.up.sql diff --git a/migrations/postgres/000025_maintenance_policies.down.sql b/migrations/postgres/000025_maintenance_policies.down.sql new file mode 100644 index 0000000..9eda062 --- /dev/null +++ b/migrations/postgres/000025_maintenance_policies.down.sql @@ -0,0 +1,7 @@ +DROP INDEX IF EXISTS idx_postgres_maintenance_audit_policy; + +ALTER TABLE postgres_maintenance_audit DROP COLUMN IF EXISTS policy_id; + +DROP TABLE IF EXISTS maintenance_policy_config_audit; + +DROP TABLE IF EXISTS maintenance_policy; diff --git a/migrations/postgres/000025_maintenance_policies.up.sql b/migrations/postgres/000025_maintenance_policies.up.sql new file mode 100644 index 0000000..3017e25 --- /dev/null +++ b/migrations/postgres/000025_maintenance_policies.up.sql @@ -0,0 +1,47 @@ +CREATE TABLE IF NOT EXISTS maintenance_policy ( + id TEXT PRIMARY KEY, + name TEXT NOT NULL, + table_name TEXT NOT NULL, + condition_sql TEXT NOT NULL DEFAULT 'true', + retention_period_sec INTEGER, + max_rows INTEGER, + vacuum_strategy TEXT NOT NULL DEFAULT 'none', + schedule_cron TEXT NOT NULL, + enabled BOOLEAN NOT NULL DEFAULT true, + dry_run_enabled BOOLEAN NOT NULL DEFAULT false, + last_run_at TIMESTAMPTZ, + last_status TEXT, + last_error TEXT, + created_at TIMESTAMPTZ NOT NULL DEFAULT now(), + updated_at TIMESTAMPTZ NOT NULL DEFAULT now(), + CONSTRAINT maintenance_policy_name_chk CHECK (length(trim(name)) > 0), + CONSTRAINT maintenance_policy_table_name_chk CHECK (length(trim(table_name)) > 0), + CONSTRAINT maintenance_policy_vacuum_strategy_chk CHECK ( + vacuum_strategy IN ('none', 'vacuum', 'analyze', 'vacuum_analyze', 'reindex') + ) +); + +CREATE INDEX IF NOT EXISTS idx_maintenance_policy_enabled + ON maintenance_policy (enabled); + +CREATE TABLE IF NOT EXISTS maintenance_policy_config_audit ( + id TEXT PRIMARY KEY, + policy_id TEXT, + actor_prefix TEXT NOT NULL, + action TEXT NOT NULL, + before_json JSONB, + after_json JSONB, + created_at TIMESTAMPTZ NOT NULL DEFAULT now(), + CONSTRAINT maintenance_policy_config_audit_action_chk CHECK ( + action IN ('create', 'update', 'delete') + ) +); + +CREATE INDEX IF NOT EXISTS idx_maintenance_policy_config_audit_created + ON maintenance_policy_config_audit (created_at DESC); + +ALTER TABLE postgres_maintenance_audit + ADD COLUMN IF NOT EXISTS policy_id TEXT REFERENCES maintenance_policy (id) ON DELETE SET NULL; + +CREATE INDEX IF NOT EXISTS idx_postgres_maintenance_audit_policy + ON postgres_maintenance_audit (policy_id); diff --git a/migrations/sqlite/000025_maintenance_policies.down.sql b/migrations/sqlite/000025_maintenance_policies.down.sql new file mode 100644 index 0000000..5aa9d4a --- /dev/null +++ b/migrations/sqlite/000025_maintenance_policies.down.sql @@ -0,0 +1,5 @@ +ALTER TABLE postgres_maintenance_audit DROP COLUMN policy_id; + +DROP TABLE IF EXISTS maintenance_policy_config_audit; + +DROP TABLE IF EXISTS maintenance_policy; diff --git a/migrations/sqlite/000025_maintenance_policies.up.sql b/migrations/sqlite/000025_maintenance_policies.up.sql new file mode 100644 index 0000000..bf1a351 --- /dev/null +++ b/migrations/sqlite/000025_maintenance_policies.up.sql @@ -0,0 +1,29 @@ +CREATE TABLE IF NOT EXISTS maintenance_policy ( + id TEXT PRIMARY KEY, + name TEXT NOT NULL, + table_name TEXT NOT NULL, + condition_sql TEXT NOT NULL DEFAULT 'true', + retention_period_sec INTEGER, + max_rows INTEGER, + vacuum_strategy TEXT NOT NULL DEFAULT 'none', + schedule_cron TEXT NOT NULL, + enabled INTEGER NOT NULL DEFAULT 1, + dry_run_enabled INTEGER NOT NULL DEFAULT 0, + last_run_at TEXT, + last_status TEXT, + last_error TEXT, + created_at TEXT NOT NULL, + updated_at TEXT NOT NULL +); + +CREATE TABLE IF NOT EXISTS maintenance_policy_config_audit ( + id TEXT PRIMARY KEY, + policy_id TEXT, + actor_prefix TEXT NOT NULL, + action TEXT NOT NULL, + before_json TEXT, + after_json TEXT, + created_at TEXT NOT NULL +); + +ALTER TABLE postgres_maintenance_audit ADD COLUMN policy_id TEXT;