forked from flynn/flynn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.go
118 lines (105 loc) · 3.71 KB
/
schema.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package main
import (
"github.com/flynn/flynn/Godeps/_workspace/src/github.com/flynn/go-sql"
"github.com/flynn/flynn/pkg/migrate"
)
func migrateDB(db *sql.DB) error {
m := migrate.NewMigrations()
m.Add(1,
`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`,
`CREATE EXTENSION IF NOT EXISTS "hstore"`,
`CREATE TABLE artifacts (
artifact_id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
type text NOT NULL,
uri text NOT NULL,
created_at timestamptz NOT NULL DEFAULT now(),
deleted_at timestamptz
)`,
`CREATE UNIQUE INDEX ON artifacts (type, uri) WHERE deleted_at IS NULL`,
`CREATE TABLE releases (
release_id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
artifact_id uuid NOT NULL REFERENCES artifacts (artifact_id),
data text NOT NULL,
created_at timestamptz NOT NULL DEFAULT now(),
deleted_at timestamptz
)`,
`CREATE TABLE apps (
app_id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
name text NOT NULL,
release_id uuid REFERENCES releases (release_id),
protected bool NOT NULL DEFAULT false,
meta hstore,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
deleted_at timestamptz
)`,
`CREATE UNIQUE INDEX ON apps (name) WHERE deleted_at IS NULL`,
`CREATE TABLE formations (
app_id uuid NOT NULL REFERENCES apps (app_id),
release_id uuid NOT NULL REFERENCES releases (release_id),
processes hstore,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
deleted_at timestamptz,
PRIMARY KEY (app_id, release_id)
)`,
`CREATE FUNCTION notify_formation() RETURNS TRIGGER AS $$
BEGIN
PERFORM pg_notify('formations', NEW.app_id || ':' || NEW.release_id);
RETURN NULL;
END;
$$ LANGUAGE plpgsql`,
`CREATE TRIGGER notify_formation
AFTER INSERT OR UPDATE ON formations
FOR EACH ROW EXECUTE PROCEDURE notify_formation()`,
`CREATE TABLE keys (
key_id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
fingerprint text NOT NULL,
key text NOT NULL,
comment text,
created_at timestamptz NOT NULL DEFAULT now(),
deleted_at timestamptz
)`,
`CREATE UNIQUE INDEX ON keys (fingerprint) WHERE deleted_at IS NULL`,
`CREATE TABLE providers (
provider_id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
name text NOT NULL UNIQUE,
url text NOT NULL UNIQUE,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
deleted_at timestamptz
)`,
`CREATE TABLE resources (
resource_id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
provider_id uuid NOT NULL REFERENCES providers (provider_id),
external_id text NOT NULL,
env hstore,
created_at timestamptz NOT NULL DEFAULT now(),
deleted_at timestamptz,
UNIQUE (provider_id, external_id)
)`,
`CREATE TABLE app_resources (
app_id uuid NOT NULL REFERENCES apps (app_id),
resource_id uuid NOT NULL REFERENCES resources (resource_id),
created_at timestamptz NOT NULL DEFAULT now(),
deleted_at timestamptz,
PRIMARY KEY (app_id, resource_id)
)`,
`CREATE INDEX ON app_resources (resource_id)`,
`CREATE TYPE job_state AS ENUM ('starting', 'up', 'down', 'crashed')`,
`CREATE TABLE job_cache (
job_id text NOT NULL,
host_id text NOT NULL,
app_id uuid NOT NULL REFERENCES apps (app_id),
release_id uuid NOT NULL REFERENCES releases (release_id),
process_type text,
state job_state NOT NULL,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY (job_id, host_id),
FOREIGN KEY (app_id, release_id) REFERENCES formations (app_id, release_id)
)`,
`CREATE SEQUENCE name_ids MAXVALUE 4294967295`,
)
return m.Migrate(db)
}