2024-01-14 15:53:37 +00:00
|
|
|
package migrator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
2024-01-15 10:33:24 +00:00
|
|
|
|
2024-01-14 15:53:37 +00:00
|
|
|
"git.gocasts.ir/ebhomengo/niki/repository/mysql"
|
|
|
|
migrate "github.com/rubenv/sql-migrate"
|
|
|
|
)
|
|
|
|
|
2024-08-06 11:49:46 +00:00
|
|
|
type Config struct {
|
|
|
|
MysqlConfig mysql.Config
|
|
|
|
MigrationPath string
|
|
|
|
MigrationDBName string
|
|
|
|
}
|
|
|
|
|
2024-01-14 15:53:37 +00:00
|
|
|
type Migrator struct {
|
2024-08-06 11:49:46 +00:00
|
|
|
cfg Config
|
2024-01-14 15:53:37 +00:00
|
|
|
dialect string
|
|
|
|
migrations *migrate.FileMigrationSource
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO - set migration table name
|
|
|
|
// TODO - add limit to Up and Down method
|
|
|
|
|
2024-08-06 11:49:46 +00:00
|
|
|
func New(cfg Config) Migrator {
|
2024-01-14 15:53:37 +00:00
|
|
|
// OR: Read migrations from a folder:
|
|
|
|
migrations := &migrate.FileMigrationSource{
|
2024-08-06 11:49:46 +00:00
|
|
|
Dir: cfg.MigrationPath,
|
2024-01-14 15:53:37 +00:00
|
|
|
}
|
|
|
|
|
2024-08-06 11:49:46 +00:00
|
|
|
return Migrator{cfg: cfg, dialect: "mysql", migrations: migrations}
|
2024-01-14 15:53:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m Migrator) Up() {
|
2024-08-06 11:49:46 +00:00
|
|
|
migrate.SetTable(m.cfg.MigrationDBName)
|
|
|
|
|
2024-01-14 15:53:37 +00:00
|
|
|
db, err := sql.Open(m.dialect, fmt.Sprintf("%s:%s@(%s:%d)/%s?parseTime=true",
|
2024-08-06 11:49:46 +00:00
|
|
|
m.cfg.MysqlConfig.Username, m.cfg.MysqlConfig.Password, m.cfg.MysqlConfig.Host, m.cfg.MysqlConfig.Port, m.cfg.MysqlConfig.DBName))
|
2024-01-14 15:53:37 +00:00
|
|
|
if err != nil {
|
2024-01-15 10:33:24 +00:00
|
|
|
panic(fmt.Errorf("can't open mysql db: %w", err))
|
2024-01-14 15:53:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
n, err := migrate.Exec(db, m.dialect, m.migrations, migrate.Up)
|
|
|
|
if err != nil {
|
2024-01-15 10:33:24 +00:00
|
|
|
panic(fmt.Errorf("can't apply migrations: %w", err))
|
2024-01-14 15:53:37 +00:00
|
|
|
}
|
|
|
|
fmt.Printf("Applied %d migrations!\n", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m Migrator) Down() {
|
2024-08-06 11:49:46 +00:00
|
|
|
migrate.SetTable(m.cfg.MigrationDBName)
|
|
|
|
|
2024-01-14 15:53:37 +00:00
|
|
|
db, err := sql.Open(m.dialect, fmt.Sprintf("%s:%s@(%s:%d)/%s?parseTime=true",
|
2024-08-06 11:49:46 +00:00
|
|
|
m.cfg.MysqlConfig.Username, m.cfg.MysqlConfig.Password, m.cfg.MysqlConfig.Host, m.cfg.MysqlConfig.Port, m.cfg.MysqlConfig.DBName))
|
2024-01-14 15:53:37 +00:00
|
|
|
if err != nil {
|
2024-01-15 10:33:24 +00:00
|
|
|
panic(fmt.Errorf("can't open mysql db: %w", err))
|
2024-01-14 15:53:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
n, err := migrate.Exec(db, m.dialect, m.migrations, migrate.Down)
|
|
|
|
if err != nil {
|
2024-01-15 10:33:24 +00:00
|
|
|
panic(fmt.Errorf("can't rollback migrations: %w", err))
|
2024-01-14 15:53:37 +00:00
|
|
|
}
|
|
|
|
fmt.Printf("Rollbacked %d migrations!\n", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m Migrator) Status() {
|
|
|
|
// TODO - add status
|
|
|
|
}
|