soju/database/postgres_test.go

92 lines
2.3 KiB
Go
Raw Permalink Normal View History

2022-05-09 10:34:43 +00:00
package database
2021-10-11 13:55:07 +00:00
import (
"os"
"testing"
)
// PostgreSQL version 0 schema. DO NOT EDIT.
const postgresV0Schema = `
CREATE TABLE "Config" (
id SMALLINT PRIMARY KEY,
version INTEGER NOT NULL,
CHECK(id = 1)
);
INSERT INTO "Config" (id, version) VALUES (1, 1);
CREATE TABLE "User" (
id SERIAL PRIMARY KEY,
username VARCHAR(255) NOT NULL UNIQUE,
password VARCHAR(255),
admin BOOLEAN NOT NULL DEFAULT FALSE,
realname VARCHAR(255)
);
CREATE TABLE "Network" (
id SERIAL PRIMARY KEY,
name VARCHAR(255),
"user" INTEGER NOT NULL REFERENCES "User"(id) ON DELETE CASCADE,
addr VARCHAR(255) NOT NULL,
nick VARCHAR(255) NOT NULL,
username VARCHAR(255),
realname VARCHAR(255),
pass VARCHAR(255),
connect_commands VARCHAR(1023),
sasl_mechanism VARCHAR(255),
sasl_plain_username VARCHAR(255),
sasl_plain_password VARCHAR(255),
sasl_external_cert BYTEA DEFAULT NULL,
sasl_external_key BYTEA DEFAULT NULL,
enabled BOOLEAN NOT NULL DEFAULT TRUE,
UNIQUE("user", addr, nick),
UNIQUE("user", name)
);
CREATE TABLE "Channel" (
id SERIAL PRIMARY KEY,
network INTEGER NOT NULL REFERENCES "Network"(id) ON DELETE CASCADE,
name VARCHAR(255) NOT NULL,
key VARCHAR(255),
detached BOOLEAN NOT NULL DEFAULT FALSE,
detached_internal_msgid VARCHAR(255),
relay_detached INTEGER NOT NULL DEFAULT 0,
reattach_on INTEGER NOT NULL DEFAULT 0,
detach_after INTEGER NOT NULL DEFAULT 0,
detach_on INTEGER NOT NULL DEFAULT 0,
UNIQUE(network, name)
);
CREATE TABLE "DeliveryReceipt" (
id SERIAL PRIMARY KEY,
network INTEGER NOT NULL REFERENCES "Network"(id) ON DELETE CASCADE,
target VARCHAR(255) NOT NULL,
client VARCHAR(255) NOT NULL DEFAULT '',
internal_msgid VARCHAR(255) NOT NULL,
UNIQUE(network, target, client)
);
`
2022-05-09 10:34:43 +00:00
func TestPostgresMigrations(t *testing.T) {
2021-10-11 13:55:07 +00:00
source, ok := os.LookupEnv("SOJU_TEST_POSTGRES")
if !ok {
t.Skip("set SOJU_TEST_POSTGRES to a connection string to execute PostgreSQL tests")
}
2022-05-09 10:34:43 +00:00
sqlDB, err := openTempPostgresDB(source)
2021-10-11 13:55:07 +00:00
if err != nil {
2022-05-09 10:34:43 +00:00
t.Fatalf("openTempPostgresDB() failed: %v", err)
2021-10-11 13:55:07 +00:00
}
db := &PostgresDB{db: sqlDB, temp: true}
defer db.Close()
if _, err := db.db.Exec(postgresV0Schema); err != nil {
2021-10-11 13:55:07 +00:00
t.Fatalf("DB.Exec() failed for v0 schema: %v", err)
}
if err := db.upgrade(); err != nil {
t.Fatalf("PostgresDB.Upgrade() failed: %v", err)
}
}