Skip to content

Commit

Permalink
migrate database from Datastore (not psql)
Browse files Browse the repository at this point in the history
This is following a pattern I saw here:

https://github.com/GoogleCloudPlatform/golang-samples/blob/master/getting-started/bookshelf/db_mysql.go

Keep the database setup statements as code, and allow the Datastore to
run through those statements itself. This means it can use the right
DATABASE_URL, and we can do it between tests if necessary.
  • Loading branch information
Paul Fawkesley committed Dec 5, 2018
1 parent 9d1dc46 commit 46bead0
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 63 deletions.
6 changes: 1 addition & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ run_collectors:

.PHONY: migrate
migrate:
./migrations/migrate migrations/*.sql

.PHONY: migrate_heroku
migrate_heroku:
MIGRATE_HEROKU=1 ./migrations/migrate migrations/*.sql
go run cmd/migrate/migrate.go

.PHONY: test
test:
Expand Down
21 changes: 21 additions & 0 deletions cmd/migrate/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import (
"fmt"
"github.com/fluidkeys/api/datastore"
)

func main() {
fmt.Print("Running database migrations.\n")

err := datastore.Initialize(datastore.MustReadDatabaseUrl())
if err != nil {
panic(err)
}
err = datastore.Migrate()
if err != nil {
panic(err)
}

fmt.Print("Done.\n")
}
44 changes: 30 additions & 14 deletions datastore/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,6 @@ import (

var db *sql.DB

func init() {
databaseUrl, present := os.LookupEnv("DATABASE_URL")

if !present {
panic("Missing DATABASE_URL, it should be e.g. " +
"postgres://vagrant:password@localhost:5432/vagrant")
}

err := Initialize(databaseUrl)
if err != nil {
panic(err)
}
}

// Initialize initialises a postgres database from the given databaseUrl
func Initialize(databaseUrl string) error {
var err error
Expand Down Expand Up @@ -169,6 +155,36 @@ func DeleteSecret(secretUUID uuid.UUID, recipientFingerprint fpr.Fingerprint) (f
return true, nil // found and deleted
}

func MustReadDatabaseUrl() string {
databaseUrl, present := os.LookupEnv("DATABASE_URL")

if !present {
panic("Missing DATABASE_URL, it should be e.g. " +
"postgres://vagrant:password@localhost:5432/vagrant")
}
return databaseUrl
}

func Migrate() error {
tx, err := db.Begin()
if err != nil {
return err
}

for _, sql := range migrateDatabaseStatements {
_, err := tx.Exec(sql)
if err != nil {
return fmt.Errorf("error (rolling back everything): %v", err)
}
}

err = tx.Commit()
if err != nil {
return err
}
return nil
}

func dbFormat(fingerprint fpr.Fingerprint) string {
return fmt.Sprintf("4:%s", fingerprint.Hex())
}
Expand Down
34 changes: 34 additions & 0 deletions datastore/schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package datastore

var migrateDatabaseStatements = []string{
`CREATE TABLE IF NOT EXISTS keys (
id BIGSERIAL PRIMARY KEY,
-- fingerprint is the uppercase hex version of the fingerprint,
-- prepended by the version number and a colon, e.g.
-- 4:A999B7498D1A8DC473E53C92309F635DAD1B5517
fingerprint VARCHAR UNIQUE NOT NULL,
armored_public_key TEXT NOT NULL
)`,

`CREATE TABLE IF NOT EXISTS email_key_link (
-- The email -> key mapping is many-to-one, e.g. an email will always resolve
-- to a single key, and multiple emails can point to the same key.
--
-- If the key is deleted, the email should be deleted too since it's not used
-- for anything but mapping to a key.
id BIGSERIAL PRIMARY KEY,
email VARCHAR UNIQUE NOT NULL,
key_id INT UNIQUE NOT NULL REFERENCES keys(id) ON DELETE CASCADE
)`,

`CREATE TABLE IF NOT EXISTS secrets (
id BIGSERIAL PRIMARY KEY,
uuid UUID UNIQUE NOT NULL,
created_at TIMESTAMP NOT NULL,
recipient_key_id INT NOT NULL REFERENCES keys(id) ON DELETE CASCADE,
armored_encrypted_secret TEXT NOT NULL
)`,
}
30 changes: 0 additions & 30 deletions migrations/0001_initial.sql

This file was deleted.

14 changes: 0 additions & 14 deletions migrations/migrate

This file was deleted.

0 comments on commit 46bead0

Please sign in to comment.