MCPcopy
hub / github.com/gotify/server / New

Function New

database/database.go:27–98  ·  view source on GitHub ↗

New creates a new wrapper for the gorm database framework.

(dialect, connection, defaultUser, defaultPass string, strength int, createDefaultUserIfNotExist bool)

Source from the content-addressed store, hash-verified

25
26// New creates a new wrapper for the gorm database framework.
27func New(dialect, connection, defaultUser, defaultPass string, strength int, createDefaultUserIfNotExist bool) (*GormDatabase, error) {
28 createDirectoryIfSqlite(dialect, connection)
29
30 dbLogger := logger.New(log.New(os.Stderr, "\r\n", log.LstdFlags), logger.Config{
31 SlowThreshold: 200 * time.Millisecond,
32 LogLevel: logger.Warn,
33 IgnoreRecordNotFoundError: true,
34 Colorful: isatty.IsTerminal(os.Stderr.Fd()),
35 })
36 gormConfig := &gorm.Config{
37 Logger: dbLogger,
38 DisableForeignKeyConstraintWhenMigrating: true,
39 TranslateError: true,
40 }
41
42 var db *gorm.DB
43 err := errors.New("unsupported dialect: " + dialect)
44
45 switch dialect {
46 case "mysql":
47 db, err = gorm.Open(mysql.Open(connection), gormConfig)
48 case "postgres":
49 db, err = gorm.Open(postgres.Open(connection), gormConfig)
50 case "sqlite3":
51 db, err = gorm.Open(sqlite.Open(connection), gormConfig)
52 }
53
54 if err != nil {
55 return nil, err
56 }
57
58 sqldb, err := db.DB()
59 if err != nil {
60 return nil, err
61 }
62
63 // We normally don't need that much connections, so we limit them. F.ex. mysql complains about
64 // "too many connections", while load testing Gotify.
65 sqldb.SetMaxOpenConns(10)
66
67 if dialect == "sqlite3" {
68 // We use the database connection inside the handlers from the http
69 // framework, therefore concurrent access occurs. Sqlite cannot handle
70 // concurrent writes, so we limit sqlite to one connection.
71 // see https://github.com/mattn/go-sqlite3/issues/274
72 sqldb.SetMaxOpenConns(1)
73 }
74
75 if dialect == "mysql" {
76 // Mysql has a setting called wait_timeout, which defines the duration
77 // after which a connection may not be used anymore.
78 // The default for this setting on mariadb is 10 minutes.
79 // See https://github.com/docker-library/mariadb/issues/113
80 sqldb.SetConnMaxLifetime(9 * time.Minute)
81 }
82
83 if err := db.AutoMigrate(new(model.User), new(model.Application), new(model.Message), new(model.Client), new(model.PluginConf)); err != nil {
84 return nil, err

Callers 10

mainFunction · 0.92
NewDBWithDefaultUserFunction · 0.92
NewDBFunction · 0.92
TestMigrationMethod · 0.70
BeforeTestMethod · 0.70
TestInvalidDialectFunction · 0.70
TestCreateSqliteFolderFunction · 0.70
TestPanicsOnMkdirErrorFunction · 0.70
TestMigrateSortKeyFunction · 0.70

Calls 3

CreatePasswordFunction · 0.92
createDirectoryIfSqliteFunction · 0.85
OpenMethod · 0.80

Tested by 7

TestMigrationMethod · 0.56
BeforeTestMethod · 0.56
TestInvalidDialectFunction · 0.56
TestCreateSqliteFolderFunction · 0.56
TestPanicsOnMkdirErrorFunction · 0.56
TestMigrateSortKeyFunction · 0.56