-
Notifications
You must be signed in to change notification settings - Fork 0
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
defany
committed
Feb 21, 2024
1 parent
20d39eb
commit 2f5e885
Showing
14 changed files
with
286 additions
and
42 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
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
File renamed without changes.
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,46 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"github.com/defany/auth-service/app/internal/app" | ||
"github.com/defany/auth-service/app/pkg/logger/sl" | ||
"github.com/defany/auth-service/app/pkg/postgres" | ||
"log/slog" | ||
) | ||
|
||
func main() { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
a := app.NewApp() | ||
|
||
db := a.DI().Database(ctx) | ||
|
||
log := a.DI().Log(ctx) | ||
|
||
migrator, err := postgres.NewMigrator(db.Pool(), a.DI().Config(ctx).Database.MigrationsDir) | ||
if err != nil { | ||
log.Error("failed to setup migrator", sl.ErrAttr(err)) | ||
|
||
return | ||
} | ||
|
||
log.Info("upping the migrations") | ||
|
||
upped, err := migrator.Up(ctx) | ||
if err != nil { | ||
log.Error("failed to up migrations", sl.ErrAttr(err)) | ||
|
||
return | ||
} | ||
|
||
if len(upped) == 0 { | ||
log.Info("there is no migrations to up") | ||
|
||
return | ||
} | ||
|
||
for _, migration := range upped { | ||
log.Info("migration upped!", slog.String("name", migration.Source.Path)) | ||
} | ||
} |
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
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,33 @@ | ||
package postgres | ||
|
||
import ( | ||
"context" | ||
"github.com/jackc/pgx/v5/pgxpool" | ||
"github.com/jackc/pgx/v5/stdlib" | ||
"github.com/pressly/goose/v3" | ||
"os" | ||
) | ||
|
||
type Migrator struct { | ||
provider *goose.Provider | ||
} | ||
|
||
func NewMigrator(db *pgxpool.Pool, migrationsDir string) (*Migrator, error) { | ||
provider, err := goose.NewProvider(goose.DialectPostgres, stdlib.OpenDBFromPool(db), os.DirFS(migrationsDir)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &Migrator{ | ||
provider: provider, | ||
}, nil | ||
} | ||
|
||
func (u *Migrator) Up(ctx context.Context) ([]*goose.MigrationResult, error) { | ||
res, err := u.provider.Up(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return res, nil | ||
} |
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
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
Oops, something went wrong.