-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
227 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,161 @@ | ||
package pgutil | ||
|
||
// TODO | ||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestDrift_Extensions(t *testing.T) { | ||
t.Run("create", func(t *testing.T) { | ||
testDrift(t, | ||
// setup | ||
`CREATE EXTENSION IF NOT EXISTS hstore;`, | ||
// alter | ||
`DROP EXTENSION IF EXISTS hstore;`, | ||
// expected | ||
`CREATE EXTENSION IF NOT EXISTS "public"."hstore";`, | ||
) | ||
}) | ||
|
||
t.Run("drop", func(t *testing.T) { | ||
testDrift(t, | ||
// setup | ||
``, | ||
// alter | ||
`CREATE EXTENSION IF NOT EXISTS pg_trgm;`, | ||
// expected | ||
`DROP EXTENSION IF EXISTS "public"."pg_trgm";`, | ||
) | ||
}) | ||
} | ||
|
||
func TestDrift_Enums(t *testing.T) { | ||
t.Run("create", func(t *testing.T) { | ||
testDrift(t, | ||
// setup | ||
`CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy');`, | ||
// alter | ||
`DROP TYPE IF EXISTS mood;`, | ||
// expected | ||
`CREATE TYPE "public"."mood" AS ENUM ('sad', 'ok', 'happy');`, | ||
) | ||
}) | ||
|
||
t.Run("drop", func(t *testing.T) { | ||
testDrift(t, | ||
// setup | ||
``, | ||
// alter | ||
`CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy');`, | ||
// expected | ||
`DROP TYPE IF EXISTS "public"."mood";`, | ||
) | ||
}) | ||
|
||
t.Run("alter", func(t *testing.T) { | ||
// TODO - test view closures (fix impl first) | ||
}) | ||
} | ||
|
||
var postgresAddFunctionDefinition = `CREATE OR REPLACE FUNCTION public.add(integer, integer) | ||
RETURNS integer | ||
LANGUAGE sql | ||
AS $function$SELECT $1 + $2;$function$ | ||
;` | ||
|
||
func TestDrift_Functions(t *testing.T) { | ||
t.Run("create", func(t *testing.T) { | ||
t.Skip() // TODO | ||
testDrift(t, | ||
// setup | ||
`CREATE FUNCTION add(integer, integer) RETURNS integer AS 'SELECT $1 + $2;' LANGUAGE SQL;`, | ||
// alter | ||
`DROP FUNCTION IF EXISTS add(integer, integer);`, | ||
// expected | ||
postgresAddFunctionDefinition, | ||
) | ||
}) | ||
|
||
t.Run("drop", func(t *testing.T) { | ||
t.Skip() // TODO | ||
testDrift(t, | ||
// setup | ||
``, | ||
// alter | ||
`CREATE FUNCTION add(integer, integer) RETURNS integer AS 'SELECT $1 + $2;' LANGUAGE SQL;`, | ||
// expected | ||
`DROP FUNCTION IF EXISTS "public"."add"(int4, int4);`, | ||
) | ||
}) | ||
|
||
t.Run("alter mismatched definition", func(t *testing.T) { | ||
testDrift(t, | ||
// setup | ||
`CREATE FUNCTION add(integer, integer) RETURNS integer AS 'SELECT $1 + $2;' LANGUAGE SQL;`, | ||
// alter | ||
`CREATE OR REPLACE FUNCTION add(integer, integer) RETURNS integer AS 'SELECT $1 - $2;' LANGUAGE SQL;`, | ||
// expected | ||
postgresAddFunctionDefinition, | ||
) | ||
}) | ||
|
||
t.Run("does not alter differing argument types", func(t *testing.T) { | ||
testDrift(t, | ||
// setup | ||
`CREATE FUNCTION add(integer, integer) RETURNS integer AS 'SELECT $1 + $2;' LANGUAGE SQL;`, | ||
// alter | ||
`CREATE FUNCTION add(integer, integer, integer) RETURNS integer AS 'SELECT $1 + $2 + $3;' LANGUAGE SQL;`, | ||
// expected (drops extra function) | ||
`DROP FUNCTION IF EXISTS "public"."add"(int4, int4, int4);`, | ||
) | ||
}) | ||
} | ||
|
||
func TestDrift_Tables(t *testing.T) { | ||
// TODO | ||
} | ||
|
||
func TestDrift_Sequences(t *testing.T) { | ||
// TODO | ||
} | ||
|
||
func TestDrift_Views(t *testing.T) { | ||
// TODO | ||
} | ||
|
||
func TestDrift_Triggers(t *testing.T) { | ||
// TODO | ||
} | ||
|
||
func TestDrift_EnumDependencies(t *testing.T) { | ||
// TODO | ||
} | ||
|
||
func TestDrift_ColumnDependencies(t *testing.T) { | ||
// TODO | ||
} | ||
|
||
// | ||
// | ||
|
||
func testDrift(t *testing.T, setupQuery string, alterQuery string, expectedQueries ...string) { | ||
t.Helper() | ||
db := NewTestDB(t) | ||
ctx := context.Background() | ||
|
||
require.NoError(t, db.Exec(ctx, RawQuery(setupQuery))) | ||
|
||
before, err := DescribeSchema(ctx, db) | ||
if err != nil { | ||
t.Fatalf("Failed to describe schema: %v", err) | ||
} | ||
|
||
require.NoError(t, db.Exec(ctx, RawQuery(alterQuery))) | ||
|
||
after, err := DescribeSchema(ctx, db) | ||
require.NoError(t, err) | ||
assert.Equal(t, expectedQueries, Compare(before, after)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters