Skip to content

Commit

Permalink
fix: missing indices and foreign keys
Browse files Browse the repository at this point in the history
  • Loading branch information
alnr committed Mar 6, 2024
1 parent a3b56b7 commit 6733fd8
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 23 deletions.
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ go 1.21

replace (
github.com/go-sql-driver/mysql => github.com/go-sql-driver/mysql v1.7.2-0.20231005084435-37980127edfb
github.com/gorilla/sessions => github.com/ory/sessions v1.2.2-0.20220110165800-b09c17334dc2

// https://github.com/gobuffalo/pop/pull/833
github.com/gobuffalo/pop/v6 => github.com/alnr/pop/v6 v6.1.2-0.20240220141536-653aad67c0c2

github.com/gorilla/sessions => github.com/ory/sessions v1.2.2-0.20220110165800-b09c17334dc2
github.com/mattn/go-sqlite3 => github.com/mattn/go-sqlite3 v1.14.16

// Use the internal httpclient which can be generated in this codebase but mark it as the
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRF
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho=
github.com/alecthomas/units v0.0.0-20210208195552-ff826a37aa15 h1:AUNCr9CiJuwrRYS3XieqF+Z9B9gNxo/eANAJCF2eiN4=
github.com/alecthomas/units v0.0.0-20210208195552-ff826a37aa15/go.mod h1:OMCwj8VM1Kc9e19TLln2VL61YJF0x1XFtfdL4JdbSyE=
github.com/alnr/pop/v6 v6.1.2-0.20240220141536-653aad67c0c2 h1:GcIj2UDicQcj5xPwdpyYzqFP3GITJFzuoRyvqZTHz1c=
github.com/alnr/pop/v6 v6.1.2-0.20240220141536-653aad67c0c2/go.mod h1:1n7jAmI1i7fxuXPZjZb0VBPQDbksRtCoFnrDV5IsvaI=
github.com/arbovm/levenshtein v0.0.0-20160628152529-48b4e1c0c4d0 h1:jfIu9sQUG6Ig+0+Ap1h4unLjW6YQJpKZVmUzxsD4E/Q=
github.com/arbovm/levenshtein v0.0.0-20160628152529-48b4e1c0c4d0/go.mod h1:t2tdKJDJF9BV14lnkjHmOQgcvEKgtqs5a1N3LNdJhGE=
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
Expand Down Expand Up @@ -329,8 +331,6 @@ github.com/gobuffalo/packr/v2 v2.2.0/go.mod h1:CaAwI0GPIAv+5wKLtv8Afwl+Cm78K/I/V
github.com/gobuffalo/plush/v4 v4.1.16/go.mod h1:6t7swVsarJ8qSLw1qyAH/KbrcSTwdun2ASEQkOznakg=
github.com/gobuffalo/plush/v4 v4.1.18 h1:bnPjdMTEUQHqj9TNX2Ck3mxEXYZa+0nrFMNM07kpX9g=
github.com/gobuffalo/plush/v4 v4.1.18/go.mod h1:xi2tJIhFI4UdzIL8sxZtzGYOd2xbBpcFbLZlIPGGZhU=
github.com/gobuffalo/pop/v6 v6.1.2-0.20230318123913-c85387acc9a0 h1:+LF3Enal3HZ+rFmaLZfBRNHKqtnoA0d8jk0Iio8InZM=
github.com/gobuffalo/pop/v6 v6.1.2-0.20230318123913-c85387acc9a0/go.mod h1:1n7jAmI1i7fxuXPZjZb0VBPQDbksRtCoFnrDV5IsvaI=
github.com/gobuffalo/syncx v0.0.0-20190224160051-33c29581e754/go.mod h1:HhnNqWY95UYwwW3uSASeV7vtgYkT2t16hJgV3AEPUpw=
github.com/gobuffalo/tags/v3 v3.1.4 h1:X/ydLLPhgXV4h04Hp2xlbI2oc5MDaa7eub6zw8oHjsM=
github.com/gobuffalo/tags/v3 v3.1.4/go.mod h1:ArRNo3ErlHO8BtdA0REaZxijuWnWzF6PUXngmMXd2I0=
Expand Down
25 changes: 11 additions & 14 deletions persistence/sql/migratest/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func TestMigrations_Cockroach(t *testing.T) {

func testDatabase(t *testing.T, db string, c *pop.Connection) {
ctx := context.Background()
l := logrusx.New("", "", logrusx.ForceLevel(logrus.ErrorLevel))
l := logrusx.New("", "", logrusx.ForceLevel(logrus.DebugLevel))

t.Logf("Cleaning up before migrations")
_ = os.Remove("../migrations/sql/schema.sql")
Expand All @@ -130,15 +130,14 @@ func testDatabase(t *testing.T, db string, c *pop.Connection) {
}
t.Logf("URL: %s", url)

t.Run("suite=up", func(t *testing.T) {
tm, err := popx.NewMigrationBox(
os.DirFS("../migrations/sql"),
popx.NewMigrator(c, logrusx.New("", "", logrusx.ForceLevel(logrus.DebugLevel)), nil, 1*time.Minute),
popx.WithTestdata(t, os.DirFS("./testdata")),
)
require.NoError(t, err)
require.NoError(t, tm.Up(ctx))
})
tm, err := popx.NewMigrationBox(
os.DirFS("../migrations/sql"),
popx.NewMigrator(c, l, nil, 1*time.Minute),
popx.WithTestdata(t, os.DirFS("./testdata")),
)
require.NoError(t, err)
tm.DumpMigrations = true
require.NoError(t, tm.Up(ctx))

t.Run("suite=fixtures", func(t *testing.T) {
wg := &sync.WaitGroup{}
Expand Down Expand Up @@ -423,8 +422,6 @@ func testDatabase(t *testing.T, db string, c *pop.Connection) {
})
})

t.Run("suite=down", func(t *testing.T) {
tm := popx.NewTestMigrator(t, c, os.DirFS("../migrations/sql"), os.DirFS("./testdata"), l)
require.NoError(t, tm.Down(ctx, -1))
})
tm.DumpMigrations = false
require.NoError(t, tm.Down(ctx, -1))
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
DROP INDEX IF EXISTS identity_login_codes@identity_login_codes_identity_id_idx;
DROP INDEX IF EXISTS identity_login_codes@identity_login_codes_flow_id_idx;
DROP INDEX IF EXISTS identity_recovery_codes@identity_recovery_codes_flow_id_idx;
DROP INDEX IF EXISTS identity_registration_codes@identity_registration_codes_flow_id_idx;
DROP INDEX IF EXISTS identity_verification_codes@identity_verification_codes_flow_id_idx;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
DROP INDEX IF EXISTS identity_login_codes.identity_login_codes_identity_id_idx;
DROP INDEX IF EXISTS identity_login_codes.identity_login_codes_flow_id_idx;
DROP INDEX IF EXISTS identity_recovery_codes.identity_recovery_codes_flow_id_idx;
DROP INDEX IF EXISTS identity_registration_codes.identity_registration_codes_flow_id_idx;
DROP INDEX IF EXISTS identity_verification_codes.identity_verification_codes_flow_id_idx;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
ALTER TABLE `identity_recovery_codes`
DROP FOREIGN KEY `identity_recovery_codes_identity_id_fk`,
ADD CONSTRAINT `identity_recovery_tokens_identity_id_fk` FOREIGN KEY (`identity_id`) REFERENCES `identities` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT;

ALTER TABLE `identity_login_codes`
DROP FOREIGN KEY `identity_login_codes_identity_id_fk`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- This FK was previously misnamed.
ALTER TABLE `identity_recovery_codes`
DROP FOREIGN KEY `identity_recovery_tokens_identity_id_fk`,
ADD CONSTRAINT `identity_recovery_codes_identity_id_fk` FOREIGN KEY (`identity_id`) REFERENCES `identities` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT;

-- Missing FK
ALTER TABLE `identity_login_codes`
ADD CONSTRAINT `identity_login_codes_identity_id_fk` FOREIGN KEY (`identity_id`) REFERENCES `identities` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT;

-- MySQL has created the remaining indices automatically together with the foreign key constraints.

-- CREATE INDEX identity_login_codes_identity_id_idx ON identity_login_codes (identity_id ASC);
-- CREATE INDEX identity_login_codes_flow_id_idx ON identity_login_codes (selfservice_login_flow_id ASC);
-- CREATE INDEX identity_registration_codes_flow_id_idx ON identity_registration_codes (selfservice_registration_flow_id ASC);
-- CREATE INDEX identity_recovery_codes_flow_id_idx ON identity_recovery_codes (selfservice_recovery_flow_id ASC);
-- CREATE INDEX identity_verification_codes_flow_id_idx ON identity_verification_codes (selfservice_verification_flow_id ASC);
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE INDEX IF NOT EXISTS identity_login_codes_identity_id_idx ON identity_login_codes (identity_id ASC);
CREATE INDEX IF NOT EXISTS identity_login_codes_flow_id_idx ON identity_login_codes (selfservice_login_flow_id ASC);
CREATE INDEX IF NOT EXISTS identity_registration_codes_flow_id_idx ON identity_registration_codes (selfservice_registration_flow_id ASC);
CREATE INDEX IF NOT EXISTS identity_recovery_codes_flow_id_idx ON identity_recovery_codes (selfservice_recovery_flow_id ASC);
CREATE INDEX IF NOT EXISTS identity_verification_codes_flow_id_idx ON identity_verification_codes (selfservice_verification_flow_id ASC);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
ALTER TABLE identity_login_codes
ADD CONSTRAINT identity_login_codes_identity_id_fk FOREIGN KEY (identity_id) REFERENCES identities (id) ON DELETE CASCADE ON UPDATE RESTRICT;

CREATE INDEX IF NOT EXISTS identity_login_codes_identity_id_idx ON identity_login_codes (identity_id ASC);
CREATE INDEX IF NOT EXISTS identity_login_codes_flow_id_idx ON identity_login_codes (selfservice_login_flow_id ASC);
CREATE INDEX IF NOT EXISTS identity_registration_codes_flow_id_idx ON identity_registration_codes (selfservice_registration_flow_id ASC);
CREATE INDEX IF NOT EXISTS identity_recovery_codes_flow_id_idx ON identity_recovery_codes (selfservice_recovery_flow_id ASC);
CREATE INDEX IF NOT EXISTS identity_verification_codes_flow_id_idx ON identity_verification_codes (selfservice_verification_flow_id ASC);

0 comments on commit 6733fd8

Please sign in to comment.