source: code/trunk/db_postgres_test.go@ 785

Last change on this file since 785 was 621, checked in by contact, 4 years ago

db_postgres: add migration test

File size: 2.6 KB
Line 
1package soju
2
3import (
4 "database/sql"
5 "os"
6 "testing"
7)
8
9// PostgreSQL version 0 schema. DO NOT EDIT.
10const postgresV0Schema = `
11CREATE TABLE "Config" (
12 id SMALLINT PRIMARY KEY,
13 version INTEGER NOT NULL,
14 CHECK(id = 1)
15);
16
17INSERT INTO "Config" (id, version) VALUES (1, 1);
18
19CREATE TABLE "User" (
20 id SERIAL PRIMARY KEY,
21 username VARCHAR(255) NOT NULL UNIQUE,
22 password VARCHAR(255),
23 admin BOOLEAN NOT NULL DEFAULT FALSE,
24 realname VARCHAR(255)
25);
26
27CREATE TABLE "Network" (
28 id SERIAL PRIMARY KEY,
29 name VARCHAR(255),
30 "user" INTEGER NOT NULL REFERENCES "User"(id) ON DELETE CASCADE,
31 addr VARCHAR(255) NOT NULL,
32 nick VARCHAR(255) NOT NULL,
33 username VARCHAR(255),
34 realname VARCHAR(255),
35 pass VARCHAR(255),
36 connect_commands VARCHAR(1023),
37 sasl_mechanism VARCHAR(255),
38 sasl_plain_username VARCHAR(255),
39 sasl_plain_password VARCHAR(255),
40 sasl_external_cert BYTEA DEFAULT NULL,
41 sasl_external_key BYTEA DEFAULT NULL,
42 enabled BOOLEAN NOT NULL DEFAULT TRUE,
43 UNIQUE("user", addr, nick),
44 UNIQUE("user", name)
45);
46
47CREATE TABLE "Channel" (
48 id SERIAL PRIMARY KEY,
49 network INTEGER NOT NULL REFERENCES "Network"(id) ON DELETE CASCADE,
50 name VARCHAR(255) NOT NULL,
51 key VARCHAR(255),
52 detached BOOLEAN NOT NULL DEFAULT FALSE,
53 detached_internal_msgid VARCHAR(255),
54 relay_detached INTEGER NOT NULL DEFAULT 0,
55 reattach_on INTEGER NOT NULL DEFAULT 0,
56 detach_after INTEGER NOT NULL DEFAULT 0,
57 detach_on INTEGER NOT NULL DEFAULT 0,
58 UNIQUE(network, name)
59);
60
61CREATE TABLE "DeliveryReceipt" (
62 id SERIAL PRIMARY KEY,
63 network INTEGER NOT NULL REFERENCES "Network"(id) ON DELETE CASCADE,
64 target VARCHAR(255) NOT NULL,
65 client VARCHAR(255) NOT NULL DEFAULT '',
66 internal_msgid VARCHAR(255) NOT NULL,
67 UNIQUE(network, target, client)
68);
69`
70
71func openTempPostgresDB(t *testing.T) *sql.DB {
72 source, ok := os.LookupEnv("SOJU_TEST_POSTGRES")
73 if !ok {
74 t.Skip("set SOJU_TEST_POSTGRES to a connection string to execute PostgreSQL tests")
75 }
76
77 db, err := sql.Open("postgres", source)
78 if err != nil {
79 t.Fatalf("failed to connect to PostgreSQL: %v", err)
80 }
81
82 // Store all tables in a temporary schema which will be dropped when the
83 // connection to PostgreSQL is closed.
84 db.SetMaxOpenConns(1)
85 if _, err := db.Exec("SET search_path TO pg_temp"); err != nil {
86 t.Fatalf("failed to set PostgreSQL search_path: %v", err)
87 }
88
89 return db
90}
91
92func TestPostgresMigrations(t *testing.T) {
93 sqlDB := openTempPostgresDB(t)
94 if _, err := sqlDB.Exec(postgresV0Schema); err != nil {
95 t.Fatalf("DB.Exec() failed for v0 schema: %v", err)
96 }
97
98 db := &PostgresDB{db: sqlDB}
99 defer db.Close()
100
101 if err := db.upgrade(); err != nil {
102 t.Fatalf("PostgresDB.Upgrade() failed: %v", err)
103 }
104}
Note: See TracBrowser for help on using the repository browser.