Created
April 4, 2017 15:43
-
-
Save benesch/cce1a20c0981bc0d45c3a47165c47834 to your computer and use it in GitHub Desktop.
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
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