Skip to content

Commit

Permalink
WIP.
Browse files Browse the repository at this point in the history
  • Loading branch information
efritz committed Sep 16, 2024
1 parent 1581c4c commit e14c3cf
Show file tree
Hide file tree
Showing 43 changed files with 649 additions and 33 deletions.
11 changes: 5 additions & 6 deletions describe_columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ import (
)

type ColumnDescription struct {
Name string
Index int
Type string
IsNullable bool
Default string
// TODO - are some of these redundant with type name?
Name string
Index int
Type string
IsNullable bool
Default string
CharacterMaximumLength int
IsIdentity bool
IdentityGeneration string
Expand Down
28 changes: 27 additions & 1 deletion describe_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,29 @@
package pgutil

// TODO
import (
"context"
"os"
"path"
"testing"

"github.com/hexops/autogold/v2"
"github.com/stretchr/testify/require"
)

func TestDescribeSchema(t *testing.T) {
var (
goldenDir = path.Join("testdata", "golden")
schemaFile = path.Join("testdata", "schemas", "describe.sql")
)

schemaBytes, err := os.ReadFile(schemaFile)
require.NoError(t, err)

db := NewTestDB(t)
err = db.Exec(context.Background(), RawQuery(string(schemaBytes)))
require.NoError(t, err)

schema, err := DescribeSchema(context.Background(), db)
require.NoError(t, err)
autogold.ExpectFile(t, schema, autogold.Dir(goldenDir))
}
1 change: 0 additions & 1 deletion description.json

This file was deleted.

18 changes: 8 additions & 10 deletions drift_enums_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package pgutil
import (
"testing"

// TODO - use testify
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestUnifyLabels(t *testing.T) {
Expand Down Expand Up @@ -60,14 +60,12 @@ func TestUnifyLabels(t *testing.T) {
},
} {
t.Run(testCase.name, func(t *testing.T) {
if reconstruction, valid := unifyLabels(testCase.expectedLabels, testCase.existingLabels); valid {
if !testCase.valid {
t.Fatalf("expected unification to fail")
} else if diff := cmp.Diff(testCase.reconstruction, reconstruction); diff != "" {
t.Errorf("unexpected reconstruction (-want +got):\n%s", diff)
}
} else if testCase.valid {
t.Fatalf("expected unification to succeed")
reconstruction, valid := unifyLabels(testCase.expectedLabels, testCase.existingLabels)
if testCase.valid {
require.True(t, valid)
assert.Equal(t, testCase.reconstruction, reconstruction)
} else {
require.False(t, valid)
}
})
}
Expand Down
15 changes: 12 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/go-nacelle/config/v3 v3.0.0
github.com/go-nacelle/log/v2 v2.0.1
github.com/go-nacelle/nacelle/v2 v2.1.1
github.com/google/go-cmp v0.6.0
github.com/hexops/autogold/v2 v2.2.1
github.com/jackc/pgconn v1.14.1
github.com/lib/pq v1.10.9
github.com/segmentio/fasthash v1.0.3
Expand All @@ -20,10 +20,15 @@ require (
github.com/BurntSushi/toml v1.4.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/derision-test/glock v1.1.0 // indirect
github.com/fatih/color v1.15.0 // indirect
github.com/fatih/structtag v1.2.0 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/go-nacelle/process/v2 v2.1.0 // indirect
github.com/go-nacelle/service/v2 v2.0.1 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/hexops/autogold v1.3.1 // indirect
github.com/hexops/gotextdiff v1.0.3 // indirect
github.com/hexops/valast v1.4.4 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgio v1.0.0 // indirect
Expand All @@ -34,11 +39,15 @@ require (
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-zglob v0.0.6 // indirect
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
github.com/nightlyone/lockfile v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/crypto v0.6.0 // indirect
golang.org/x/crypto v0.12.0 // indirect
golang.org/x/mod v0.13.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.7.0 // indirect
golang.org/x/text v0.12.0 // indirect
golang.org/x/tools v0.14.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
mvdan.cc/gofumpt v0.5.0 // indirect
)
83 changes: 77 additions & 6 deletions go.sum

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions migration_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ CREATE TABLE comments (
post_id INTEGER NOT NULL REFERENCES posts(id) ON DELETE CASCADE,
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
content TEXT NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
`

Expand All @@ -30,7 +30,7 @@ DROP INDEX CONCURRENTLY IF EXISTS idx_users_email;`

func TestReadMigrations(t *testing.T) {
t.Run("valid", func(t *testing.T) {
definitions, err := ReadMigrations(NewFilesystemMigrationReader(path.Join("testdata", "valid")))
definitions, err := ReadMigrations(NewFilesystemMigrationReader(path.Join("testdata", "migrations", "valid")))
require.NoError(t, err)
require.Len(t, definitions, 3)

Expand All @@ -44,7 +44,7 @@ func TestReadMigrations(t *testing.T) {

t.Run("CIC pattern", func(t *testing.T) {
t.Skip()
definitions, err := ReadMigrations(NewFilesystemMigrationReader(path.Join("testdata", "cic_pattern")))
definitions, err := ReadMigrations(NewFilesystemMigrationReader(path.Join("testdata", "migrations", "cic_pattern")))
require.NoError(t, err)
require.Len(t, definitions, 4)

Expand All @@ -61,17 +61,17 @@ func TestReadMigrations(t *testing.T) {
})

t.Run("duplicate identifiers", func(t *testing.T) {
_, err := ReadMigrations(NewFilesystemMigrationReader(path.Join("testdata", "duplicate_identifiers")))
_, err := ReadMigrations(NewFilesystemMigrationReader(path.Join("testdata", "migrations", "duplicate_identifiers")))
assert.ErrorContains(t, err, "duplicate migration identifier 2")
})

t.Run("CIC with additional queries", func(t *testing.T) {
_, err := ReadMigrations(NewFilesystemMigrationReader(path.Join("testdata", "cic_with_additional_queries")))
_, err := ReadMigrations(NewFilesystemMigrationReader(path.Join("testdata", "migrations", "cic_with_additional_queries")))
assert.ErrorContains(t, err, `"create index concurrently" is not the only statement in the up migration`)
})

t.Run("CIC in down migration", func(t *testing.T) {
_, err := ReadMigrations(NewFilesystemMigrationReader(path.Join("testdata", "cic_in_down_migration")))
_, err := ReadMigrations(NewFilesystemMigrationReader(path.Join("testdata", "migrations", "cic_in_down_migration")))
assert.ErrorContains(t, err, `"create index concurrently" is not the only statement in the up migration`)
})
}
Loading

0 comments on commit e14c3cf

Please sign in to comment.