diff --git a/device/datastore.go b/device/datastore.go index aff3d2ad..ddff1662 100644 --- a/device/datastore.go +++ b/device/datastore.go @@ -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) -} diff --git a/glide.yaml b/glide.yaml index a31322c0..36cf2d2b 100644 --- a/glide.yaml +++ b/glide.yaml @@ -25,7 +25,7 @@ import: subpackages: - context - package: github.com/rs/cors -- package: "" +- name: github.com/RobotsAndPencils/buford version: 2861f2a5612806afe88232539f5e4255d39ae544 subpackages: - certificate diff --git a/main.go b/main.go index 8c60c0ed..69a9eecb 100644 --- a/main.go +++ b/main.go @@ -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, diff --git a/workflow/datastore.go b/workflow/datastore.go index 5757643c..ddbd96b3 100644 --- a/workflow/datastore.go +++ b/workflow/datastore.go @@ -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) -}