-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
migrate database from Datastore (not psql)
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
Showing
6 changed files
with
86 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
)`, | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.