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
Showing
13 changed files
with
823 additions
and
6 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 |
---|---|---|
|
@@ -22,3 +22,6 @@ bin/ | |
# golang migrate | ||
migrate.darwin-amd64 | ||
migrate.linux-amd64 | ||
|
||
# dot env | ||
.env |
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,63 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"os" | ||
|
||
"github.com/allisson/go-env" | ||
"github.com/allisson/postmand/repository" | ||
"github.com/allisson/postmand/service" | ||
"github.com/jmoiron/sqlx" | ||
_ "github.com/joho/godotenv/autoload" | ||
"github.com/urfave/cli/v2" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func main() { | ||
// Setup logger | ||
logger, err := zap.NewProduction() | ||
if err != nil { | ||
log.Fatalf("logger-setup-error: %v\n", err) | ||
} | ||
// nolint:errcheck | ||
defer logger.Sync() | ||
|
||
// Setup postgresql database | ||
db, err := sqlx.Open("postgres", env.GetString("POSTMAND_DATABASE_URL", "")) | ||
if err != nil { | ||
logger.Fatal("database-setup-error", zap.Error(err)) | ||
} | ||
if err := db.Ping(); err != nil { | ||
logger.Fatal("database-ping-error", zap.Error(err)) | ||
} | ||
|
||
// Setup cli | ||
app := cli.NewApp() | ||
app.Name = "postmand" | ||
app.Usage = "CLI" | ||
app.Authors = []*cli.Author{ | ||
{ | ||
Name: "Allisson Azevedo", | ||
Email: "allisson@gmail.com", | ||
}, | ||
} | ||
app.Commands = []*cli.Command{ | ||
{ | ||
Name: "migrate", | ||
Aliases: []string{"m"}, | ||
Usage: "execute database migration", | ||
Action: func(c *cli.Context) error { | ||
migrationRepository := repository.NewMigration( | ||
db, | ||
env.GetString("POSTMAND_DATABASE_MIGRATION_DIR", "file:///db/migrations"), | ||
) | ||
migrationService := service.NewMigration(migrationRepository) | ||
return migrationService.Run(c.Context) | ||
}, | ||
}, | ||
} | ||
err = app.Run(os.Args) | ||
if err != nil { | ||
logger.Fatal("cli-error", zap.Error(err)) | ||
} | ||
} |
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,3 @@ | ||
POSTMAND_DATABASE_URL='postgres://user:pass@localhost:5432/postmand?sslmode=disable' | ||
# See https://github.com/golang-migrate/migrate/tree/master/source/file | ||
POSTMAND_DATABASE_MIGRATION_DIR='file://db/migrations' |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,50 @@ | ||
package repository | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/golang-migrate/migrate/v4" | ||
"github.com/golang-migrate/migrate/v4/database/postgres" | ||
|
||
// For migrate with files | ||
_ "github.com/golang-migrate/migrate/v4/source/file" | ||
"github.com/jmoiron/sqlx" | ||
) | ||
|
||
// Migration implements postmand.MigrationRepository interface. | ||
type Migration struct { | ||
db *sqlx.DB | ||
migrationDir string | ||
} | ||
|
||
// Run migrations | ||
func (m *Migration) Run(ctx context.Context) error { | ||
driver, err := postgres.WithInstance(m.db.DB, &postgres.Config{}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
mg, err := migrate.NewWithDatabaseInstance(m.migrationDir, "postgres", driver) | ||
if err != nil { | ||
fmt.Println(err) | ||
return err | ||
} | ||
|
||
err = mg.Up() | ||
if err != nil { | ||
if err != migrate.ErrNoChange { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// NewMigration will create an implementation of postmand.MigrationRepository. | ||
func NewMigration(db *sqlx.DB, migrationDir string) *Migration { | ||
return &Migration{ | ||
db: db, | ||
migrationDir: migrationDir, | ||
} | ||
} |
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,18 @@ | ||
package repository | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMigration(t *testing.T) { | ||
ctx := context.Background() | ||
th := newTestHelper() | ||
defer th.db.Close() | ||
migrationDir := "file://../db/migrations" | ||
migration := NewMigration(th.db, migrationDir) | ||
err := migration.Run(ctx) | ||
assert.Nil(t, err) | ||
} |
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,22 @@ | ||
package service | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/allisson/postmand" | ||
) | ||
|
||
// Migration implements postmand.MigrationService interface. | ||
type Migration struct { | ||
migrationRepo postmand.MigrationRepository | ||
} | ||
|
||
// Run database migrations. | ||
func (m Migration) Run(ctx context.Context) error { | ||
return m.migrationRepo.Run(ctx) | ||
} | ||
|
||
// NewMigration will create an implementation of postmand.MigrationService. | ||
func NewMigration(migrationRepo postmand.MigrationRepository) *Migration { | ||
return &Migration{migrationRepo: migrationRepo} | ||
} |
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,20 @@ | ||
package service | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/allisson/postmand/mocks" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
) | ||
|
||
func TestMigration(t *testing.T) { | ||
migrationRepository := &mocks.MigrationRepository{} | ||
migrationService := NewMigration(migrationRepository) | ||
migrationRepository.On("Run", mock.Anything).Return(nil) | ||
ctx := context.Background() | ||
err := migrationService.Run(ctx) | ||
assert.Nil(t, err) | ||
migrationRepository.AssertExpectations(t) | ||
} |