diff --git a/internal/migrations/sql/00022_data_lifecycle.sql b/internal/migrations/sql/00022_data_lifecycle.sql new file mode 100644 index 0000000..399d9f0 --- /dev/null +++ b/internal/migrations/sql/00022_data_lifecycle.sql @@ -0,0 +1,33 @@ +-- +goose Up +-- TimescaleDB reports usage to its vendor by default. Self-hosted +-- installations must not phone home, so it is switched off for this +-- database; the setting applies to new connections and background jobs. +-- +goose StatementBegin +DO $$ +BEGIN + EXECUTE format('ALTER DATABASE %I SET timescaledb.telemetry_level = off', current_database()); +END +$$; +-- +goose StatementEnd + +-- Aggregates are compressed once they are past their refresh window. +ALTER MATERIALIZED VIEW check_results_1h SET (timescaledb.compress = true); +SELECT add_compression_policy('check_results_1h', compress_after => INTERVAL '30 days'); +ALTER MATERIALIZED VIEW check_results_1d SET (timescaledb.compress = true); +SELECT add_compression_policy('check_results_1d', compress_after => INTERVAL '180 days'); + +-- The daily aggregate was kept forever, including data of deleted monitors. +-- The API adjusts all retention periods to its configuration at start. +SELECT add_retention_policy('check_results_1d', INTERVAL '1095 days'); + +-- +goose Down +SELECT remove_retention_policy('check_results_1d', if_exists => true); +SELECT remove_compression_policy('check_results_1d', if_exists => true); +SELECT remove_compression_policy('check_results_1h', if_exists => true); +-- +goose StatementBegin +DO $$ +BEGIN + EXECUTE format('ALTER DATABASE %I RESET timescaledb.telemetry_level', current_database()); +END +$$; +-- +goose StatementEnd