-
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
47 changed files
with
701 additions
and
152 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
.vscode | ||
vendor | ||
migrations |
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 |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package pgutil | ||
|
||
import ( | ||
"path" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
var testUpQuery = `-- Create the comments table | ||
CREATE TABLE comments ( | ||
id SERIAL PRIMARY KEY, | ||
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(), | ||
); | ||
` | ||
|
||
var testDownQuery = `-- Drop the comments table | ||
DROP TABLE IF EXISTS comments; | ||
` | ||
|
||
var testConcurrentIndexUpQuery = `-- Create a concurrent index | ||
CREATE INDEX CONCURRENTLY idx_users_email ON users (email);` | ||
|
||
var testConcurrentIndexDownQuery = `-- Drop the concurrent index | ||
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"))) | ||
require.NoError(t, err) | ||
require.Len(t, definitions, 3) | ||
|
||
assert.Equal(t, Definition{ | ||
ID: 3, | ||
Name: "third", | ||
UpQuery: RawQuery(testUpQuery), | ||
DownQuery: RawQuery(testDownQuery), | ||
}, definitions[2]) | ||
}) | ||
|
||
t.Run("CIC pattern", func(t *testing.T) { | ||
t.Skip() | ||
definitions, err := ReadMigrations(NewFilesystemMigrationReader(path.Join("testdata", "cic_pattern"))) | ||
require.NoError(t, err) | ||
require.Len(t, definitions, 4) | ||
|
||
assert.Equal(t, Definition{ | ||
ID: 3, | ||
Name: "third", | ||
UpQuery: RawQuery(testConcurrentIndexUpQuery), | ||
DownQuery: RawQuery(testConcurrentIndexDownQuery), | ||
IndexMetadata: &IndexMetadata{ | ||
TableName: "users", | ||
IndexName: "idx_users_email", | ||
}, | ||
}, definitions[3]) | ||
}) | ||
|
||
t.Run("duplicate identifiers", func(t *testing.T) { | ||
_, err := ReadMigrations(NewFilesystemMigrationReader(path.Join("testdata", "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"))) | ||
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"))) | ||
assert.ErrorContains(t, err, `"create index concurrently" is not the only statement in the up migration`) | ||
}) | ||
} |
Oops, something went wrong.