2021-09-10 18:00:04 -04:00
package sql
func ( s * Store ) createPostgresSchema () error {
2025-09-10 22:08:58 -04:00
// Create suite tables
2021-09-10 18:00:04 -04:00
_ , err := s . db . Exec ( `
2025-09-10 22:08:58 -04:00
CREATE TABLE IF NOT EXISTS suites (
suite_id BIGSERIAL PRIMARY KEY,
suite_key TEXT UNIQUE,
suite_name TEXT NOT NULL,
suite_group TEXT NOT NULL,
UNIQUE(suite_name, suite_group)
)
` )
if err != nil {
return err
}
_ , err = s . db . Exec ( `
CREATE TABLE IF NOT EXISTS suite_results (
suite_result_id BIGSERIAL PRIMARY KEY,
suite_id BIGINT NOT NULL REFERENCES suites(suite_id) ON DELETE CASCADE,
success BOOLEAN NOT NULL,
errors TEXT NOT NULL,
duration BIGINT NOT NULL,
timestamp TIMESTAMP NOT NULL
)
` )
if err != nil {
return err
}
// Create endpoint tables
_ , err = s . db . Exec ( `
2021-10-23 16:47:12 -04:00
CREATE TABLE IF NOT EXISTS endpoints (
endpoint_id BIGSERIAL PRIMARY KEY,
endpoint_key TEXT UNIQUE,
2022-09-07 18:17:28 -04:00
endpoint_name TEXT NOT NULL,
endpoint_group TEXT NOT NULL,
2021-10-23 16:47:12 -04:00
UNIQUE(endpoint_name, endpoint_group)
2021-09-10 18:00:04 -04:00
)
` )
if err != nil {
return err
}
_ , err = s . db . Exec ( `
2021-10-23 16:47:12 -04:00
CREATE TABLE IF NOT EXISTS endpoint_events (
endpoint_event_id BIGSERIAL PRIMARY KEY,
2024-05-15 21:29:45 -04:00
endpoint_id BIGINT NOT NULL REFERENCES endpoints(endpoint_id) ON DELETE CASCADE,
2022-09-07 18:17:28 -04:00
event_type TEXT NOT NULL,
event_timestamp TIMESTAMP NOT NULL
2021-09-10 18:00:04 -04:00
)
` )
if err != nil {
return err
}
_ , err = s . db . Exec ( `
2021-10-23 16:47:12 -04:00
CREATE TABLE IF NOT EXISTS endpoint_results (
endpoint_result_id BIGSERIAL PRIMARY KEY,
2022-09-07 18:17:28 -04:00
endpoint_id BIGINT NOT NULL REFERENCES endpoints(endpoint_id) ON DELETE CASCADE,
success BOOLEAN NOT NULL,
errors TEXT NOT NULL,
connected BOOLEAN NOT NULL,
status BIGINT NOT NULL,
dns_rcode TEXT NOT NULL,
certificate_expiration BIGINT NOT NULL,
domain_expiration BIGINT NOT NULL,
hostname TEXT NOT NULL,
ip TEXT NOT NULL,
duration BIGINT NOT NULL,
2025-09-05 15:39:12 -04:00
timestamp TIMESTAMP NOT NULL,
suite_result_id BIGINT REFERENCES suite_results(suite_result_id) ON DELETE CASCADE
2021-09-10 18:00:04 -04:00
)
` )
if err != nil {
return err
}
_ , err = s . db . Exec ( `
2021-10-23 16:47:12 -04:00
CREATE TABLE IF NOT EXISTS endpoint_result_conditions (
endpoint_result_condition_id BIGSERIAL PRIMARY KEY,
2022-09-07 18:17:28 -04:00
endpoint_result_id BIGINT NOT NULL REFERENCES endpoint_results(endpoint_result_id) ON DELETE CASCADE,
condition TEXT NOT NULL,
success BOOLEAN NOT NULL
2021-09-10 18:00:04 -04:00
)
` )
if err != nil {
return err
}
_ , err = s . db . Exec ( `
2021-10-23 16:47:12 -04:00
CREATE TABLE IF NOT EXISTS endpoint_uptimes (
endpoint_uptime_id BIGSERIAL PRIMARY KEY,
2022-09-07 18:17:28 -04:00
endpoint_id BIGINT NOT NULL REFERENCES endpoints(endpoint_id) ON DELETE CASCADE,
hour_unix_timestamp BIGINT NOT NULL,
total_executions BIGINT NOT NULL,
successful_executions BIGINT NOT NULL,
total_response_time BIGINT NOT NULL,
2021-10-23 16:47:12 -04:00
UNIQUE(endpoint_id, hour_unix_timestamp)
2021-09-10 18:00:04 -04:00
)
` )
2024-05-15 21:29:45 -04:00
if err != nil {
return err
}
_ , err = s . db . Exec ( `
CREATE TABLE IF NOT EXISTS endpoint_alerts_triggered (
endpoint_alert_trigger_id BIGSERIAL PRIMARY KEY,
endpoint_id BIGINT NOT NULL REFERENCES endpoints(endpoint_id) ON DELETE CASCADE,
configuration_checksum TEXT NOT NULL,
resolve_key TEXT NOT NULL,
number_of_successes_in_a_row INTEGER NOT NULL,
UNIQUE(endpoint_id, configuration_checksum)
)
` )
2025-09-05 15:39:12 -04:00
if err != nil {
return err
}
// Create index for suite_results
_ , err = s . db . Exec ( `
CREATE INDEX IF NOT EXISTS suite_results_suite_id_idx ON suite_results (suite_id);
` )
2024-05-15 21:29:45 -04:00
// Silent table modifications TODO: Remove this in v6.0.0
2022-09-07 18:17:28 -04:00
_ , _ = s . db . Exec ( `ALTER TABLE endpoint_results ADD IF NOT EXISTS domain_expiration BIGINT NOT NULL DEFAULT 0` )
2025-09-05 15:39:12 -04:00
// Add suite_result_id to endpoint_results table for suite endpoint linkage
_ , _ = s . db . Exec ( `ALTER TABLE endpoint_results ADD COLUMN IF NOT EXISTS suite_result_id BIGINT REFERENCES suite_results(suite_result_id) ON DELETE CASCADE` )
// Create index for suite_result_id
_ , _ = s . db . Exec ( `CREATE INDEX IF NOT EXISTS endpoint_results_suite_result_id_idx ON endpoint_results(suite_result_id)` )
2021-09-10 18:00:04 -04:00
return err
}