diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index a86bbc9..826adfc 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -7,7 +7,7 @@ jobs: runs-on: ${{ matrix.os }} strategy: matrix: - go-version: [1.13.x, 1.14.x] + go-version: [1.15.x, 1.16.x] os: [macos-latest, windows-latest, ubuntu-latest] steps: - name: Checkout Code diff --git a/model.go b/model.go index 291b58e..5bc8172 100644 --- a/model.go +++ b/model.go @@ -25,7 +25,7 @@ type Model struct { func (m *Model) SetupTest() { m.Assertions = require.New(m.T()) if m.DB != nil { - err := m.DB.TruncateAll() + err := m.CleanDB() m.NoError(err) } } @@ -96,3 +96,38 @@ func (m *Model) Run(name string, subtest func()) bool { subtest() }) } + +// CleanDB clears records from the database, this function is +// useful to run before tests to ensure other tests are not +// affecting the one running. +func (m *Model) CleanDB() error { + if m.DB == nil { + return nil + } + + switch m.DB.Dialect.Name() { + case "postgres": + deleteAllQuery := `DO + $func$ + DECLARE + _tbl text; + _sch text; + BEGIN + FOR _sch, _tbl IN + SELECT schemaname, tablename + FROM pg_tables + WHERE tablename <> '%s' AND schemaname NOT IN ('pg_catalog', 'information_schema') AND tableowner = current_user + LOOP + --RAISE ERROR '%%', + EXECUTE -- dangerous, test before you execute! + format('DELETE FROM %%I.%%I CASCADE', _sch, _tbl); + END LOOP; + END + $func$;` + + q := m.DB.RawQuery(fmt.Sprintf(deleteAllQuery, m.DB.MigrationTableName())) + return q.Exec() + default: + return m.DB.TruncateAll() + } +}