Migrate shared UI and backend components
This commit is contained in:
1 parent
64d22d0aac
commit
ac08be0248
16 files changed
+236
-395
No files matched your search
+6
-22
@@ -7,8 +7,9 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
sharedpostgres "repos.gfranz.ch/Gabriel20xx/SharedCode/backend/postgres"
|
||||
)
|
||||
|
||||
// DB owns the PostgreSQL pool. The schema deliberately uses the same physical
|
||||
@@ -19,13 +20,9 @@ type DB struct {
|
||||
}
|
||||
|
||||
func connectDB(ctx context.Context, connectionString string) (*DB, error) {
|
||||
pool, err := pgxpool.New(ctx, connectionString)
|
||||
pool, err := sharedpostgres.Open(ctx, sharedpostgres.DefaultConfig(connectionString))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create pool: %w", err)
|
||||
}
|
||||
if err := pool.Ping(ctx); err != nil {
|
||||
pool.Close()
|
||||
return nil, fmt.Errorf("ping database: %w", err)
|
||||
return nil, err
|
||||
}
|
||||
return &DB{pool: pool}, nil
|
||||
}
|
||||
@@ -35,19 +32,7 @@ func (d *DB) close() {
|
||||
}
|
||||
|
||||
func (d *DB) withTx(ctx context.Context, fn func(pgx.Tx) error) error {
|
||||
tx, err := d.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("begin transaction: %w", err)
|
||||
}
|
||||
defer tx.Rollback(ctx) // harmless after a successful commit
|
||||
|
||||
if err := fn(tx); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return fmt.Errorf("commit transaction: %w", err)
|
||||
}
|
||||
return nil
|
||||
return sharedpostgres.WithTx(ctx, d.pool, fn)
|
||||
}
|
||||
|
||||
// initSchema is idempotent so an existing CollabTable PostgreSQL database can
|
||||
@@ -211,8 +196,7 @@ func (d *DB) createUser(ctx context.Context, username, email, passwordHash strin
|
||||
RETURNING id, username, email
|
||||
`, username, email, passwordHash).Scan(&user.ID, &user.Username, &user.Email)
|
||||
if err != nil {
|
||||
var pgErr *pgconn.PgError
|
||||
if errors.As(err, &pgErr) && pgErr.Code == "23505" {
|
||||
if sharedpostgres.IsUniqueViolation(err) {
|
||||
return nil, ErrSingletonUserExists
|
||||
}
|
||||
return nil, fmt.Errorf("create user: %w", err)
|
||||
|
||||
Reference in new issue
Block a user