Sets up and initializes database for freed (SQLite)
This commit is contained in:
parent
6f79f255d9
commit
23e29c44f4
5 changed files with 221 additions and 1 deletions
36
internal/database/database.go
Normal file
36
internal/database/database.go
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
package database
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"fmt"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
migrate "github.com/rubenv/sql-migrate"
|
||||
)
|
||||
|
||||
//go:embed migrations/*
|
||||
var dbMigrations embed.FS
|
||||
|
||||
func New(filename string) (*sqlx.DB, error) {
|
||||
db, err := sqlx.Open("sqlite3", filename)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
migrations := migrate.EmbedFileSystemMigrationSource{
|
||||
FileSystem: dbMigrations,
|
||||
Root: "migrations",
|
||||
}
|
||||
|
||||
n, err := migrate.Exec(db.DB, "sqlite3", migrations, migrate.Up)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fmt.Printf("Applied %d migrations - Database is ready!\n", n)
|
||||
|
||||
return db, err
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue