2023-04-25 18:43:01 +02:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
2024-05-04 17:06:01 +02:00
|
|
|
"os"
|
|
|
|
|
2024-05-09 15:28:04 +02:00
|
|
|
"log"
|
|
|
|
|
2024-05-04 17:06:01 +02:00
|
|
|
"code.lila.network/adoralaura/go-urlsh/models"
|
2023-04-25 18:43:01 +02:00
|
|
|
"github.com/uptrace/bun"
|
|
|
|
"github.com/uptrace/bun/dialect/pgdialect"
|
|
|
|
"github.com/uptrace/bun/driver/pgdriver"
|
|
|
|
)
|
|
|
|
|
2024-05-09 15:28:04 +02:00
|
|
|
func InitializeDB() *bun.DB {
|
2023-04-25 18:43:01 +02:00
|
|
|
sqldb := sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(os.Getenv("DATABASE_URL"))))
|
2024-05-09 15:28:04 +02:00
|
|
|
db := bun.NewDB(sqldb, pgdialect.New())
|
2023-04-25 18:43:01 +02:00
|
|
|
|
2024-05-09 15:28:04 +02:00
|
|
|
_, err := db.NewCreateTable().IfNotExists().Model((*models.Link)(nil)).Exec(context.Background())
|
2023-04-25 18:43:01 +02:00
|
|
|
if err != nil {
|
2024-05-09 15:28:04 +02:00
|
|
|
log.Panicf("[DB] couldn't create database: [%w]", err)
|
2023-04-25 18:43:01 +02:00
|
|
|
}
|
2023-04-27 10:37:42 +02:00
|
|
|
|
2024-05-09 15:28:04 +02:00
|
|
|
_, err = db.NewCreateTable().IfNotExists().Model((*models.User)(nil)).Exec(context.Background())
|
2023-04-27 10:37:42 +02:00
|
|
|
if err != nil {
|
2024-05-09 15:28:04 +02:00
|
|
|
log.Panicf("[DB] couldn't create database: [%w]", err)
|
2023-04-27 10:37:42 +02:00
|
|
|
}
|
|
|
|
|
2024-05-09 15:28:04 +02:00
|
|
|
_, err = db.NewCreateTable().IfNotExists().Model((*models.Session)(nil)).Exec(context.Background())
|
2023-04-27 10:37:42 +02:00
|
|
|
if err != nil {
|
2024-05-09 15:28:04 +02:00
|
|
|
log.Panicf("[DB] couldn't create database: [%w]", err)
|
2023-04-27 10:37:42 +02:00
|
|
|
}
|
|
|
|
|
2024-05-09 15:28:04 +02:00
|
|
|
_, err = db.NewCreateTable().IfNotExists().Model((*models.ApiKey)(nil)).Exec(context.Background())
|
2023-04-27 10:37:42 +02:00
|
|
|
if err != nil {
|
2024-05-09 15:28:04 +02:00
|
|
|
log.Panicf("[DB] couldn't create database: [%w]", err)
|
2024-05-04 17:06:01 +02:00
|
|
|
}
|
|
|
|
|
2024-05-09 15:28:04 +02:00
|
|
|
_, err = db.NewCreateTable().IfNotExists().Model((*models.MFALoginTransaction)(nil)).Exec(context.Background())
|
2024-05-04 17:06:01 +02:00
|
|
|
if err != nil {
|
2024-05-09 15:28:04 +02:00
|
|
|
log.Panicf("[DB] couldn't create database: [%w]", err)
|
2024-05-04 17:06:01 +02:00
|
|
|
}
|
|
|
|
|
2024-05-09 15:28:04 +02:00
|
|
|
_, err = db.NewCreateTable().IfNotExists().Model((*models.MFAConfig)(nil)).Exec(context.Background())
|
2024-05-04 17:06:01 +02:00
|
|
|
if err != nil {
|
2024-05-09 15:28:04 +02:00
|
|
|
log.Panicf("[DB] couldn't create database: [%w]", err)
|
2024-05-04 17:06:01 +02:00
|
|
|
}
|
|
|
|
|
2024-05-09 15:28:04 +02:00
|
|
|
_, err = db.NewCreateTable().IfNotExists().Model((*models.MFAScratchCode)(nil)).Exec(context.Background())
|
2024-05-06 15:32:53 +02:00
|
|
|
if err != nil {
|
2024-05-09 15:28:04 +02:00
|
|
|
log.Panicf("[DB] couldn't create database: [%w]", err)
|
2024-05-06 15:32:53 +02:00
|
|
|
}
|
|
|
|
|
2024-05-09 15:28:04 +02:00
|
|
|
err = doMigrations(db)
|
2024-05-04 17:06:01 +02:00
|
|
|
if err != nil {
|
2024-05-09 15:28:04 +02:00
|
|
|
log.Panicf("[DB] Error during Migrations: [%w]", err)
|
2023-04-27 10:37:42 +02:00
|
|
|
}
|
2024-05-09 15:28:04 +02:00
|
|
|
|
|
|
|
return db
|
2023-04-25 18:43:01 +02:00
|
|
|
}
|