Skip to content

Commit d026839

Browse files
committed
cmd/relui,internal/relui: add database migrations
This change adds golang-migrate to manage database migrations for relui. relui will run all database migrations on start, and return an error for any failures. The current developer tooling is managed through the Makefile. For ease of use, common tasks will be moved to a script in a future CL. For golang/go#47401 Change-Id: I678c3b21d7e89d7d5a66305f66075f02cc2f3284 Reviewed-on: https://go-review.googlesource.com/c/build/+/349951 Trust: Alexander Rakoczy <[email protected]> Run-TryBot: Alexander Rakoczy <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Heschi Kreinick <[email protected]>
1 parent 74917a5 commit d026839

10 files changed

+686
-69
lines changed

cmd/relui/Makefile

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,42 @@
33
# license that can be found in the LICENSE file.
44

55
VERSION := $(shell ../coordinator/version.sh)
6+
DOCKER_TAG := golang/relui:$(VERSION)
7+
8+
INTERNAL_PATH := ../../internal/relui
9+
MIGRATION_FILES := $(wildcard $(INTERNAL_PATH)/migrations/*.sql)
10+
GO_FILES := $(shell find $(INTERNAL_PATH) -name "*.go")
11+
12+
POSTGRES_DATA_DEV := "postgres-data-dev:/var/lib/postgresql/data"
13+
POSTGRES_RUN_DEV := "postgres-run-dev:/var/run/postgresql"
14+
POSTGRES_USER := "postgres"
15+
POSTGRES_TEST := psql --username=$(POSTGRES_USER) -c "SELECT 1;"
16+
17+
DEV_CFG := ${HOME}/.local/share/relui
618

719
.PHONY: dev
820
dev: postgres-dev docker
9-
docker run --rm --name=relui-dev -v postgres-run-dev:/var/run/postgresql -p 8080:8080 golang/relui:$(VERSION)
21+
docker run --rm --name=relui-dev -v $(POSTGRES_RUN_DEV) -p 8080:8080 $(DOCKER_TAG)
1022

11-
RUNNING := $(shell docker ps -q -f Name=postgres-dev)
1223
.PHONY: postgres-dev
13-
postgres-dev: network-dev
14-
docker stop postgres-dev || true
15-
docker run -d --rm --name=postgres-dev --network=relui-dev -v postgres-data-dev:/var/lib/postgresql/data -v postgres-run-dev:/var/run/postgresql -e POSTGRES_HOST_AUTH_METHOD=trust postgres:13
24+
postgres-dev: $(DEV_CFG)/pgpass
25+
docker exec postgres-dev $(POSTGRES_TEST) || \
26+
docker run --rm -d --name=postgres-dev \
27+
-p 127.0.0.1:5432:5432 \
28+
-v $(POSTGRES_DATA_DEV) \
29+
-v $(POSTGRES_RUN_DEV) \
30+
-v $(DEV_CFG)/pgpass:/run/secrets/pgpass \
31+
-e POSTGRES_PASSWORD_FILE=/run/secrets/pgpass \
32+
postgres:13
1633

17-
.PHONY: network-dev
18-
network-dev:
19-
docker network inspect relui-dev -f "{{.Name}}" || docker network create -d bridge relui-dev
34+
schema.sql: $(MIGRATION_FILES) $(GO_FILES)
35+
docker build -f Dockerfile -t golang/relui:$(VERSION) ../..
36+
docker run --rm --name=relui-dev-migrate -v $(POSTGRES_RUN_DEV) $(DOCKER_TAG) --only-migrate
37+
docker exec postgres-dev pg_dump --username=$(POSTGRES_USER) --schema-only relui-dev > schema.sql
2038

2139
.PHONY: test
2240
test: postgres-dev docker-test
23-
docker run --rm --name=relui-test -v postgres-run-dev:/var/run/postgresql -e RELUI_TEST_DATABASE="host=/var/run/postgresql user=postgres database=relui-test" golang/relui-test:$(VERSION)
41+
docker run --rm --name=relui-test -v $(POSTGRES_RUN_DEV) -e RELUI_TEST_DATABASE="host=/var/run/postgresql user=postgres database=relui-test" golang/relui-test:$(VERSION)
2442

2543
.PHONY: docker
2644
docker:
@@ -29,3 +47,11 @@ docker:
2947
.PHONY: docker-test
3048
docker-test:
3149
docker build -f Dockerfile.test -t golang/relui-test:$(VERSION) ../..
50+
51+
$(DEV_CFG):
52+
mkdir -p $(DEV_CFG)
53+
54+
$(DEV_CFG)/pgpass: $(DEV_CFG)
55+
touch $(DEV_CFG)/pgpass
56+
chmod 0600 $(DEV_CFG)/pgpass
57+
openssl rand -hex -out $(DEV_CFG)/pgpass 32

cmd/relui/main.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,25 @@ import (
1818
)
1919

2020
var (
21-
pgConnect = flag.String("pg-connect", "host=/var/run/postgresql user=postgres database=relui-dev", "Postgres connection string or URI")
21+
pgConnect = flag.String("pg-connect", "host=/var/run/postgresql user=postgres database=relui-dev", "Postgres connection string or URI")
22+
onlyMigrate = flag.Bool("only-migrate", false, "Exit after running migrations. Migrations are run by default.")
2223
)
2324

2425
func main() {
26+
flag.Parse()
2527
ctx := context.Background()
26-
s, err := relui.NewServer(ctx, *pgConnect)
28+
if err := relui.InitDB(ctx, *pgConnect); err != nil {
29+
log.Fatalf("relui.InitDB() = %v", err)
30+
}
31+
if *onlyMigrate {
32+
return
33+
}
34+
d := new(relui.PgStore)
35+
if err := d.Connect(ctx, *pgConnect); err != nil {
36+
log.Fatal(err)
37+
}
38+
defer d.Close()
39+
s, err := relui.NewServer(ctx, d)
2740
if err != nil {
2841
log.Fatalf("relui.NewServer() = %v", err)
2942
}

cmd/relui/schema.sql

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
--
2+
-- PostgreSQL database dump
3+
--
4+
5+
-- Dumped from database version 13.3 (Debian 13.3-1.pgdg100+1)
6+
-- Dumped by pg_dump version 13.3 (Debian 13.3-1.pgdg100+1)
7+
8+
SET statement_timeout = 0;
9+
SET lock_timeout = 0;
10+
SET idle_in_transaction_session_timeout = 0;
11+
SET client_encoding = 'UTF8';
12+
SET standard_conforming_strings = on;
13+
SELECT pg_catalog.set_config('search_path', '', false);
14+
SET check_function_bodies = false;
15+
SET xmloption = content;
16+
SET client_min_messages = warning;
17+
SET row_security = off;
18+
19+
SET default_tablespace = '';
20+
21+
SET default_table_access_method = heap;
22+
23+
--
24+
-- Name: migrations; Type: TABLE; Schema: public; Owner: postgres
25+
--
26+
27+
CREATE TABLE public.migrations (
28+
version bigint NOT NULL,
29+
dirty boolean NOT NULL
30+
);
31+
32+
33+
ALTER TABLE public.migrations OWNER TO postgres;
34+
35+
--
36+
-- Name: tasks; Type: TABLE; Schema: public; Owner: postgres
37+
--
38+
39+
CREATE TABLE public.tasks (
40+
workflow_id uuid NOT NULL,
41+
name text NOT NULL,
42+
finished boolean DEFAULT false NOT NULL,
43+
result jsonb,
44+
error text,
45+
created_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL,
46+
updated_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL
47+
);
48+
49+
50+
ALTER TABLE public.tasks OWNER TO postgres;
51+
52+
--
53+
-- Name: workflows; Type: TABLE; Schema: public; Owner: postgres
54+
--
55+
56+
CREATE TABLE public.workflows (
57+
id uuid NOT NULL,
58+
params jsonb,
59+
name text,
60+
created_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL,
61+
updated_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL
62+
);
63+
64+
65+
ALTER TABLE public.workflows OWNER TO postgres;
66+
67+
--
68+
-- Name: migrations migrations_pkey; Type: CONSTRAINT; Schema: public; Owner: postgres
69+
--
70+
71+
ALTER TABLE ONLY public.migrations
72+
ADD CONSTRAINT migrations_pkey PRIMARY KEY (version);
73+
74+
75+
--
76+
-- Name: tasks tasks_pkey; Type: CONSTRAINT; Schema: public; Owner: postgres
77+
--
78+
79+
ALTER TABLE ONLY public.tasks
80+
ADD CONSTRAINT tasks_pkey PRIMARY KEY (workflow_id, name);
81+
82+
83+
--
84+
-- Name: workflows workflows_pkey; Type: CONSTRAINT; Schema: public; Owner: postgres
85+
--
86+
87+
ALTER TABLE ONLY public.workflows
88+
ADD CONSTRAINT workflows_pkey PRIMARY KEY (id);
89+
90+
91+
--
92+
-- Name: tasks tasks_workflow_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: postgres
93+
--
94+
95+
ALTER TABLE ONLY public.tasks
96+
ADD CONSTRAINT tasks_workflow_id_fkey FOREIGN KEY (workflow_id) REFERENCES public.workflows(id);
97+
98+
99+
--
100+
-- PostgreSQL database dump complete
101+
--
102+

go.mod

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module golang.org/x/build
33
go 1.16
44

55
require (
6-
cloud.google.com/go v0.65.0
6+
cloud.google.com/go v0.88.0
77
cloud.google.com/go/bigquery v1.8.0
88
cloud.google.com/go/datastore v1.1.0
99
cloud.google.com/go/storage v1.10.0
@@ -16,11 +16,11 @@ require (
1616
github.com/creack/pty v1.1.15
1717
github.com/davecgh/go-spew v1.1.1
1818
github.com/gliderlabs/ssh v0.3.3
19-
github.com/golang/protobuf v1.4.3
20-
github.com/google/go-cmp v0.5.5
19+
github.com/golang-migrate/migrate/v4 v4.15.0-beta.3
20+
github.com/golang/protobuf v1.5.2
21+
github.com/google/go-cmp v0.5.6
2122
github.com/google/go-github v17.0.0+incompatible
22-
github.com/google/go-querystring v1.0.0 // indirect
23-
github.com/google/uuid v1.1.2
23+
github.com/google/uuid v1.2.0
2424
github.com/googleapis/gax-go/v2 v2.0.5
2525
github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8
2626
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7
@@ -37,9 +37,9 @@ require (
3737
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c
3838
golang.org/x/text v0.3.6
3939
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac
40-
google.golang.org/api v0.30.0
41-
google.golang.org/genproto v0.0.0-20200825200019-8632dd797987
42-
google.golang.org/grpc v1.33.2
40+
google.golang.org/api v0.51.0
41+
google.golang.org/genproto v0.0.0-20210726143408-b02e89920bf0
42+
google.golang.org/grpc v1.39.0
4343
gopkg.in/inf.v0 v0.9.1
4444
grpc.go4.org v0.0.0-20170609214715-11d0a25b4919
4545
)

0 commit comments

Comments
 (0)