initial commit

This commit is contained in:
dhax 2017-09-25 18:23:11 +02:00
commit 93d8310491
46 changed files with 3379 additions and 0 deletions

View file

@ -0,0 +1,65 @@
package migrate
import (
"fmt"
"github.com/go-pg/migrations"
)
const AccountTable = `
CREATE TABLE accounts (
id serial NOT NULL,
created_at timestamp with time zone NOT NULL DEFAULT current_timestamp,
updated_at timestamp with time zone DEFAULT current_timestamp,
last_login timestamp with time zone NOT NULL DEFAULT current_timestamp,
email text NOT NULL UNIQUE,
name text NOT NULL,
active boolean NOT NULL DEFAULT TRUE,
roles text[] NOT NULL DEFAULT '{"user"}',
PRIMARY KEY (id)
)`
const TokenTable = `
CREATE TABLE tokens (
id serial NOT NULL,
created_at timestamp with time zone NOT NULL DEFAULT current_timestamp,
updated_at timestamp with time zone NOT NULL DEFAULT current_timestamp,
account_id int NOT NULL REFERENCES accounts(id),
token text NOT NULL UNIQUE,
expiry timestamp with time zone NOT NULL,
mobile boolean NOT NULL DEFAULT FALSE,
identifier text,
PRIMARY KEY (id)
)`
func init() {
up := []string{
AccountTable,
TokenTable,
}
down := []string{
`DROP TABLE tokens`,
`DROP TABLE accounts`,
}
migrations.Register(func(db migrations.DB) error {
fmt.Println("creating initial tables")
for _, q := range up {
_, err := db.Exec(q)
if err != nil {
return err
}
}
return nil
}, func(db migrations.DB) error {
fmt.Println("dropping initial tables")
for _, q := range down {
_, err := db.Exec(q)
if err != nil {
return err
}
}
return nil
})
}

View file

@ -0,0 +1,48 @@
package migrate
import (
"fmt"
"github.com/go-pg/migrations"
)
const bootstrapAdminAccount = `
INSERT INTO accounts (id, email, name, active, roles)
VALUES (DEFAULT, 'admin@boot.io', 'Admin Boot', true, '{admin}')
`
const bootstrapUserAccount = `
INSERT INTO accounts (id, email, name, active)
VALUES (DEFAULT, 'user@boot.io', 'User Boot', true)
`
func init() {
up := []string{
bootstrapAdminAccount,
bootstrapUserAccount,
}
down := []string{
`TRUNCATE accounts CASCADE`,
}
migrations.Register(func(db migrations.DB) error {
fmt.Println("add bootstrap accounts")
for _, q := range up {
_, err := db.Exec(q)
if err != nil {
return err
}
}
return nil
}, func(db migrations.DB) error {
fmt.Println("truncate accounts cascading")
for _, q := range down {
_, err := db.Exec(q)
if err != nil {
return err
}
}
return nil
})
}

View file

@ -0,0 +1,53 @@
package migrate
import (
"fmt"
"github.com/go-pg/migrations"
)
const ProfileTable = `
CREATE TABLE profiles (
id serial NOT NULL,
created_at timestamp with time zone NOT NULL DEFAULT current_timestamp,
updated_at timestamp with time zone NOT NULL DEFAULT current_timestamp,
account_id int NOT NULL REFERENCES accounts(id),
theme text NOT NULL DEFAULT 'default',
PRIMARY KEY (id)
)`
const bootstrapAccountProfiles = `
INSERT INTO profiles(account_id) VALUES(1);
INSERT INTO profiles(account_id) VALUES(2);
`
func init() {
up := []string{
ProfileTable,
bootstrapAccountProfiles,
}
down := []string{
`DROP TABLE profiles`,
}
migrations.Register(func(db migrations.DB) error {
fmt.Println("create profile table")
for _, q := range up {
_, err := db.Exec(q)
if err != nil {
return err
}
}
return nil
}, func(db migrations.DB) error {
fmt.Println("drop profile table")
for _, q := range down {
_, err := db.Exec(q)
if err != nil {
return err
}
}
return nil
})
}

70
database/migrate/main.go Normal file
View file

@ -0,0 +1,70 @@
package migrate
import (
"fmt"
"os"
"github.com/dhax/go-base/database"
"github.com/go-pg/migrations"
"github.com/go-pg/pg"
)
func Migrate(args []string) {
db, err := database.DBConn()
if err != nil {
panic(err)
}
err = db.RunInTransaction(func(tx *pg.Tx) error {
oldVersion, newVersion, err := migrations.Run(tx, args...)
if err != nil {
return err
}
if newVersion != oldVersion {
fmt.Printf("migrated from version %d to %d\n", oldVersion, newVersion)
} else {
fmt.Printf("version is %d\n", oldVersion)
}
return nil
})
if err != nil {
exitf(err.Error())
}
}
func Reset() {
db, err := database.DBConn()
if err != nil {
exitf(err.Error())
}
version, err := migrations.Version(db)
if err != nil {
exitf(err.Error())
}
err = db.RunInTransaction(func(tx *pg.Tx) error {
for version != 0 {
oldVersion, newVersion, err := migrations.Run(tx, "down")
if err != nil {
return err
}
fmt.Printf("migrated from version %d to %d\n", oldVersion, newVersion)
version = newVersion
}
return nil
})
if err != nil {
exitf(err.Error())
}
}
func errorf(s string, args ...interface{}) {
fmt.Fprintf(os.Stderr, s+"\n", args...)
}
func exitf(s string, args ...interface{}) {
errorf(s, args...)
os.Exit(1)
}