/*
migrate-new is a helper script for auto-migrating Linstrom's
database layout into a database defined in the given config
*/
package main

import (
	"flag"

	"git.mstar.dev/mstar/goutils/other"
	"github.com/rs/zerolog/log"
	"gorm.io/driver/postgres"
	"gorm.io/gorm"

	"git.mstar.dev/mstar/linstrom/config"
	"git.mstar.dev/mstar/linstrom/shared"
	storage "git.mstar.dev/mstar/linstrom/storage-new"
)

func main() {
	other.SetupFlags()
	flag.Parse()
	other.ConfigureLogging(nil)
	if err := config.ReadAndWriteToGlobal(*shared.FlagConfigFile); err != nil {
		log.Fatal().Err(err).Send()
	}

	db, err := gorm.Open(
		postgres.Open(config.GlobalConfig.Storage.BuildPostgresDSN()),
		&gorm.Config{
			PrepareStmt: false,
			Logger:      shared.NewGormLogger(log.Logger),
		},
	)
	if err != nil {
		log.Fatal().Err(err).Msg("Failed to open connection to temporary container")
	}
	if err := storage.Migrate(db); err != nil {
		log.Fatal().Err(err).Send()
	}
}
