fix(storage): Create suite-related tables before endpoint-related tables to avoid reference issues (#1251)

Fixes #1250
This commit is contained in:
TwiN
2025-09-10 22:08:58 -04:00
committed by GitHub
parent 3d61f5fe60
commit e3cae4637c
2 changed files with 54 additions and 52 deletions

View File

@@ -1,7 +1,34 @@
package sql
func (s *Store) createPostgresSchema() error {
// Create suite tables
_, err := s.db.Exec(`
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(`
CREATE TABLE IF NOT EXISTS endpoints (
endpoint_id BIGSERIAL PRIMARY KEY,
endpoint_key TEXT UNIQUE,
@@ -83,32 +110,6 @@ func (s *Store) createPostgresSchema() error {
if err != nil {
return err
}
// Create suite tables
_, err = s.db.Exec(`
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 index for suite_results
_, err = s.db.Exec(`
CREATE INDEX IF NOT EXISTS suite_results_suite_id_idx ON suite_results (suite_id);

View File

@@ -1,7 +1,34 @@
package sql
func (s *Store) createSQLiteSchema() error {
// Create suite tables
_, err := s.db.Exec(`
CREATE TABLE IF NOT EXISTS suites (
suite_id INTEGER 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 INTEGER PRIMARY KEY,
suite_id INTEGER NOT NULL REFERENCES suites(suite_id) ON DELETE CASCADE,
success INTEGER NOT NULL,
errors TEXT NOT NULL,
duration INTEGER NOT NULL,
timestamp TIMESTAMP NOT NULL
)
`)
if err != nil {
return err
}
// Create endpoint tables
_, err = s.db.Exec(`
CREATE TABLE IF NOT EXISTS endpoints (
endpoint_id INTEGER PRIMARY KEY,
endpoint_key TEXT UNIQUE,
@@ -83,32 +110,6 @@ func (s *Store) createSQLiteSchema() error {
if err != nil {
return err
}
// Create suite tables
_, err = s.db.Exec(`
CREATE TABLE IF NOT EXISTS suites (
suite_id INTEGER 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 INTEGER PRIMARY KEY,
suite_id INTEGER NOT NULL REFERENCES suites(suite_id) ON DELETE CASCADE,
success INTEGER NOT NULL,
errors TEXT NOT NULL,
duration INTEGER NOT NULL,
timestamp TIMESTAMP NOT NULL
)
`)
if err != nil {
return err
}
// Create indices for performance reasons
_, err = s.db.Exec(`
CREATE INDEX IF NOT EXISTS endpoint_results_endpoint_id_idx ON endpoint_results (endpoint_id);