fix(storage): Default domain_expiration to 0 for SQL when the column doesn't already exist

This will prevent temporary issues with the parsing of old results that would otherwise
have a value of NULL for domain_expiration

Fixes an issue introduced by #325
This commit is contained in:
TwiN 2022-09-07 18:17:28 -04:00
parent 01484832fc
commit d9f86f1155
3 changed files with 58 additions and 54 deletions

View File

@ -5,8 +5,8 @@ func (s *Store) createPostgresSchema() error {
CREATE TABLE IF NOT EXISTS endpoints ( CREATE TABLE IF NOT EXISTS endpoints (
endpoint_id BIGSERIAL PRIMARY KEY, endpoint_id BIGSERIAL PRIMARY KEY,
endpoint_key TEXT UNIQUE, endpoint_key TEXT UNIQUE,
endpoint_name TEXT, endpoint_name TEXT NOT NULL,
endpoint_group TEXT, endpoint_group TEXT NOT NULL,
UNIQUE(endpoint_name, endpoint_group) UNIQUE(endpoint_name, endpoint_group)
) )
`) `)
@ -16,9 +16,9 @@ func (s *Store) createPostgresSchema() error {
_, err = s.db.Exec(` _, err = s.db.Exec(`
CREATE TABLE IF NOT EXISTS endpoint_events ( CREATE TABLE IF NOT EXISTS endpoint_events (
endpoint_event_id BIGSERIAL PRIMARY KEY, endpoint_event_id BIGSERIAL PRIMARY KEY,
endpoint_id INTEGER REFERENCES endpoints(endpoint_id) ON DELETE CASCADE, endpoint_id INTEGER NOT NULL REFERENCES endpoints(endpoint_id) ON DELETE CASCADE,
event_type TEXT, event_type TEXT NOT NULL,
event_timestamp TIMESTAMP event_timestamp TIMESTAMP NOT NULL
) )
`) `)
if err != nil { if err != nil {
@ -27,18 +27,18 @@ func (s *Store) createPostgresSchema() error {
_, err = s.db.Exec(` _, err = s.db.Exec(`
CREATE TABLE IF NOT EXISTS endpoint_results ( CREATE TABLE IF NOT EXISTS endpoint_results (
endpoint_result_id BIGSERIAL PRIMARY KEY, endpoint_result_id BIGSERIAL PRIMARY KEY,
endpoint_id BIGINT REFERENCES endpoints(endpoint_id) ON DELETE CASCADE, endpoint_id BIGINT NOT NULL REFERENCES endpoints(endpoint_id) ON DELETE CASCADE,
success BOOLEAN, success BOOLEAN NOT NULL,
errors TEXT, errors TEXT NOT NULL,
connected BOOLEAN, connected BOOLEAN NOT NULL,
status BIGINT, status BIGINT NOT NULL,
dns_rcode TEXT, dns_rcode TEXT NOT NULL,
certificate_expiration BIGINT, certificate_expiration BIGINT NOT NULL,
domain_expiration BIGINT, domain_expiration BIGINT NOT NULL,
hostname TEXT, hostname TEXT NOT NULL,
ip TEXT, ip TEXT NOT NULL,
duration BIGINT, duration BIGINT NOT NULL,
timestamp TIMESTAMP timestamp TIMESTAMP NOT NULL
) )
`) `)
if err != nil { if err != nil {
@ -47,9 +47,9 @@ func (s *Store) createPostgresSchema() error {
_, err = s.db.Exec(` _, err = s.db.Exec(`
CREATE TABLE IF NOT EXISTS endpoint_result_conditions ( CREATE TABLE IF NOT EXISTS endpoint_result_conditions (
endpoint_result_condition_id BIGSERIAL PRIMARY KEY, endpoint_result_condition_id BIGSERIAL PRIMARY KEY,
endpoint_result_id BIGINT REFERENCES endpoint_results(endpoint_result_id) ON DELETE CASCADE, endpoint_result_id BIGINT NOT NULL REFERENCES endpoint_results(endpoint_result_id) ON DELETE CASCADE,
condition TEXT, condition TEXT NOT NULL,
success BOOLEAN success BOOLEAN NOT NULL
) )
`) `)
if err != nil { if err != nil {
@ -58,15 +58,15 @@ func (s *Store) createPostgresSchema() error {
_, err = s.db.Exec(` _, err = s.db.Exec(`
CREATE TABLE IF NOT EXISTS endpoint_uptimes ( CREATE TABLE IF NOT EXISTS endpoint_uptimes (
endpoint_uptime_id BIGSERIAL PRIMARY KEY, endpoint_uptime_id BIGSERIAL PRIMARY KEY,
endpoint_id BIGINT REFERENCES endpoints(endpoint_id) ON DELETE CASCADE, endpoint_id BIGINT NOT NULL REFERENCES endpoints(endpoint_id) ON DELETE CASCADE,
hour_unix_timestamp BIGINT, hour_unix_timestamp BIGINT NOT NULL,
total_executions BIGINT, total_executions BIGINT NOT NULL,
successful_executions BIGINT, successful_executions BIGINT NOT NULL,
total_response_time BIGINT, total_response_time BIGINT NOT NULL,
UNIQUE(endpoint_id, hour_unix_timestamp) UNIQUE(endpoint_id, hour_unix_timestamp)
) )
`) `)
// Silent table modifications // Silent table modifications
_, _ = s.db.Exec(`ALTER TABLE endpoint_results ADD IF NOT EXISTS domain_expiration BIGINT`) _, _ = s.db.Exec(`ALTER TABLE endpoint_results ADD IF NOT EXISTS domain_expiration BIGINT NOT NULL DEFAULT 0`)
return err return err
} }

View File

@ -5,8 +5,8 @@ func (s *Store) createSQLiteSchema() error {
CREATE TABLE IF NOT EXISTS endpoints ( CREATE TABLE IF NOT EXISTS endpoints (
endpoint_id INTEGER PRIMARY KEY, endpoint_id INTEGER PRIMARY KEY,
endpoint_key TEXT UNIQUE, endpoint_key TEXT UNIQUE,
endpoint_name TEXT, endpoint_name TEXT NOT NULL,
endpoint_group TEXT, endpoint_group TEXT NOT NULL,
UNIQUE(endpoint_name, endpoint_group) UNIQUE(endpoint_name, endpoint_group)
) )
`) `)
@ -16,9 +16,9 @@ func (s *Store) createSQLiteSchema() error {
_, err = s.db.Exec(` _, err = s.db.Exec(`
CREATE TABLE IF NOT EXISTS endpoint_events ( CREATE TABLE IF NOT EXISTS endpoint_events (
endpoint_event_id INTEGER PRIMARY KEY, endpoint_event_id INTEGER PRIMARY KEY,
endpoint_id INTEGER REFERENCES endpoints(endpoint_id) ON DELETE CASCADE, endpoint_id INTEGER NOT NULL REFERENCES endpoints(endpoint_id) ON DELETE CASCADE,
event_type TEXT, event_type TEXT NOT NULL,
event_timestamp TIMESTAMP event_timestamp TIMESTAMP NOT NULL
) )
`) `)
if err != nil { if err != nil {
@ -27,18 +27,18 @@ func (s *Store) createSQLiteSchema() error {
_, err = s.db.Exec(` _, err = s.db.Exec(`
CREATE TABLE IF NOT EXISTS endpoint_results ( CREATE TABLE IF NOT EXISTS endpoint_results (
endpoint_result_id INTEGER PRIMARY KEY, endpoint_result_id INTEGER PRIMARY KEY,
endpoint_id INTEGER REFERENCES endpoints(endpoint_id) ON DELETE CASCADE, endpoint_id INTEGER NOT NULL REFERENCES endpoints(endpoint_id) ON DELETE CASCADE,
success INTEGER, success INTEGER NOT NULL,
errors TEXT, errors TEXT NOT NULL,
connected INTEGER, connected INTEGER NOT NULL,
status INTEGER, status INTEGER NOT NULL,
dns_rcode TEXT, dns_rcode TEXT NOT NULL,
certificate_expiration INTEGER, certificate_expiration INTEGER NOT NULL,
domain_expiration INTEGER, domain_expiration INTEGER NOT NULL,
hostname TEXT, hostname TEXT NOT NULL,
ip TEXT, ip TEXT NOT NULL,
duration INTEGER, duration INTEGER NOT NULL,
timestamp TIMESTAMP timestamp TIMESTAMP NOT NULL
) )
`) `)
if err != nil { if err != nil {
@ -47,9 +47,9 @@ func (s *Store) createSQLiteSchema() error {
_, err = s.db.Exec(` _, err = s.db.Exec(`
CREATE TABLE IF NOT EXISTS endpoint_result_conditions ( CREATE TABLE IF NOT EXISTS endpoint_result_conditions (
endpoint_result_condition_id INTEGER PRIMARY KEY, endpoint_result_condition_id INTEGER PRIMARY KEY,
endpoint_result_id INTEGER REFERENCES endpoint_results(endpoint_result_id) ON DELETE CASCADE, endpoint_result_id INTEGER NOT NULL REFERENCES endpoint_results(endpoint_result_id) ON DELETE CASCADE,
condition TEXT, condition TEXT NOT NULL,
success INTEGER success INTEGER NOT NULL
) )
`) `)
if err != nil { if err != nil {
@ -58,15 +58,15 @@ func (s *Store) createSQLiteSchema() error {
_, err = s.db.Exec(` _, err = s.db.Exec(`
CREATE TABLE IF NOT EXISTS endpoint_uptimes ( CREATE TABLE IF NOT EXISTS endpoint_uptimes (
endpoint_uptime_id INTEGER PRIMARY KEY, endpoint_uptime_id INTEGER PRIMARY KEY,
endpoint_id INTEGER REFERENCES endpoints(endpoint_id) ON DELETE CASCADE, endpoint_id INTEGER NOT NULL REFERENCES endpoints(endpoint_id) ON DELETE CASCADE,
hour_unix_timestamp INTEGER, hour_unix_timestamp INTEGER NOT NULL,
total_executions INTEGER, total_executions INTEGER NOT NULL,
successful_executions INTEGER, successful_executions INTEGER NOT NULL,
total_response_time INTEGER, total_response_time INTEGER NOT NULL,
UNIQUE(endpoint_id, hour_unix_timestamp) UNIQUE(endpoint_id, hour_unix_timestamp)
) )
`) `)
// Silent table modifications // Silent table modifications TODO: Remove this
_, _ = s.db.Exec(`ALTER TABLE endpoint_results ADD domain_expiration INTEGER`) _, _ = s.db.Exec(`ALTER TABLE endpoint_results ADD domain_expiration INTEGER NOT NULL DEFAULT 0`)
return err return err
} }

View File

@ -609,7 +609,11 @@ func (s *Store) getEndpointResultsByEndpointID(tx *sql.Tx, endpointID int64, pag
result := &core.Result{} result := &core.Result{}
var id int64 var id int64
var joinedErrors string var joinedErrors string
_ = rows.Scan(&id, &result.Success, &joinedErrors, &result.Connected, &result.HTTPStatus, &result.DNSRCode, &result.CertificateExpiration, &result.DomainExpiration, &result.Hostname, &result.IP, &result.Duration, &result.Timestamp) err = rows.Scan(&id, &result.Success, &joinedErrors, &result.Connected, &result.HTTPStatus, &result.DNSRCode, &result.CertificateExpiration, &result.DomainExpiration, &result.Hostname, &result.IP, &result.Duration, &result.Timestamp)
if err != nil {
log.Printf("[sql][getEndpointResultsByEndpointID] Silently failed to retrieve endpoint result for endpointID=%d: %s", endpointID, err.Error())
err = nil
}
if len(joinedErrors) != 0 { if len(joinedErrors) != 0 {
result.Errors = strings.Split(joinedErrors, arraySeparator) result.Errors = strings.Split(joinedErrors, arraySeparator)
} }