Skip to content

Commit

Permalink
run script
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandremr01 committed Apr 5, 2022
1 parent 462f992 commit 559f6f2
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 9 deletions.
43 changes: 43 additions & 0 deletions cmd/apply.go
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")
}

}
16 changes: 7 additions & 9 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,30 @@ import (
"github.com/spf13/cobra"
)

var (
rootCmd = &cobra.Command{
func Execute() {
rootCmd := &cobra.Command{
Use: "mgr8",
Short: "mgr8 is a framework-agnostic database migrations tool",
Long: `Lonog version: MGR8 a framework agnostic database migrations tool
sbrubbles
sbrubbles`,
}

generateCmd = &cobra.Command{
generateCmd := &cobra.Command{
Use: "generate",
Short: "generate creates migration script based on the diff between schema versions",
}

applyCmd = &cobra.Command{
applyCommand := apply{}

applyCmd := &cobra.Command{
Use: "apply",
Short: "apply runs migrations in the selected database",
Run: applyCommand.Execute,
}
)

func init() {
rootCmd.AddCommand(generateCmd)
rootCmd.AddCommand(applyCmd)
}

func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
Expand Down
13 changes: 13 additions & 0 deletions docker-compose.yml
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
18 changes: 18 additions & 0 deletions drivers/drivers.go
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)
}
39 changes: 39 additions & 0 deletions drivers/postgres/parser.go
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()
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ go 1.17

require (
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jmoiron/sqlx v1.3.4 // indirect
github.com/lib/pq v1.10.4 // indirect
github.com/spf13/cobra v1.4.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
)
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jmoiron/sqlx v1.3.4 h1:wv+0IJZfL5z0uZoUjlpKgHkgaFSYD+r9CfrXjEXsO7w=
github.com/jmoiron/sqlx v1.3.4/go.mod h1:2BljVx/86SuTyjE+aPYlHCTNvZrnJXghYGpNiXLBMCQ=
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.10.4 h1:SO9z7FRPzA03QhHKJrH5BXA6HU1rS4V2nIVrrNC1iYk=
github.com/lib/pq v1.10.4/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.4.0 h1:y+wJpx64xcgO1V+RcnwW0LEHxTKRi2ZDPSBjWnrg88Q=
github.com/spf13/cobra v1.4.0/go.mod h1:Wo4iy3BUC+X2Fybo0PDqwJIv3dNRiZLHQymsfxlB84g=
Expand Down
5 changes: 5 additions & 0 deletions migrations/test_migration.sql
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)
);

0 comments on commit 559f6f2

Please sign in to comment.