34 lines
814 B
Go
34 lines
814 B
Go
package database
|
|
|
|
import (
|
|
domainUpdate "RenewCMS/internal/domain/update"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
"github.com/glebarez/sqlite"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
const DriverSQLite = "sqlite"
|
|
|
|
const sqliteBusyTimeoutMs = 5000
|
|
|
|
type sqliteDriver struct{}
|
|
|
|
func SQLiteDSN(dbName string) string {
|
|
return fmt.Sprintf("%s?_pragma=busy_timeout(%d)", dbName, sqliteBusyTimeoutMs)
|
|
}
|
|
|
|
func (sqliteDriver) Open(file string) (*gorm.DB, error) {
|
|
if err := os.MkdirAll(filepath.Dir(file), os.ModePerm); err != nil {
|
|
return nil, fmt.Errorf("unable to create necessary subdirectories: %w", err)
|
|
}
|
|
return gorm.Open(sqlite.Open(SQLiteDSN(file)), &gorm.Config{})
|
|
}
|
|
|
|
func (sqliteDriver) NewBackup(db *gorm.DB, directory string, now func() time.Time) domainUpdate.DatabaseBackup {
|
|
return NewSQLiteBackup(db, directory, now)
|
|
}
|