-- +goose Up
ALTER TABLE alky_dns_queries
    MODIFY COLUMN timestamp DateTime CODEC(Delta, ZSTD(1)),
    MODIFY COLUMN instance_id String CODEC(ZSTD(1)),
    MODIFY COLUMN query_name String CODEC(ZSTD(1)),
    MODIFY COLUMN query_type LowCardinality(String) CODEC(ZSTD(1)),
    MODIFY COLUMN query_class LowCardinality(String) CODEC(ZSTD(1)),
    MODIFY COLUMN remote_addr String CODEC(ZSTD(1)),
    MODIFY COLUMN response_code LowCardinality(String) CODEC(ZSTD(1)),
    MODIFY COLUMN duration Int64 CODEC(T64, ZSTD(1)),
    MODIFY COLUMN cache_hit Bool CODEC(ZSTD(1));

ALTER TABLE alky_dns_queries MODIFY TTL timestamp + INTERVAL 30 DAY;

ALTER TABLE alky_dns_cache_metrics 
    DROP COLUMN IF EXISTS total_queries,
    MODIFY COLUMN timestamp DateTime CODEC(Delta, ZSTD(1)),
    MODIFY COLUMN instance_id String CODEC(ZSTD(1)),
    MODIFY COLUMN cache_hits Int64 CODEC(T64, ZSTD(1)),
    MODIFY COLUMN cache_misses Int64 CODEC(T64, ZSTD(1)),
    MODIFY COLUMN negative_hits Int64 CODEC(T64, ZSTD(1)),
    MODIFY COLUMN positive_hits Int64 CODEC(T64, ZSTD(1)),
    MODIFY COLUMN evictions Int64 CODEC(T64, ZSTD(1)),
    MODIFY COLUMN size Int64 CODEC(T64, ZSTD(1));

ALTER TABLE alky_dns_cache_metrics 
    ADD COLUMN IF NOT EXISTS expired_count Int64 CODEC(T64, ZSTD(1));

ALTER TABLE alky_dns_cache_metrics MODIFY TTL timestamp + INTERVAL 30 DAY;

-- +goose Down
ALTER TABLE alky_dns_queries
    MODIFY COLUMN timestamp DateTime,
    MODIFY COLUMN instance_id String,
    MODIFY COLUMN query_name String,
    MODIFY COLUMN query_type String,
    MODIFY COLUMN query_class String,
    MODIFY COLUMN remote_addr String,
    MODIFY COLUMN response_code String,
    MODIFY COLUMN duration Int64,
    MODIFY COLUMN cache_hit Bool;

ALTER TABLE alky_dns_queries MODIFY TTL timestamp + toIntervalDay(30);

ALTER TABLE alky_dns_cache_metrics 
    ADD COLUMN IF NOT EXISTS total_queries Int64 AFTER instance_id;

ALTER TABLE alky_dns_cache_metrics 
    MODIFY COLUMN timestamp DateTime,
    MODIFY COLUMN instance_id String,
    MODIFY COLUMN cache_hits Int64,
    MODIFY COLUMN cache_misses Int64,
    MODIFY COLUMN negative_hits Int64,
    MODIFY COLUMN positive_hits Int64,
    MODIFY COLUMN evictions Int64,
    MODIFY COLUMN size Int;

ALTER TABLE alky_dns_cache_metrics DROP COLUMN IF EXISTS expired_count;

ALTER TABLE alky_dns_cache_metrics MODIFY TTL timestamp + toIntervalDay(30);
