Remove migrate functions from device/datastore and workflow/datastore

Add migrations into main (very messy indeed)
Fix bizarre glide dependency disappearing.
This commit is contained in:
Mosen
2016-06-18 15:05:38 +10:00
parent 5896d8ca90
commit 0633aeafef
4 changed files with 35 additions and 69 deletions

View File

@@ -241,45 +241,10 @@ func NewDB(driver, conn string, logger kitlog.Logger) (Datastore, error) {
if dbError != nil {
return nil, errors.Wrap(dbError, "device datastore")
}
migrate(db)
// TODO: Migrations
return pgStore{DB: db}, nil
default:
return nil, errors.New("unknown driver")
}
}
func migrate(db *sqlx.DB) {
schema := `
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE TABLE IF NOT EXISTS devices (
device_uuid uuid PRIMARY KEY
DEFAULT uuid_generate_v4(),
udid text NOT NULL DEFAULT '',
serial_number text,
os_version text,
model text NOT NULL DEFAULT '',
color text,
asset_tag text,
dep_profile_status text,
dep_profile_uuid text,
dep_profile_assign_time date,
dep_profile_push_time date,
dep_profile_assigned_date date,
dep_profile_assigned_by text,
description text NOT NULL DEFAULT '',
build_version text,
product_name text,
imei text NOT NULL DEFAULT '',
meid text NOT NULL DEFAULT '',
apple_mdm_token text,
apple_mdm_topic text,
apple_push_magic text,
mdm_enrolled boolean,
workflow_uuid text NOT NULL DEFAULT '',
dep_device boolean,
awaiting_configuration boolean
);
CREATE UNIQUE INDEX IF NOT EXISTS serial_idx ON devices (serial_number);`
db.MustExec(schema)
}

View File

@@ -25,7 +25,7 @@ import:
subpackages:
- context
- package: github.com/rs/cors
- package: ""
- name: github.com/RobotsAndPencils/buford
version: 2861f2a5612806afe88232539f5e4255d39ae544
subpackages:
- certificate

32
main.go
View File

@@ -21,6 +21,9 @@ import (
stdprometheus "github.com/prometheus/client_golang/prometheus"
"golang.org/x/net/context"
"github.com/rs/cors"
"github.com/DavidHuie/gomigrate"
"time"
"database/sql"
)
var (
@@ -113,6 +116,35 @@ func main() {
os.Exit(1)
}
// Run migrations
db, err := sql.Open("postgres", *flPGconn)
if err != nil {
logger.Log("err", err)
os.Exit(1)
}
var dbError error
maxAttempts := 20
for attempts := 1; attempts <= maxAttempts; attempts++ {
dbError = db.Ping()
if dbError == nil {
break
}
logger.Log("msg", fmt.Sprintf("could not connect to postgres: %v", dbError))
time.Sleep(time.Duration(attempts) * time.Second)
}
if dbError != nil {
logger.Log("err", dbError)
os.Exit(1)
}
migrator, _ := gomigrate.NewMigrator(db, gomigrate.Postgres{}, "./migrations")
migrationErr := migrator.Migrate()
if migrationErr != nil {
logger.Log("err", err)
os.Exit(1)
}
workflowDB, err := workflow.NewDB(
"postgres",
*flPGconn,

View File

@@ -83,40 +83,9 @@ func NewDB(driver, conn string, logger kitlog.Logger) (Datastore, error) {
if dbError != nil {
return nil, errors.Wrap(dbError, "workflow datastore")
}
migrate(db)
// TODO: Migrations
return pgStore{DB: db}, nil
default:
return nil, errors.New("unknown driver")
}
}
func migrate(db *sqlx.DB) {
schema := `
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE TABLE IF NOT EXISTS workflows (
workflow_uuid uuid PRIMARY KEY
DEFAULT uuid_generate_v4(),
name text UNIQUE NOT NULL CHECK (name <> '')
);
CREATE TABLE IF NOT EXISTS profiles (
profile_uuid uuid PRIMARY KEY
DEFAULT uuid_generate_v4(),
payload_identifier text UNIQUE NOT NULL CHECK (payload_identifier <> ''),
profile_data bytea
);
CREATE TABLE IF NOT EXISTS workflow_profile (
workflow_uuid uuid REFERENCES workflows,
profile_uuid uuid REFERENCES profiles,
PRIMARY KEY (workflow_uuid, profile_uuid)
);
CREATE TABLE IF NOT EXISTS workflow_workflow (
workflow_uuid uuid REFERENCES workflows,
included_workflow_uuid uuid REFERENCES workflows(workflow_uuid),
PRIMARY KEY (workflow_uuid, included_workflow_uuid)
);`
db.MustExec(schema)
}