Refactor Store into SessionStore; move domain SQL onto User/Question/Answer.

This commit is contained in:
2026-08-22 02:40:51 -07:00
parent f31f352838
commit c77298411e
15 changed files with 696 additions and 876 deletions
+9 -10
View File
@@ -48,34 +48,33 @@ func postgresDSN(raw string) (string, error) {
}
// OpenPostgres connects to Postgres, applies schema/migrations, and starts session cleanup.
func OpenPostgres(databaseURL, schema string) (*Store, error) {
func OpenPostgres(databaseURL, schema string) (*sql.DB, *SessionStore, error) {
dsn, err := postgresDSN(databaseURL)
if err != nil {
return nil, err
return nil, nil, err
}
db, err := sql.Open("pgx", dsn)
if err != nil {
return nil, err
return nil, nil, err
}
db.SetMaxOpenConns(20)
db.SetMaxIdleConns(5)
if err := db.Ping(); err != nil {
_ = db.Close()
return nil, fmt.Errorf("postgres ping: %w", err)
return nil, nil, fmt.Errorf("postgres ping: %w", err)
}
if err := applySchema(db, schema); err != nil {
_ = db.Close()
return nil, fmt.Errorf("apply schema: %w", err)
return nil, nil, fmt.Errorf("apply schema: %w", err)
}
if err := applySessionsSchema(db); err != nil {
_ = db.Close()
return nil, fmt.Errorf("apply sessions schema: %w", err)
return nil, nil, fmt.Errorf("apply sessions schema: %w", err)
}
if err := migrateUserProfileColumns(db); err != nil {
_ = db.Close()
return nil, fmt.Errorf("migrate profile columns: %w", err)
return nil, nil, fmt.Errorf("migrate profile columns: %w", err)
}
st := &Store{db: db}
st.initSessionStore(5 * time.Minute)
return st, nil
sessions := NewSessionStore(db, 5*time.Minute)
return db, sessions, nil
}