summaryrefslogtreecommitdiffstats
path: root/pkg/database/database.go
blob: b236d2d75bc7d6d016de40726cc8530970f1fd7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package database

import (
	"database/sql"
	"embed"
	"fmt"

	_ "github.com/mattn/go-sqlite3"
	"github.com/pressly/goose/v3"
)

//go:embed migrations/*.sql
var embedMigrations embed.FS

func Connect(path string) (*sql.DB, error) {
	return sql.Open("sqlite3", path+"?_foreign_keys=on")
}

func Migrate(db *sql.DB) error {
	goose.SetBaseFS(embedMigrations)

	if err := goose.SetDialect("sqlite3"); err != nil {
		return fmt.Errorf("could not set dialect: %w", err)
	}

	if err := goose.Up(db, "migrations"); err != nil {
		return fmt.Errorf("could not apply migrations: %w", err)
	}

	return nil
}