MCPcopy
hub / github.com/tinyauthapp/tinyauth / SetupDatabase

Method SetupDatabase

internal/bootstrap/db_bootstrap.go:17–57  ·  view source on GitHub ↗
(databasePath string)

Source from the content-addressed store, hash-verified

15)
16
17func (app *BootstrapApp) SetupDatabase(databasePath string) (*sql.DB, error) {
18 dir := filepath.Dir(databasePath)
19
20 if err := os.MkdirAll(dir, 0750); err != nil {
21 return nil, fmt.Errorf("failed to create database directory %s: %w", dir, err)
22 }
23
24 db, err := sql.Open("sqlite", databasePath)
25
26 if err != nil {
27 return nil, fmt.Errorf("failed to open database: %w", err)
28 }
29
30 // Limit to 1 connection to sequence writes, this may need to be revisited in the future
31 // if the sqlite connection starts being a bottleneck
32 db.SetMaxOpenConns(1)
33
34 migrations, err := iofs.New(assets.Migrations, "migrations")
35
36 if err != nil {
37 return nil, fmt.Errorf("failed to create migrations: %w", err)
38 }
39
40 target, err := sqlite3.WithInstance(db, &sqlite3.Config{})
41
42 if err != nil {
43 return nil, fmt.Errorf("failed to create sqlite3 instance: %w", err)
44 }
45
46 migrator, err := migrate.NewWithInstance("iofs", migrations, "sqlite3", target)
47
48 if err != nil {
49 return nil, fmt.Errorf("failed to create migrator: %w", err)
50 }
51
52 if err := migrator.Up(); err != nil && err != migrate.ErrNoChange {
53 return nil, fmt.Errorf("failed to migrate database: %w", err)
54 }
55
56 return db, nil
57}

Callers 5

TestUserControllerFunction · 0.95
TestWellKnownControllerFunction · 0.95
TestOIDCControllerFunction · 0.95
TestProxyControllerFunction · 0.95
SetupMethod · 0.95

Calls

no outgoing calls

Tested by 4

TestUserControllerFunction · 0.76
TestWellKnownControllerFunction · 0.76
TestOIDCControllerFunction · 0.76
TestProxyControllerFunction · 0.76