package database

import (
	"log"

	"git.tovijaeschke.xyz/tovi/personal_website/variables"
	"gorm.io/gorm"
)

func updateDefaultSidebarLinks() {
	var e error
	for name, link := range variables.DefaultSidebarLinks {
		e = AddSidebarLink(name, link)
		if e != nil {
			log.Fatal(e)
		}
	}
}

func MigrateDB() error {
	var (
		migrator gorm.Migrator
	)
	migrator = DB.Migrator()

	if !migrator.HasTable(Post{}) {
		migrator.CreateTable(&Post{})
	}

	if !migrator.HasTable(SidebarLink{}) {
		migrator.CreateTable(&SidebarLink{})
		updateDefaultSidebarLinks()
	}

	return nil
}
