Skip to content

Commit 613a257

Browse files
committedJan 10, 2023
save block status in postgres
1 parent 7cfd159 commit 613a257

9 files changed

+1221
-6
lines changed
 

‎cmd/migrate.go

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package main
2+
3+
import (
4+
"database/sql"
5+
"fmt"
6+
7+
"github.com/pressly/goose/v3"
8+
"github.com/rs/zerolog"
9+
10+
_ "github.com/lib/pq"
11+
)
12+
13+
func migrateDatabase(logger zerolog.Logger, s *Settings, command, schemaName string) {
14+
// setup database
15+
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
16+
"password=%s dbname=%s sslmode=disable",
17+
s.PostgresHOST, s.PostgresPort, s.PostgresUser, s.PostgresPassword, s.PostgresDB)
18+
19+
pg, err := sql.Open("postgres", psqlInfo)
20+
if err != nil {
21+
logger.Fatal().Msgf("failed to establish db connection: %v\n", err)
22+
}
23+
24+
if err = pg.Ping(); err != nil {
25+
logger.Fatal().Msgf("failed to ping db: %v\n", err)
26+
}
27+
28+
// set default
29+
if command == "" {
30+
command = "up"
31+
}
32+
// must create schema so that can set migrations table to that schema
33+
_, err = pg.Exec(fmt.Sprintf("CREATE SCHEMA IF NOT EXISTS %s;", schemaName))
34+
if err != nil {
35+
logger.Fatal().Err(err).Msgf("could not create schema, %s", schemaName)
36+
}
37+
goose.SetTableName(fmt.Sprintf("%s.migrations", schemaName))
38+
if err := goose.Run(command, pg, "migrations"); err != nil {
39+
logger.Fatal().Msgf("failed to apply go code migrations: %v\n", err)
40+
}
41+
// if we add any code migrations import _ "github.com/DIMO-Network/users-api/migrations" // migrations won't work without this
42+
}

‎docker-compose.yaml

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
version: '3.9'
22

33
services:
4-
redis:
5-
image: redis:6.2
6-
container_name: redis-devices-api
4+
db:
5+
container_name: db
6+
image: postgres:11.3
7+
environment:
8+
- POSTGRES_USER=postgres
9+
- POSTGRES_PASSWORD=postgres
10+
- POSTGRES_DB=postgres
711
ports:
8-
- "6379:6379"
9-
12+
- 5432:5432
1013
zookeeper:
1114
image: 'wurstmeister/zookeeper:latest'
1215
ports:
1316
- '2181:2181'
1417
environment:
1518
- ALLOW_ANONYMOUS_LOGIN=yes
16-
1719
kafka:
1820
image: 'wurstmeister/kafka:latest'
1921
ports:

‎migrations/20230110111723_blocks.sql

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
-- +goose Up
2+
-- +goose StatementBegin
3+
SELECT 'up SQL query';
4+
5+
CREATE SCHEMA IF NOT EXISTS chain_indexer;
6+
7+
SET search_path TO chain_indexer, public;
8+
9+
CREATE TABLE IF NOT EXISTS blocks (
10+
number NUMERIC(78),
11+
"hash" TEXT,
12+
processed BOOLEAN
13+
);
14+
15+
-- +goose StatementEnd
16+
17+
-- +goose Down
18+
-- +goose StatementBegin
19+
SELECT 'down SQL query';
20+
21+
SET search_path TO chain_indexer, public;
22+
DROP SCHEMA chain_indexer CASCADE;
23+
DROP TABLE IF EXISTS blocks;
24+
25+
-- +goose StatementEnd

‎models/blocks.go

+983
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎models/boil_queries.go

+33
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎models/boil_table_names.go

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎models/boil_types.go

+52
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎models/boil_view_names.go

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎models/psql_upsert.go

+61
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
Please sign in to comment.