Created
April 28, 2022 14:30
-
-
Save mehmetcemyucel/3785084c5a4eb3df27eb3d7f82b6dd48 to your computer and use it in GitHub Desktop.
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
| package migrate | |
| import ( | |
| "github.com/golang-migrate/migrate/v4" | |
| _ "github.com/golang-migrate/migrate/v4/database/postgres" | |
| _ "github.com/golang-migrate/migrate/v4/source/file" | |
| "go.uber.org/zap" | |
| "helloWorld/pkg/config/config" | |
| ) | |
| type Mig struct { | |
| Migration *migrate.Migrate | |
| } | |
| func NewMig(c config.PostgreConfig, log *zap.Logger) *Mig { | |
| fullConnStr := "postgres://" + c.Username + ":" + c.Password + "@" + c.Host + ":" + c.Port + "/" + c.Name | |
| if !c.Ssl { | |
| fullConnStr = fullConnStr + "?sslmode=disable" | |
| } | |
| log.Info("Migration has started with; ", zap.String("connUrl", fullConnStr)) | |
| var m, err = migrate.New("file://db", fullConnStr) | |
| if err != nil { | |
| log.Fatal("Migration create has an error", zap.Error(err)) | |
| } | |
| if err := m.Up(); err != nil { | |
| log.Warn("Migration has an error ", zap.Error(err)) | |
| } | |
| log.Info("Migration finished successfully") | |
| return &Mig{m} | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment