(Feat): Initial Commit
Some checks failed
Build & Test / build-test (push) Has been cancelled
Build & Test / swagger-codegen-cli (push) Has been cancelled
CodeQL / Analyze (go) (push) Has been cancelled

This commit is contained in:
2026-07-03 19:41:31 +05:30
commit 7e940c83a7
461 changed files with 45002 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
package persistence
import (
"context"
"database/sql"
"fmt"
"time"
"allaboutapps.dev/aw/go-starter/internal/config"
)
const (
dbPingTimeout = 10 * time.Second
)
func NewDB(cfg config.Database) (*sql.DB, error) {
db, err := sql.Open("postgres", cfg.ConnectionString())
if err != nil {
return nil, fmt.Errorf("failed to open db connection: %w", err)
}
if cfg.MaxOpenConns > 0 {
db.SetMaxOpenConns(cfg.MaxOpenConns)
}
if cfg.MaxIdleConns > 0 {
db.SetMaxIdleConns(cfg.MaxIdleConns)
}
if cfg.ConnMaxLifetime > 0 {
db.SetConnMaxLifetime(cfg.ConnMaxLifetime)
}
ctx, cancel := context.WithTimeout(context.Background(), dbPingTimeout)
defer cancel()
if err := db.PingContext(ctx); err != nil {
return nil, fmt.Errorf("failed to ping DB: %w", err)
}
return db, nil
}

View File

@@ -0,0 +1,63 @@
package persistence_test
import (
"database/sql"
"testing"
"allaboutapps.dev/aw/go-starter/internal/config"
"allaboutapps.dev/aw/go-starter/internal/test"
migrate "github.com/rubenv/sql-migrate"
"github.com/stretchr/testify/require"
)
func TestMigrations(t *testing.T) {
test.WithTestDatabaseEmpty(t, func(db *sql.DB) {
migrate.SetTable(config.DatabaseMigrationTable)
// use the migrations from the migrations folder
migrationSource := &migrate.FileMigrationSource{Dir: config.DatabaseMigrationFolder}
// run all up migrations
missingUpMigrations, _, err := migrate.PlanMigration(db, "postgres", migrationSource, migrate.Up, 0)
require.NoError(t, err)
require.NotEmpty(t, missingUpMigrations)
for _, migration := range missingUpMigrations {
n, err := migrate.ExecMax(db, "postgres", migrationSource, migrate.Up, 1)
require.NoError(t, err, "failed to apply up migration %s", migration.Id)
require.Equal(t, 1, n, "expected 1 migration to be applied for %s, got %d", migration.Id, n)
}
// expect all migrations to be applied
missingUpMigrationsAfterApply, _, err := migrate.PlanMigration(db, "postgres", migrationSource, migrate.Up, 0)
require.NoError(t, err)
require.Empty(t, missingUpMigrationsAfterApply)
// run all down migrations
downMigrations, _, err := migrate.PlanMigration(db, "postgres", migrationSource, migrate.Down, 0)
require.NoError(t, err)
require.NotEmpty(t, downMigrations)
for _, migration := range downMigrations {
n, err := migrate.ExecMax(db, "postgres", migrationSource, migrate.Down, 1)
require.NoError(t, err, "failed to apply down migration %s", migration.Id)
require.Equal(t, 1, n, "expected 1 migration to be applied for %s, got %d", migration.Id, n)
}
// expect all down migrations to be applied
missingDownMigrationsAfterDown, _, err := migrate.PlanMigration(db, "postgres", migrationSource, migrate.Down, 0)
require.NoError(t, err)
require.Empty(t, missingDownMigrationsAfterDown)
// run all up migrations again to test if the down migrations cleanup the database correctly
upMigrations, _, err := migrate.PlanMigration(db, "postgres", migrationSource, migrate.Up, 0)
require.NoError(t, err)
require.NotEmpty(t, upMigrations)
for _, migration := range upMigrations {
n, err := migrate.ExecMax(db, "postgres", migrationSource, migrate.Up, 1)
require.NoError(t, err, "failed to apply up migration %s after down migrations", migration.Id)
require.Equal(t, 1, n, "expected 1 migration to be applied for %s after down migrations, got %d", migration.Id, n)
}
})
}