package migrate

import (
	"github.com/pingcap/log"
	"github.com/tidbcloud/dataflow-service-ng/app/repo/entity"
	"go.uber.org/zap"
	"gorm.io/gorm"
)

func initDataflowDB(db *gorm.DB) error {
	log.L().Info("init empty database with data")
	// create the tables in an empty database
	// it is 3 time faster than migrate model individually
	// NOTE: not all models defined in `model.go` are owned by dataflow-service, so we should NOT do the migration for them.
	var allModels []interface{}
	allModels = append(allModels, entity.ORMModels...)
	if err := db.AutoMigrate(allModels...); err != nil {
		return err
	}
	if err := entity.InitDataBase(db); err != nil {
		log.L().Error("init data failed", zap.Error(err))
		return err
	}
	log.L().Info("after create database tables")
	return nil
}
