-
Notifications
You must be signed in to change notification settings - Fork 8
Create postgres driver #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MakoTano
wants to merge
8
commits into
master
Choose a base branch
from
create_postgres_driver
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
fa53e09
WIP - create postgres driver
MakoTano a7416a4
delete develop section on config.tml
MakoTano 80df40f
add sample sqls
MakoTano baf700e
add github.com/lib/pq to .travis.yml
MakoTano 109faad
rename example files
MakoTano 9fd5215
add examples files
MakoTano 819976e
uncomment mysql driver init
MakoTano 8dbd311
create develop sample like mysql
MakoTano File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,217 @@ | ||
| package driver | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "database/sql" | ||
| "errors" | ||
| "fmt" | ||
| "strings" | ||
| "sync" | ||
|
|
||
| "github.com/eure/kamimai/core" | ||
| "github.com/lib/pq" | ||
| ) | ||
|
|
||
| type ( | ||
| // Postgres driver object. | ||
| Postgres struct { | ||
| db *sql.DB | ||
| tx *sql.Tx | ||
| mu sync.Mutex | ||
| } | ||
| ) | ||
|
|
||
| // Open is the first function to be called. | ||
| // Check the dsn string and open and verify any connection | ||
| // that has to be made. | ||
| func (d *Postgres) Open(dsn string) error { | ||
| z := strings.SplitN(dsn, "postgres:", 2) | ||
| if len(z) != 2 { | ||
| return errors.New("invalid data source name of postgres") | ||
| } | ||
|
|
||
| db, err := sql.Open("postgres", z[1]) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if err := db.Ping(); err != nil { | ||
| return err | ||
| } | ||
| d.db = db | ||
|
|
||
| return d.Version().Create() | ||
| } | ||
|
|
||
| // Close is the last function to be called. | ||
| // Close any open connection here. | ||
| func (d *Postgres) Close() error { | ||
| return d.db.Close() | ||
| } | ||
|
|
||
| // Ext returns the sql file extension used by path. The extension is the | ||
| // suffix beginning at the final dot in the final element of path; it is | ||
| // empty if there is no dot. | ||
| func (d *Postgres) Ext() string { | ||
| return ".sql" | ||
| } | ||
|
|
||
| // Transaction starts a db transaction. The isolation level is dependent on the | ||
| // driver. | ||
| func (d *Postgres) Transaction(fn func(*sql.Tx) error) error { | ||
| d.mu.Lock() | ||
| defer func() { | ||
| d.tx = nil | ||
| d.mu.Unlock() | ||
| }() | ||
|
|
||
| tx, err := d.db.Begin() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| d.tx = tx | ||
|
|
||
| // Procedure | ||
| if err := fn(d.tx); err != nil { | ||
| if rberr := d.tx.Rollback(); rberr != nil { | ||
| return rberr | ||
| } | ||
| return err | ||
| } | ||
|
|
||
| // Commit | ||
| if err := d.tx.Commit(); err != nil { | ||
| if rberr := d.tx.Rollback(); rberr != nil { | ||
| return rberr | ||
| } | ||
| return err | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // Exec executes a query without returning any rows. The args are for any | ||
| // placeholder parameters in the query. | ||
| func (d *Postgres) Exec(query string, args ...interface{}) (sql.Result, error) { | ||
| if d.tx != nil { | ||
| return d.tx.Exec(query, args...) | ||
| } | ||
| return d.db.Exec(query, args...) | ||
| } | ||
|
|
||
| // Version returns a version interface. | ||
| func (d *Postgres) Version() core.Version { | ||
| return d | ||
| } | ||
|
|
||
| // Migrate applies migration file. | ||
| func (d *Postgres) Migrate(m *core.Migration) error { | ||
| b, err := m.Read() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| stmts := bytes.Split(b, []byte(";")) | ||
| for _, stmt := range stmts { | ||
| query := strings.TrimSpace(string(stmt)) | ||
| if len(query) == 0 { | ||
| continue | ||
| } | ||
| _, err = d.Exec(query) | ||
| if err != nil { | ||
| isWarn := strings.Contains(err.Error(), pq.Ewarning) | ||
| if !isWarn { | ||
| return err | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // Insert inserts the given migration version. | ||
| func (d *Postgres) Insert(val uint64) error { | ||
| query := fmt.Sprintf(`INSERT INTO %s (version) VALUES (%d)`, | ||
| versionTableName, val) | ||
|
|
||
| _, err := d.Exec(query) | ||
| if err != nil { | ||
| isWarn := strings.Contains(err.Error(), pq.Ewarning) | ||
| if !isWarn { | ||
| return err | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // Delete deletes the given migration version. | ||
| func (d *Postgres) Delete(val uint64) error { | ||
| query := fmt.Sprintf(`DELETE FROM %s WHERE version = %d`, | ||
| versionTableName, val) | ||
|
|
||
| _, err := d.Exec(query) | ||
| if err != nil { | ||
| isWarn := strings.Contains(err.Error(), pq.Ewarning) | ||
| if !isWarn { | ||
| return err | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // Count counts number of row the given migration version. | ||
| func (d *Postgres) Count(val uint64) int { | ||
| query := fmt.Sprintf(`SELECT count(version) count FROM %s WHERE version = %d`, | ||
| versionTableName, val) | ||
|
|
||
| var count int | ||
| if err := d.db.QueryRow(query).Scan(&count); err != nil { | ||
| return 0 | ||
| } | ||
| return count | ||
| } | ||
|
|
||
| // Current returns the current migration version. | ||
| func (d *Postgres) Current() (uint64, error) { | ||
| const query = `SELECT version FROM ` + | ||
| versionTableName + ` ORDER BY version DESC LIMIT 1` | ||
|
|
||
| var version uint64 | ||
| err := d.db.QueryRow(query).Scan(&version) | ||
| switch { | ||
| case err == sql.ErrNoRows: | ||
| return 0, nil | ||
| case err != nil: | ||
| return 0, err | ||
| } | ||
| return version, nil | ||
| } | ||
|
|
||
| // Create creates | ||
| func (d *Postgres) Create() error { | ||
| const query = `CREATE TABLE IF NOT EXISTS ` + | ||
| versionTableName + ` (version BIGINT NOT NULL PRIMARY KEY);` | ||
|
|
||
| _, err := d.Exec(query) | ||
|
|
||
| if err != nil { | ||
| isWarn := strings.Contains(err.Error(), pq.Ewarning) | ||
| if !isWarn { | ||
| return err | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // Drop drops | ||
| func (d *Postgres) Drop() error { | ||
| const query = `DROP TABLE IF EXISTS ` + versionTableName | ||
|
|
||
| _, err := d.Exec(query) | ||
| if err != nil { | ||
| isWarn := strings.Contains(err.Error(), pq.Ewarning) | ||
| if !isWarn { | ||
| return err | ||
| } | ||
| } | ||
| return nil | ||
| } |
This file contains hidden or 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,8 @@ | ||
| [develop] | ||
| driver = "postgres" | ||
| dsn = "postgres:user=$POSTGRES_USER dbname=hogedb password=$POSTGRES_PASSWORD sslmode=disable" | ||
|
|
||
| [test1] | ||
| driver = "postgres" | ||
| dsn = "postgres:user=$POSTGRES_USER dbname=hogedb password=$POSTGRES_PASSWORD sslmode=disable" | ||
| directory = "test1" |
This file contains hidden or 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 @@ | ||
| DROP TABLE product; |
This file contains hidden or 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,4 @@ | ||
| CREATE TABLE product ( | ||
| id int4 PRIMARY KEY, | ||
| name text | ||
| ); |
This file contains hidden or 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 @@ | ||
| DROP TABLE product; |
This file contains hidden or 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,4 @@ | ||
| CREATE TABLE product ( | ||
| id int4 PRIMARY KEY, | ||
| name text | ||
| ); |
This file contains hidden or 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 @@ | ||
| DELETE FROM product WHERE id in (1, 2); |
This file contains hidden or 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 @@ | ||
| INSERT INTO product (id, name) VALUES (1, 'prod1'), (2, 'prod2'); |
This file contains hidden or 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 @@ | ||
| DELETE FROM product WHERE id in (3, 4); |
This file contains hidden or 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 @@ | ||
| INSERT INTO product (id, name) VALUES (3, 'prod3'), (4, 'prod4'); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have any idea to resolve this..