(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,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)
}
})
}