Cleanup SQL migrations
* upgrade the migrate package * remove the dev migrations * remove unused migration code
This commit is contained in:
parent
f048384d37
commit
8d453e9236
7
Makefile
7
Makefile
@ -5,7 +5,7 @@ DB_PASS=test
|
|||||||
DB_DSN_DEV=postgres://$(DB_USER):$(DB_PASS)@127.0.0.1:5432/dev?sslmode=disable
|
DB_DSN_DEV=postgres://$(DB_USER):$(DB_PASS)@127.0.0.1:5432/dev?sslmode=disable
|
||||||
DB_DSN_TEST=postgres://$(DB_USER):$(DB_PASS)@127.0.0.1:5432/test?sslmode=disable
|
DB_DSN_TEST=postgres://$(DB_USER):$(DB_PASS)@127.0.0.1:5432/test?sslmode=disable
|
||||||
|
|
||||||
MIGRATION=migrate -url '$(DB_DSN_DEV)'
|
MIGRATION=migrate -database '$(DB_DSN_DEV)'
|
||||||
MIGRATION_SCHEMA=./sql/migration
|
MIGRATION_SCHEMA=./sql/migration
|
||||||
MIGRATION_TEST_DATA=./sql/dev
|
MIGRATION_TEST_DATA=./sql/dev
|
||||||
|
|
||||||
@ -38,10 +38,7 @@ build_docker_image:
|
|||||||
migration-schema: docker
|
migration-schema: docker
|
||||||
$(MIGRATION) -path $(MIGRATION_SCHEMA) up
|
$(MIGRATION) -path $(MIGRATION_SCHEMA) up
|
||||||
|
|
||||||
migration-dev-data: docker migration-schema
|
migration: migration-schema
|
||||||
$(MIGRATION) -path $(MIGRATION_TEST_DATA) up
|
|
||||||
|
|
||||||
migration: migration-schema migration-dev-data
|
|
||||||
|
|
||||||
dev: docker migration watch-go
|
dev: docker migration watch-go
|
||||||
|
|
||||||
|
14
README.md
14
README.md
@ -24,8 +24,9 @@ go get ./...
|
|||||||
go tools:
|
go tools:
|
||||||
|
|
||||||
```
|
```
|
||||||
go get -v github.com/pilu/fresh
|
go get -v -u github.com/pilu/fresh
|
||||||
go get -v github.com/mattes/migrate
|
go get -v -u github.com/mattes/migrate
|
||||||
|
go build -tags 'postgres' -o $GOPATH/bin/migrate github.com/mattes/migrate/cli
|
||||||
```
|
```
|
||||||
|
|
||||||
## Dev
|
## Dev
|
||||||
@ -50,7 +51,14 @@ yarn start
|
|||||||
docker run -it --rm -e PGPASSWORD=test --link canape_postgresql_dev:postgres postgres:9.5 psql -h postgres -U test -d dev
|
docker run -it --rm -e PGPASSWORD=test --link canape_postgresql_dev:postgres postgres:9.5 psql -h postgres -U test -d dev
|
||||||
```
|
```
|
||||||
|
|
||||||
## Default users
|
## Setup the dev users
|
||||||
|
|
||||||
|
Connect to the database and enter this sql queries:
|
||||||
|
|
||||||
|
```
|
||||||
|
INSERT INTO users (name, hash, admin, activated) VALUES ('test', '$2a$10$QHx07iyuxO1RcehgtjMgjOzv03Bx2eeSKvsxkoj9oR2NJ4cklh6ue', false, true);
|
||||||
|
INSERT INTO users (name, hash, admin, activated) VALUES ('admin', '$2a$10$qAbyDZsHtcnhXhjhQZkD2uKlX72eMHsX8Hi2Cnl1vJUqHQiey2qa6', true, true);
|
||||||
|
```
|
||||||
|
|
||||||
This users are defined with this parameters:
|
This users are defined with this parameters:
|
||||||
pepper: "pepper"
|
pepper: "pepper"
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
echo "Running migrations..."
|
echo "Running migrations..."
|
||||||
/opt/canapeapp/migrate \
|
/opt/canapeapp/migrate \
|
||||||
-url postgres://${DB_USER}:${DB_PASS}@database:5432/${DB_ENV}?sslmode=disable \
|
-database "postgres://$DB_USER:$DB_PASS@database:5432/$DB_ENV?sslmode=disable" \
|
||||||
-path /opt/canapeapp/migrations \
|
-path /opt/canapeapp/migrations \
|
||||||
up
|
up
|
||||||
echo "Migrations done"
|
echo "Migrations done"
|
||||||
|
@ -1 +0,0 @@
|
|||||||
DELETE FROM users;
|
|
@ -1,2 +0,0 @@
|
|||||||
INSERT INTO users (name, hash, admin, activated) VALUES ('test', '$2a$10$QHx07iyuxO1RcehgtjMgjOzv03Bx2eeSKvsxkoj9oR2NJ4cklh6ue', false, true);
|
|
||||||
INSERT INTO users (name, hash, admin, activated) VALUES ('admin', '$2a$10$qAbyDZsHtcnhXhjhQZkD2uKlX72eMHsX8Hi2Cnl1vJUqHQiey2qa6', true, true);
|
|
@ -1,49 +1,12 @@
|
|||||||
package sqly
|
package sqly
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
|
||||||
"testing"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/jmoiron/sqlx"
|
|
||||||
"github.com/mattes/migrate/migrate"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var migrationPath string
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
migrationPath = os.Getenv("MIGRATION_PATH")
|
|
||||||
if migrationPath == "" {
|
|
||||||
migrationPath = "../../../sql/migration"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// BaseModel have to be embeded in all your struct which reflect a table
|
// BaseModel have to be embeded in all your struct which reflect a table
|
||||||
type BaseModel struct {
|
type BaseModel struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
Updated time.Time `db:"updated_at" json:"updated_at"`
|
Updated time.Time `db:"updated_at" json:"updated_at"`
|
||||||
Created time.Time `db:"created_at" json:"created_at"`
|
Created time.Time `db:"created_at" json:"created_at"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// RunWithLastestMigration runs your test with database migration set to the lastest
|
|
||||||
func RunWithLastestMigration(db *sqlx.DB, pgdsn string, t *testing.T, test func(db *sqlx.DB, t *testing.T)) {
|
|
||||||
defer func() {
|
|
||||||
allErrors, ok := migrate.DownSync(pgdsn, migrationPath)
|
|
||||||
if !ok {
|
|
||||||
for _, err := range allErrors {
|
|
||||||
t.Log(err)
|
|
||||||
t.Fatal("We get some errors when reset the database schema")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
allErrors, ok := migrate.UpSync(pgdsn, migrationPath)
|
|
||||||
if !ok {
|
|
||||||
for _, err := range allErrors {
|
|
||||||
t.Log(err)
|
|
||||||
t.Fatal("Impossible to run test we get some errors when initialize the database schema")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
test(db, t)
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user