a18f96912d
interfaces - refactor: split UserRepository into AuthUserRepository and AdminUserRepository capabilities - refactor: split SessionRepository into AuthSessionRepository and repository-owned CLI/admin methods - refactor: replace generic Repository Update/Delete with operation-specific params and ownership predicates - refactor: replace CredentialRepository generic CRUD with passkey-specific methods - refactor: replace FileRepository generic Create/Update/Delete with UploadedFileParams, DirectoryParams, and owned soft-delete - refactor: remove repository fields from WebApp struct; repositories are now composition-time wiring only - feat: add domain error kinds ErrParentNotFound, ErrParentNotDir, ErrDirectoryNotEmpty, ErrInvalidMove - feat: add CredentialTypeAppPasskey constant - feat: add testutil.SetUserAdmin for test fixture setup that bypasses production service ports - test: add architecture test banning GORM Save in repository package - test: add capability interface contract tests ensuring each service receives the minimal interface - test: add blockingStorage helper for concurrent promotion tests - test: add preserved DSN parameter test for sqliteImmediateDSN - docs: update architecture decisions with repository write rules and capability separation - docs: update roadmap to clarify atomic single-use refresh sessions - docs: add -race test target to development docs
75 lines
1.7 KiB
Go
75 lines
1.7 KiB
Go
package repository
|
|
|
|
import (
|
|
"fmt"
|
|
"net/url"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"gorm.io/driver/postgres"
|
|
"gorm.io/driver/sqlite"
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/dhao2001/mygo/internal/config"
|
|
"github.com/dhao2001/mygo/internal/model"
|
|
)
|
|
|
|
// Open creates a GORM database connection based on the config driver.
|
|
func Open(cfg config.DatabaseConfig) (*gorm.DB, error) {
|
|
var dialector gorm.Dialector
|
|
|
|
switch cfg.Driver {
|
|
case "sqlite3":
|
|
dir := filepath.Dir(cfg.SQLite.Path)
|
|
if err := os.MkdirAll(dir, 0755); err != nil {
|
|
return nil, fmt.Errorf("create db directory: %w", err)
|
|
}
|
|
dialector = sqlite.Open(sqliteImmediateDSN(cfg.SQLite.Path))
|
|
case "postgres":
|
|
dsn := fmt.Sprintf(
|
|
"host=%s user=%s password=%s dbname=%s port=%d sslmode=%s",
|
|
cfg.Postgres.Host,
|
|
cfg.Postgres.User,
|
|
cfg.Postgres.Password,
|
|
cfg.Postgres.DBName,
|
|
cfg.Postgres.Port,
|
|
cfg.Postgres.SSLMode,
|
|
)
|
|
dialector = postgres.Open(dsn)
|
|
default:
|
|
return nil, fmt.Errorf("unsupported database driver: %s", cfg.Driver)
|
|
}
|
|
|
|
db, err := gorm.Open(dialector, &gorm.Config{})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("open database: %w", err)
|
|
}
|
|
|
|
return db, nil
|
|
}
|
|
|
|
func sqliteImmediateDSN(path string) string {
|
|
base, rawQuery, found := strings.Cut(path, "?")
|
|
query, err := url.ParseQuery(rawQuery)
|
|
if err != nil {
|
|
separator := "?"
|
|
if found {
|
|
separator = "&"
|
|
}
|
|
return path + separator + "_txlock=immediate"
|
|
}
|
|
query.Set("_txlock", "immediate")
|
|
return base + "?" + query.Encode()
|
|
}
|
|
|
|
// AutoMigrate runs schema migration for all domain models.
|
|
func AutoMigrate(db *gorm.DB) error {
|
|
return db.AutoMigrate(
|
|
&model.User{},
|
|
&model.Session{},
|
|
&model.File{},
|
|
&model.Credential{},
|
|
)
|
|
}
|