Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Postgres database #710

Open
wants to merge 6 commits into
base: v1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ jobs:
- name: Get dependencies
run: go get -v -t -d ./...

- name: Setup Pg test db
run: make pgcreatetestdb

- name: Run Tests
run: |
export TEST_ELEMENTS_ENDPOINT='http://admin1:123@localhost:18884'
Expand Down
83 changes: 82 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,85 @@ integrationtest:
## mock: generates mocks for unit tests
mock:
@echo "Generating mocks for unit tests..."
@mockery --dir=internal/core/domain --name=SwapParser --structname=MockSwapParser --filename=swap.go --output=internal/core/domain/mocks
@mockery --dir=internal/core/domain --name=SwapParser --structname=MockSwapParser --filename=swap.go --output=internal/core/domain/mocks

######## PG_DB ########
## pg: starts postgres db inside docker container
pg:
@echo "Starting postgres container..."
@docker run --name tdexd-pg -p 5432:5432 -e POSTGRES_USER=root -e POSTGRES_PASSWORD=secret -e POSTGRES_DB=tdexd -d postgres

## droppg: stop and remove postgres container
droppg:
@echo "Stopping and removing postgres container..."
@docker stop tdexd-pg
@docker rm tdexd-pg

## createdb: create db inside docker container
createdb:
@echo "Creating db..."
@docker exec tdexd-pg createdb --username=root --owner=root tdexd

## createtestdb: create test db inside docker container
createtestdb:
@echo "Creating test db..."
@docker exec tdexd-pg createdb --username=root --owner=root tdexd-test

## recreatedb: drop and create main db
recreatedb: dropdb createdb

## recreatetestdb: drop and create main and test db
recreatetestdb: droptestdb createtestdb

## pgcreatetestdb: starts docker container and creates test db, used in CI
pgcreatetestdb: pg sleep createtestdb
@echo "Starting postgres container with test db..."

## dropdb: drops db inside docker container
dropdb:
@echo "Dropping db..."
@docker exec tdexd-pg dropdb tdexd

## droptestdb: drops test db inside docker container
droptestdb:
@echo "Dropping test db..."
@docker exec tdexd-pg dropdb tdexd-test

## mig_file: creates pg migration file(eg. make FILE=init mig_file)
mig_file:
@echo "creating migration file..."
@migrate create -ext sql -dir ./internal/infrastructure/storage/db/pg/migration $(FILE)

## mig_up_test: creates test db schema
mig_up_test:
@echo "creating test db schema..."
@echo "creating db schema..."
@migrate -database "postgres://root:secret@localhost:5432/tdexd-test?sslmode=disable" -path ./internal/infrastructure/storage/db/pg/migration up

## mig_up: creates db schema
mig_up:
@echo "creating db schema..."
@migrate -database "postgres://root:secret@localhost:5432/tdexd?sslmode=disable" -path ./internal/infrastructure/storage/db/pg/migration up

## mig_down_test: apply down migration on test db
mig_down_test:
@echo "migration down on test db..."
@migrate -database "postgres://root:secret@localhost:5432/tdexd-test?sslmode=disable" -path ./internal/infrastructure/storage/db/pg/migration down

## mig_down: apply down migration without prompt
mig_down:
@echo "migration down..."
@"yes" | migrate -database "postgres://root:secret@localhost:5432/tdexd?sslmode=disable" -path ./internal/infrastructure/storage/db/pg/migration down

## vet_db: check if mig_up and mig_down are ok
vet_db: recreatedb mig_up mig_down
@echo "vet db migration scripts..."

## sqlc: gen sql
sqlc:
@echo "gen sql..."
@cd ./internal/infrastructure/storage/db/pg; sqlc generate

sleep:
@echo "sleeping for 3 seconds..."
@sleep 3
10 changes: 9 additions & 1 deletion cmd/tdexd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"syscall"
"time"

postgresdb "github.com/tdex-network/tdex-daemon/internal/infrastructure/storage/db/pg"

pricefeeder "github.com/tdex-network/tdex-daemon/internal/infrastructure/price-feeder"
pricefeederstore "github.com/tdex-network/tdex-daemon/internal/infrastructure/price-feeder/store/badger"

Expand All @@ -37,6 +39,7 @@ var (
noMacaroons, noOperatorTls, profilerEnabled bool
datadir, dbDir, profilerDir, tradeTLSKey, tradeTLSCert string
walletUnlockPasswordFile, dbType, oceanWalletAddr string
pgConnectString, pgMigrationPath string
connectAddr, connectProto string
operatorTLSExtraIPs, operatorTLSExtraDomains []string
// App services config
Expand Down Expand Up @@ -87,7 +90,10 @@ func main() {
TradePriceSlippage: pricesSlippagePercentage,
TradeSatsPerByte: satsPerByte,
DBType: dbType,
DBConfig: dbDir,
DBConfig: postgresdb.DbConfig{
DataSourceURL: pgConnectString,
MigrationSourceURL: pgMigrationPath,
},
}

runOnOnePort := operatorSvcPort == tradeSvcPort
Expand Down Expand Up @@ -147,6 +153,8 @@ func loadConfig() error {
tradeSvcPort = config.GetInt(config.TradeListeningPortKey)
operatorSvcPort = config.GetInt(config.OperatorListeningPortKey)
oceanWalletAddr = config.GetString(config.OceanWalletAddrKey)
pgConnectString = config.GetString(config.PgConnectAddr)
pgMigrationPath = config.GetString(config.PgMigrationSource)

return nil
}
Expand Down
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@ require (
github.com/btcsuite/btcd v0.23.2
github.com/btcsuite/btcd/btcec/v2 v2.2.0
github.com/btcsuite/btcd/btcutil v1.1.0
github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect
github.com/desertbit/timer v0.0.0-20180107155436-c41aec40b27f // indirect
github.com/dgraph-io/badger/v3 v3.2103.1
github.com/gogo/protobuf v1.3.2
github.com/golang-jwt/jwt v3.2.1+incompatible
github.com/golang-migrate/migrate/v4 v4.15.2
github.com/google/uuid v1.3.0
github.com/gorilla/websocket v1.5.0 // indirect
github.com/gorilla/websocket v1.5.0
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0
github.com/grpc-ecosystem/grpc-gateway/v2 v2.15.2
github.com/improbable-eng/grpc-web v0.13.0
github.com/jackc/pgconn v1.14.0
github.com/jackc/pgx/v4 v4.18.1
github.com/prometheus/client_golang v1.11.1
github.com/rs/cors v1.7.0 // indirect
github.com/shopspring/decimal v1.2.0
Expand Down
Loading