Personal Git forge.

Code git clone ssh://git.poy.ooo:6969/~poyo/farga ZIP
db/db.go / 1KiB #3917d7af
Raw
package dbimport (	"farga/db/types"	"farga/lib"	"os"	"time"	"gorm.io/driver/sqlite"	"gorm.io/gorm"	"gorm.io/gorm/logger")type DB struct {	*gorm.DB}func New() (*DB, error) {	path := os.Getenv("SQLITE_PATH")	dsn := path + "?_foreign_keys=on&_busy_timeout=5000&_journal_mode=WAL&_synchronous=NORMAL"	db, err := gorm.Open(sqlite.Open(dsn), &gorm.Config{		Logger: logger.Default.LogMode(logger.Error), // only errors to console	})	if err != nil {		return nil, err	}	// foreign_keys = ON    -  disallow ignoring foreign keys.	// journal_mode = WAL   -  enables write-ahead log.	// synchronous = NORMAL -  https://mort.coffee/home/sqlite-editions/	// busy_timeout = 5000  -  timeout for writers waiting for sqlite to unlock.	migrate(db)	return &DB{db}, nil}func migrate(db *gorm.DB) {	db.AutoMigrate(&types.User{})	db.AutoMigrate(&types.UserConfig{})	db.AutoMigrate(&types.Invite{})	// sessions from before token hashing can't be migrated (the NOT NULL hash	// column can't be added to existing rows), so drop them and log everyone out.	if db.Migrator().HasTable(&types.Session{}) && !db.Migrator().HasColumn(&types.Session{}, "Hash") {		db.Migrator().DropTable(&types.Session{})	}	db.AutoMigrate(&types.Session{})	db.AutoMigrate(&types.Repo{})	db.AutoMigrate(&types.RepoConfig{})	db.AutoMigrate(&types.Issue{})	db.AutoMigrate(&types.IssueTag{})	db.AutoMigrate(&types.IssueComment{})	db.AutoMigrate(&types.SSHPubKey{})	db.AutoMigrate(&types.OAuthState{})	db.AutoMigrate(&types.Gist{})}func (db *DB) CleanupTicker() {	ticker := time.NewTicker(lib.DatabaseCleanupMinutes * time.Minute)	defer ticker.Stop()	for range ticker.C {		db.DeleteExpiredSessions()		db.DeleteExpiredOAuthStates()		db.DeleteExpiredInviteTokens()	}}