cloudflare/cfssl_trust
Publicmirrored from https://github.com/cloudflare/cfssl_trustAvailable
vendor/github.com/mattes/migrate/driver/postgres/postgres.go
127lines · modecode
| 1 | // Package postgres implements the Driver interface. |
| 2 | package postgres |
| 3 | |
| 4 | import ( |
| 5 | "database/sql" |
| 6 | "errors" |
| 7 | "fmt" |
| 8 | "strconv" |
| 9 | |
| 10 | "github.com/lib/pq" |
| 11 | "github.com/mattes/migrate/driver" |
| 12 | "github.com/mattes/migrate/file" |
| 13 | "github.com/mattes/migrate/migrate/direction" |
| 14 | ) |
| 15 | |
| 16 | type Driver struct { |
| 17 | db *sql.DB |
| 18 | } |
| 19 | |
| 20 | const tableName = "schema_migrations" |
| 21 | |
| 22 | func (driver *Driver) Initialize(url string) error { |
| 23 | db, err := sql.Open("postgres", url) |
| 24 | if err != nil { |
| 25 | return err |
| 26 | } |
| 27 | if err := db.Ping(); err != nil { |
| 28 | return err |
| 29 | } |
| 30 | driver.db = db |
| 31 | |
| 32 | if err := driver.ensureVersionTableExists(); err != nil { |
| 33 | return err |
| 34 | } |
| 35 | return nil |
| 36 | } |
| 37 | |
| 38 | func (driver *Driver) Close() error { |
| 39 | if err := driver.db.Close(); err != nil { |
| 40 | return err |
| 41 | } |
| 42 | return nil |
| 43 | } |
| 44 | |
| 45 | func (driver *Driver) ensureVersionTableExists() error { |
| 46 | if _, err := driver.db.Exec("CREATE TABLE IF NOT EXISTS " + tableName + " (version int not null primary key);"); err != nil { |
| 47 | return err |
| 48 | } |
| 49 | return nil |
| 50 | } |
| 51 | |
| 52 | func (driver *Driver) FilenameExtension() string { |
| 53 | return "sql" |
| 54 | } |
| 55 | |
| 56 | func (driver *Driver) Migrate(f file.File, pipe chan interface{}) { |
| 57 | defer close(pipe) |
| 58 | pipe <- f |
| 59 | |
| 60 | tx, err := driver.db.Begin() |
| 61 | if err != nil { |
| 62 | pipe <- err |
| 63 | return |
| 64 | } |
| 65 | |
| 66 | if f.Direction == direction.Up { |
| 67 | if _, err := tx.Exec("INSERT INTO "+tableName+" (version) VALUES ($1)", f.Version); err != nil { |
| 68 | pipe <- err |
| 69 | if err := tx.Rollback(); err != nil { |
| 70 | pipe <- err |
| 71 | } |
| 72 | return |
| 73 | } |
| 74 | } else if f.Direction == direction.Down { |
| 75 | if _, err := tx.Exec("DELETE FROM "+tableName+" WHERE version=$1", f.Version); err != nil { |
| 76 | pipe <- err |
| 77 | if err := tx.Rollback(); err != nil { |
| 78 | pipe <- err |
| 79 | } |
| 80 | return |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | if err := f.ReadContent(); err != nil { |
| 85 | pipe <- err |
| 86 | return |
| 87 | } |
| 88 | |
| 89 | if _, err := tx.Exec(string(f.Content)); err != nil { |
| 90 | pqErr := err.(*pq.Error) |
| 91 | offset, err := strconv.Atoi(pqErr.Position) |
| 92 | if err == nil && offset >= 0 { |
| 93 | lineNo, columnNo := file.LineColumnFromOffset(f.Content, offset-1) |
| 94 | errorPart := file.LinesBeforeAndAfter(f.Content, lineNo, 5, 5, true) |
| 95 | pipe <- errors.New(fmt.Sprintf("%s %v: %s in line %v, column %v:\n\n%s", pqErr.Severity, pqErr.Code, pqErr.Message, lineNo, columnNo, string(errorPart))) |
| 96 | } else { |
| 97 | pipe <- errors.New(fmt.Sprintf("%s %v: %s", pqErr.Severity, pqErr.Code, pqErr.Message)) |
| 98 | } |
| 99 | |
| 100 | if err := tx.Rollback(); err != nil { |
| 101 | pipe <- err |
| 102 | } |
| 103 | return |
| 104 | } |
| 105 | |
| 106 | if err := tx.Commit(); err != nil { |
| 107 | pipe <- err |
| 108 | return |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | func (driver *Driver) Version() (uint64, error) { |
| 113 | var version uint64 |
| 114 | err := driver.db.QueryRow("SELECT version FROM " + tableName + " ORDER BY version DESC LIMIT 1").Scan(&version) |
| 115 | switch { |
| 116 | case err == sql.ErrNoRows: |
| 117 | return 0, nil |
| 118 | case err != nil: |
| 119 | return 0, err |
| 120 | default: |
| 121 | return version, nil |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | func init() { |
| 126 | driver.RegisterDriver("postgres", &Driver{}) |
| 127 | } |
| 128 | |