Skip to content

Instantly share code, notes, and snippets.

@benesch
Created April 4, 2017 15:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benesch/cce1a20c0981bc0d45c3a47165c47834 to your computer and use it in GitHub Desktop.
Save benesch/cce1a20c0981bc0d45c3a47165c47834 to your computer and use it in GitHub Desktop.
package main
import (
"database/sql"
_ "github.com/lib/pq"
)
func main() {
db, err := sql.Open("postgres", "postgres://root@localhost:26257/goose?sslmode=disable")
if err != nil {
panic(err)
}
_, err = db.Exec("CREATE DATABASE IF NOT EXISTS goose");
if err != nil {
panic(err)
}
txn, err := db.Begin()
if err != nil {
panic(err)
}
createVersionTableSql := `
CREATE TABLE goose_db_version (
id serial NOT NULL,
version_id bigint NOT NULL,
is_applied boolean NOT NULL,
tstamp timestamp NULL default now(),
PRIMARY KEY(id)
);`
if _, err := txn.Exec(createVersionTableSql); err != nil {
panic(err)
}
version := 0
applied := true
if _, err := db.Exec(
"INSERT INTO goose_db_version (version_id, is_applied) VALUES ($1, $2);", version, applied,
); err != nil {
txn.Rollback()
panic(err)
}
txn.Commit()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment