Skip to content

Commit

Permalink
Merge pull request #5 from ShwetaGhenand/feature/add-migration
Browse files Browse the repository at this point in the history
feat: postgres database migration
  • Loading branch information
ShwetaGhenand authored Nov 2, 2021
2 parents f3a9211 + 1dbf5d5 commit 9e25aec
Show file tree
Hide file tree
Showing 11 changed files with 1,442 additions and 17 deletions.
18 changes: 18 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
DOCKER_IMAGE = user-apis
MIGRATOR_DOCKER_IMAGE = migrator

.PHONY: build
build:
Expand All @@ -16,3 +17,20 @@ logs:
.PHONY: down
down:
docker-compose down

.PHONY: build-migrator
build-migrator:
@echo "Docker Build..."
docker build -t $(MIGRATOR_DOCKER_IMAGE) cmds/server/migrator

.PHONY: migrate-up
migrate-up:build-migrator
docker run --network host $(MIGRATOR_DOCKER_IMAGE) \
-path=/migrations/ \
-database "postgres://postgres:zyxwv@localhost:5432/userdb?sslmode=disable" up 2

.PHONY: migrate-down
migrate-down:build-migrator
docker run --network host $(MIGRATOR_DOCKER_IMAGE) \
-path=/migrations/ \
-database "postgres://postgres:zyxwv@localhost:5432/userdb?sslmode=disable" down 2
10 changes: 3 additions & 7 deletions cmds/server/db_config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package server

import (
"io/ioutil"
"fmt"
"net/url"
"strconv"

Expand Down Expand Up @@ -37,13 +37,9 @@ func initDB(c DBConfig) (*Queries, error) {
return nil, err
}
conn := stdlib.OpenDB(*cfg)
content, err := ioutil.ReadFile("schema.sql")
err = validateSchema(conn)
if err != nil {
return nil, err
}
query := string(content)
if _, err := conn.Exec(query); err != nil {
return nil, err
return nil, fmt.Errorf("validating schema: %w", err)
}
db := New(conn)
return db, nil
Expand Down
35 changes: 35 additions & 0 deletions cmds/server/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package server

import (
"database/sql"
"embed"

"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/postgres"
"github.com/golang-migrate/migrate/v4/source/iofs"
)

//go:embed migrator/migrations/*.sql
var fs embed.FS

const version = 1

func validateSchema(db *sql.DB) error {
sourceInstance, err := iofs.New(fs, "migrator/migrations")
if err != nil {
return err
}
targetInstance, err := postgres.WithInstance(db, new(postgres.Config))
if err != nil {
return err
}
m, err := migrate.NewWithInstance("iofs", sourceInstance, "postgres", targetInstance)
if err != nil {
return err
}
err = m.Migrate(version) // current version
if err != nil && err != migrate.ErrNoChange {
return err
}
return sourceInstance.Close()
}
3 changes: 3 additions & 0 deletions cmds/server/migrator/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM migrate/migrate

COPY /migrations /migrations
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
BEGIN;

DROP TABLE users;
COMMIT;
12 changes: 12 additions & 0 deletions cmds/server/migrator/migrations/001_create_users_table.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
BEGIN;

CREATE TABLE IF NOT EXISTS users(
id INT PRIMARY KEY NOT NULL,
name TEXT NOT NULL,
email TEXT NOT NULL,
phone TEXT,
age INT,
address TEXT
);

COMMIT;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
BEGIN;

ALTER TABLE users DROP COLUMN personal_number;

COMMIT;
6 changes: 6 additions & 0 deletions cmds/server/migrator/migrations/002_alter_users_table.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
BEGIN;

ALTER TABLE users
ADD COLUMN personal_number TEXT;

COMMIT;
4 changes: 4 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ services:
- .env
ports:
- "5432:5432"
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=zyxwv
- POSTGRES_DB=userdb
volumes:
- ./postgres-data:/var/lib/postgresql/data
healthcheck:
Expand Down
12 changes: 7 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,28 @@ module github.com/go-crud-apis
go 1.17

require (
github.com/golang-migrate/migrate/v4 v4.15.1
github.com/gorilla/mux v1.8.0
github.com/jackc/pgx v3.6.2+incompatible
github.com/jackc/pgx/v4 v4.13.0
github.com/sebnyberg/flagtags v0.0.0-20210812191134-9825f4cda663
github.com/urfave/cli/v2 v2.3.0
)

require (
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-multierror v1.1.0 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/fake v0.0.0-20150926172116-812a484cc733 // indirect
github.com/jackc/pgconn v1.10.0 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.1.1 // indirect
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
github.com/jackc/pgtype v1.8.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/lib/pq v1.10.2 // indirect
github.com/russross/blackfriday/v2 v2.0.1 // indirect
github.com/sebnyberg/flagtags v0.0.0-20210812191134-9825f4cda663 // indirect
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
go.uber.org/atomic v1.6.0 // indirect
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect
golang.org/x/text v0.3.7 // indirect
)
1,350 changes: 1,345 additions & 5 deletions go.sum

Large diffs are not rendered by default.

0 comments on commit 9e25aec

Please sign in to comment.