From d0f5110decdd6dc6596bbf734bd5f16d0518595f Mon Sep 17 00:00:00 2001 From: prathamesh0 Date: Tue, 10 May 2022 16:15:57 +0530 Subject: [PATCH] Update Dockerfile to run migrations --- Dockerfile | 29 ++++++++++++++++++-- README.md | 2 +- db/migrations/00018_add_data_nodes.sql | 6 ++-- docker-compose.test.yml | 38 +++++++++----------------- scripts/startup_script.sh | 1 + 5 files changed, 45 insertions(+), 31 deletions(-) diff --git a/Dockerfile b/Dockerfile index 5f18167..db9013a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,3 +1,28 @@ -FROM timescale/timescaledb:latest-pg14 +FROM golang:1.16-alpine as builder -COPY ./schema.sql /docker-entrypoint-initdb.d/init.sql \ No newline at end of file +RUN apk --update --no-cache add make git g++ linux-headers + +ADD . /go/src/github.com/vulcanize/ipld-eth-db + +# Build migration tool +WORKDIR /go/src/github.com/pressly +RUN git clone https://github.com/pressly/goose.git +WORKDIR /go/src/github.com/pressly/goose/cmd/goose +RUN GCO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags '-extldflags "-static"' -tags='no_sqlite3' -o goose . + +# app container +FROM alpine + +WORKDIR /app + +COPY --from=builder /go/src/github.com/vulcanize/ipld-eth-db/scripts/startup_script.sh . + +# copy over files for multi-node setup +COPY --from=builder /go/src/github.com/vulcanize/ipld-eth-db/docker-compose.test.yml docker-multi-node/docker-compose.test.yml +COPY --from=builder /go/src/github.com/vulcanize/ipld-eth-db/scripts/init-access-node.sh docker-multi-node/scripts/init-access-node.sh +COPY --from=builder /go/src/github.com/vulcanize/ipld-eth-db/scripts/init-data-node.sh docker-multi-node/scripts/init-data-node.sh + +COPY --from=builder /go/src/github.com/pressly/goose/cmd/goose/goose goose +COPY --from=builder /go/src/github.com/vulcanize/ipld-eth-db/db/migrations migrations/vulcanizedb + +ENTRYPOINT ["/app/startup_script.sh"] diff --git a/README.md b/README.md index e542627..ed4e601 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ Schemas and utils for IPLD ETH Postgres database * Spin up an access node and three data nodes using [docker-compose.test.yml](./docker-compose.test.yml): ```bash - docker-compose -f docker-compose.test.yml up timescale-test-db pg_data_node_1 pg_data_node_2 pg_data_node_3 + docker-compose -f docker-compose.test.yml up ``` Following final output should be seen on all the nodes: diff --git a/db/migrations/00018_add_data_nodes.sql b/db/migrations/00018_add_data_nodes.sql index 9db0622..2caa8d6 100644 --- a/db/migrations/00018_add_data_nodes.sql +++ b/db/migrations/00018_add_data_nodes.sql @@ -1,9 +1,9 @@ -- +goose NO TRANSACTION -- +goose Up -- this is generated by a script -SELECT add_data_node('dn3', host => 'pg_data_node_3', port => 5432, database => 'vulcanize_testing_v4', password => 'password'); -SELECT add_data_node('dn2', host => 'pg_data_node_2', port => 5432, database => 'vulcanize_testing_v4', password => 'password'); -SELECT add_data_node('dn1', host => 'pg_data_node_1', port => 5432, database => 'vulcanize_testing_v4', password => 'password'); +SELECT add_data_node('dn3', host => 'data-node-3', port => 5432, database => 'vulcanize_testing_v4', password => 'password'); +SELECT add_data_node('dn2', host => 'data-node-2', port => 5432, database => 'vulcanize_testing_v4', password => 'password'); +SELECT add_data_node('dn1', host => 'data-node-1', port => 5432, database => 'vulcanize_testing_v4', password => 'password'); CALL distributed_exec($$ CREATE SCHEMA eth $$); diff --git a/docker-compose.test.yml b/docker-compose.test.yml index c38ee59..6f4ba2c 100644 --- a/docker-compose.test.yml +++ b/docker-compose.test.yml @@ -1,26 +1,14 @@ version: '3.2' services: - statediff-migrations: - restart: on-failure - depends_on: - - test-db - image: vulcanize/statediff-migrations:v0.9.0 - - test-db: - restart: always - image: postgres:10.12-alpine - command: ["postgres", "-c", "log_statement=all"] - environment: - POSTGRES_USER: "vdbm" - POSTGRES_DB: "vulcanize_testing" - POSTGRES_PASSWORD: "password" - ports: - - "127.0.0.1:8066:5432" - - timescale-test-db: - restart: always + access-node: image: timescale/timescaledb:latest-pg14 + restart: always + container_name: access-node + depends_on: + - data-node-1 + - data-node-2 + - data-node-3 command: ["postgres", "-c", "log_statement=all"] environment: POSTGRES_USER: "postgres" @@ -31,10 +19,10 @@ services: volumes: - ./scripts/init-access-node.sh:/docker-entrypoint-initdb.d/init-access-node.sh - pg_data_node_1: + data-node-1: image: timescale/timescaledb:latest-pg14 - container_name: pg_data_node_1 restart: unless-stopped + container_name: data-node-1 command: ["postgres", "-c", "log_statement=all"] environment: POSTGRES_USER: "postgres" @@ -45,10 +33,10 @@ services: volumes: - ./scripts/init-data-node.sh:/docker-entrypoint-initdb.d/init-data-node.sh - pg_data_node_2: + data-node-2: image: timescale/timescaledb:latest-pg14 - container_name: pg_data_node_2 restart: unless-stopped + container_name: data-node-2 command: ["postgres", "-c", "log_statement=all"] environment: POSTGRES_USER: "postgres" @@ -59,10 +47,10 @@ services: volumes: - ./scripts/init-data-node.sh:/docker-entrypoint-initdb.d/init-data-node.sh - pg_data_node_3: + data-node-3: image: timescale/timescaledb:latest-pg14 - container_name: pg_data_node_3 restart: unless-stopped + container_name: data-node-3 command: ["postgres", "-c", "log_statement=all"] environment: POSTGRES_USER: "postgres" diff --git a/scripts/startup_script.sh b/scripts/startup_script.sh index 514a572..0d3375c 100755 --- a/scripts/startup_script.sh +++ b/scripts/startup_script.sh @@ -7,6 +7,7 @@ VDB_PG_CONNECT=postgresql://$DATABASE_USER:$DATABASE_PASSWORD@$DATABASE_HOSTNAME # Run the DB migrations echo "Connecting with: $VDB_PG_CONNECT" +sleep 15 echo "Running database migrations" ./goose -dir migrations/vulcanizedb postgres "$VDB_PG_CONNECT" up-to 21