2024-11-10 09:22:33 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"io/fs"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/golang-migrate/migrate/v4"
|
|
|
|
"github.com/golang-migrate/migrate/v4/database/postgres"
|
|
|
|
"github.com/golang-migrate/migrate/v4/source/httpfs"
|
|
|
|
"gitserver.in/patialtech/rano/db"
|
|
|
|
entMigrate "gitserver.in/patialtech/rano/db/ent/migrate"
|
|
|
|
"gitserver.in/patialtech/rano/db/migrations"
|
2024-11-15 16:12:15 +00:00
|
|
|
"gitserver.in/patialtech/rano/util/logger"
|
2024-11-10 09:22:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
client := db.Client()
|
|
|
|
defer client.Close()
|
|
|
|
|
|
|
|
err := client.Schema.Create(
|
|
|
|
context.Background(),
|
|
|
|
entMigrate.WithDropIndex(true),
|
|
|
|
entMigrate.WithDropColumn(true),
|
|
|
|
entMigrate.WithForeignKeys(true),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
logger.Fatal("😡 migrate-up failed with error: %v", err)
|
|
|
|
} else {
|
|
|
|
logger.Info("✅ migrate-up")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = sqlUp(); err != nil {
|
|
|
|
logger.Fatal("😡 .sql migrate-up failed with error: %v", err)
|
|
|
|
} else {
|
|
|
|
logger.Info("✅ migrate-up .sql migrations")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func sqlUp() error {
|
|
|
|
m, err := mig()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = m.Up(); err != nil && !errors.Is(err, migrate.ErrNoChange) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func mig() (*migrate.Migrate, error) {
|
|
|
|
// migrations files
|
|
|
|
migrations, err := fs.Sub(migrations.FS, ".")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to read embed files: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// create a source driver from the migrations file system
|
|
|
|
src, err := httpfs.New(http.FS(migrations), ".")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to create source driver: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
driver, err := postgres.WithInstance(db.New(), &postgres.Config{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return migrate.NewWithInstance("httpfs", src, "postgres", driver)
|
|
|
|
}
|