-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
462f992
commit 559f6f2
Showing
8 changed files
with
134 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,44 @@ | ||
package cmd | ||
|
||
import ( | ||
"log" | ||
"os" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/kenji-yamane/mgr8/drivers" | ||
) | ||
|
||
var defaultDriver = "postgres" | ||
|
||
type apply struct { | ||
|
||
} | ||
|
||
func (a *apply) Execute(cmd *cobra.Command, args []string){ | ||
fileName := args[0] | ||
|
||
driverName := defaultDriver | ||
if len(args) > 1 { | ||
driverName = args[1] | ||
} | ||
|
||
log.Printf("Running migrations from file %s with driver %s", fileName, driverName) | ||
content, err := os.ReadFile(fileName) | ||
if err != nil { | ||
log.Fatal("could not read from file") | ||
} | ||
|
||
driver, err := drivers.GetDriver(driverName) | ||
if err != nil { | ||
log.Fatal("could not read from file") | ||
} | ||
|
||
statements := strings.Split(string(content), ";") | ||
err = driver.Execute(statements) | ||
if err != nil { | ||
log.Fatal("could not execute transaction") | ||
} | ||
|
||
} |
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,13 @@ | ||
version: '3.7' | ||
|
||
services: | ||
postgres: | ||
image: postgres:13-alpine | ||
environment: | ||
POSTGRES_USER: root | ||
POSTGRES_PASSWORD: root | ||
POSTGRES_DB: core | ||
command: [ "postgres", "-c", "log_statement=all" ] | ||
ports: | ||
- "5432:5432/tcp" | ||
restart: always |
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 |
---|---|---|
@@ -1 +1,19 @@ | ||
package drivers | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/kenji-yamane/mgr8/drivers/postgres" | ||
) | ||
|
||
type Driver interface { | ||
Execute(statements []string) error | ||
} | ||
|
||
func GetDriver(driverName string) (Driver, error) { | ||
switch driverName { | ||
case "postgres": | ||
return postgres.NewPostgresDriver(), nil | ||
} | ||
return nil, fmt.Errorf("inexistent driver %s", driverName) | ||
} |
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 |
---|---|---|
@@ -1 +1,40 @@ | ||
package postgres | ||
|
||
import ( | ||
"log" | ||
_ "github.com/lib/pq" | ||
"github.com/jmoiron/sqlx" | ||
) | ||
|
||
type postgresDriver struct { | ||
|
||
} | ||
|
||
func NewPostgresDriver() *postgresDriver{ | ||
return &postgresDriver{} | ||
} | ||
|
||
func (p *postgresDriver) Execute(statements []string) error{ | ||
db, err := sqlx.Connect("postgres", "user=root dbname=core password=root sslmode=disable") | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
|
||
tx, err := db.Begin() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, stmt := range statements { | ||
_, err := tx.Exec(stmt) | ||
if err != nil { | ||
err2 := tx.Rollback() | ||
if err2 != nil { | ||
return err2 | ||
} | ||
return err | ||
} | ||
} | ||
|
||
return tx.Commit() | ||
} |
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
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,5 @@ | ||
CREATE TABLE users ( | ||
social_number VARCHAR(9) PRIMARY KEY, | ||
name VARCHAR(15), | ||
phone VARCHAR(11) | ||
); |