package migrations_test import ( "database/sql" "path/filepath" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" _ "modernc.org/sqlite" "sneak.berlin/go/webhooker/internal/db/migrations" ) // TestApplyRunsEachFileOnce applies each kind of database file's // migrations to a new file twice. The first time records every file. // The second finds them all recorded and runs none, which is the only // way it can succeed: 001_schema.sql creates tables that then exist. func TestApplyRunsEachFileOnce(t *testing.T) { t.Parallel() for _, dir := range []string{ migrations.Main, migrations.Events, migrations.Archive, } { t.Run(dir, func(t *testing.T) { t.Parallel() db, err := sql.Open( "sqlite", filepath.Join(t.TempDir(), "test.db"), ) require.NoError(t, err) t.Cleanup(func() { _ = db.Close() }) require.NoError(t, migrations.Apply(db, dir)) require.NoError(t, migrations.Apply(db, dir)) var versions string require.NoError(t, db.QueryRowContext(t.Context(), "SELECT group_concat(version) FROM schema_migrations", ).Scan(&versions)) assert.Equal(t, "0,1", versions) }) } }