snapshot of https://github.com/traggo/server.git (master, 2026-09-16; blobs > 131072B omitted)
Some checks failed
build / build (push) Failing after 3s

This commit is contained in:
upstream-mirror
2026-09-16 05:24:45 +05:00
commit cda7c1a474
272 changed files with 31973 additions and 0 deletions

57
database/database.go Normal file
View File

@@ -0,0 +1,57 @@
package database
import (
"os"
"path/filepath"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql" // enable the mysql dialect
_ "github.com/jinzhu/gorm/dialects/postgres" // enable the postgres dialect
_ "github.com/jinzhu/gorm/dialects/sqlite" // enable the sqlite3 dialect
"github.com/rs/zerolog/log"
"github.com/traggo/server/logger"
"github.com/traggo/server/model"
)
var mkdirAll = os.MkdirAll
// New creates a gorm instance.
func New(dialect, connection string) (*gorm.DB, error) {
createDirectoryIfSqlite(dialect, connection)
db, err := gorm.Open(dialect, connection)
if err != nil {
return nil, err
}
db.LogMode(true)
db.SetLogger(&logger.DatabaseLogger{})
// We normally don't need that much connections, so we limit them. F.ex. mysql complains about
// "too many connections".
db.DB().SetMaxOpenConns(10)
if dialect == "sqlite3" {
// We use the database connection inside the handlers from the http
// framework, therefore concurrent access occurs. Sqlite cannot handle
// concurrent writes, so we limit sqlite to one connection.
// see https://github.com/mattn/go-sqlite3/issues/274
db.DB().SetMaxOpenConns(1)
db.Exec("PRAGMA foreign_keys = ON")
}
log.Debug().Msg("Auto migrating schema's")
db.AutoMigrate(model.All()...)
log.Debug().Msg("Database initialized")
return db, nil
}
func createDirectoryIfSqlite(dialect string, connection string) {
if dialect == "sqlite3" {
if _, err := os.Stat(filepath.Dir(connection)); os.IsNotExist(err) {
if err := mkdirAll(filepath.Dir(connection), 0777); err != nil {
panic(err)
}
}
}
}

56
database/database_test.go Normal file
View File

@@ -0,0 +1,56 @@
package database
import (
"errors"
"os"
"testing"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"github.com/traggo/server/logger"
)
func TestMain(m *testing.M) {
logger.Init(zerolog.WarnLevel)
os.Exit(m.Run())
}
func TestInvalidDialect(t *testing.T) {
_, err := New("asdf", "testdb.db")
assert.NotNil(t, err)
}
func TestCreateSqliteFolder(t *testing.T) {
// ensure path not exists
os.RemoveAll("somepath")
db, err := New("sqlite3", "somepath/testdb.db")
assert.Nil(t, err)
assert.DirExists(t, "somepath")
db.Close()
assert.Nil(t, os.RemoveAll("somepath"))
}
func TestWithAlreadyExistingSqliteFolder(t *testing.T) {
// ensure path not exists
os.RemoveAll("somepath")
os.MkdirAll("somepath", 0777)
db, err := New("sqlite3", "somepath/testdb.db")
assert.Nil(t, err)
assert.DirExists(t, "somepath")
db.Close()
assert.Nil(t, os.RemoveAll("somepath"))
}
func TestPanicsOnMkdirError(t *testing.T) {
os.RemoveAll("somepath")
mkdirAll = func(path string, perm os.FileMode) error {
return errors.New("ERROR")
}
assert.Panics(t, func() {
New("sqlite3", "somepath/test.db")
})
}