-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
gormschema: fixed circular foreign keys on postgresql example (#25)
fixes #22
- Loading branch information
Showing
19 changed files
with
303 additions
and
23 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,22 +1,74 @@ | ||
package gormschema | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"ariga.io/atlas-go-sdk/recordriver" | ||
ckmodels "ariga.io/atlas-provider-gorm/internal/testdata/circularfks" | ||
"ariga.io/atlas-provider-gorm/internal/testdata/models" | ||
"github.com/stretchr/testify/require" | ||
"gorm.io/gorm" | ||
) | ||
|
||
func TestConfig(t *testing.T) { | ||
l := New("sqlite", WithConfig( | ||
func TestSQLiteConfig(t *testing.T) { | ||
resetSession() | ||
l := New("sqlite") | ||
sql, err := l.Load(models.Pet{}, models.User{}, ckmodels.Event{}, ckmodels.Location{}) | ||
require.NoError(t, err) | ||
requireEqualContent(t, sql, "testdata/sqlite_default") | ||
resetSession() | ||
l = New("sqlite", WithConfig(&gorm.Config{ | ||
DisableForeignKeyConstraintWhenMigrating: true, | ||
})) | ||
sql, err = l.Load(models.Pet{}, models.User{}) | ||
require.NoError(t, err) | ||
requireEqualContent(t, sql, "testdata/sqlite_no_fk") | ||
resetSession() | ||
} | ||
|
||
func TestPostgreSQLConfig(t *testing.T) { | ||
resetSession() | ||
l := New("postgres") | ||
sql, err := l.Load(ckmodels.Location{}, ckmodels.Event{}, models.User{}, models.Pet{}) | ||
require.NoError(t, err) | ||
requireEqualContent(t, sql, "testdata/postgresql_default") | ||
resetSession() | ||
l = New("postgres", WithConfig( | ||
&gorm.Config{ | ||
DisableForeignKeyConstraintWhenMigrating: true, | ||
})) | ||
sql, err = l.Load(ckmodels.Location{}, ckmodels.Event{}) | ||
require.NoError(t, err) | ||
requireEqualContent(t, sql, "testdata/postgresql_no_fk") | ||
} | ||
|
||
func TestMySQLConfig(t *testing.T) { | ||
resetSession() | ||
l := New("mysql") | ||
sql, err := l.Load(ckmodels.Location{}, ckmodels.Event{}, models.User{}, models.Pet{}) | ||
require.NoError(t, err) | ||
requireEqualContent(t, sql, "testdata/mysql_default") | ||
resetSession() | ||
l = New("mysql", WithConfig( | ||
&gorm.Config{ | ||
DisableForeignKeyConstraintWhenMigrating: true, | ||
}, | ||
)) | ||
sql, err := l.Load(models.Pet{}, models.User{}) | ||
sql, err = l.Load(ckmodels.Location{}, ckmodels.Event{}) | ||
require.NoError(t, err) | ||
requireEqualContent(t, sql, "testdata/mysql_no_fk") | ||
} | ||
|
||
func resetSession() { | ||
sess, ok := recordriver.Session("gorm") | ||
if ok { | ||
sess.Statements = nil | ||
} | ||
} | ||
|
||
func requireEqualContent(t *testing.T, expected, fileName string) { | ||
buf, err := os.ReadFile(fileName) | ||
require.NoError(t, err) | ||
require.Contains(t, sql, "CREATE TABLE `pets`") | ||
require.Contains(t, sql, "CREATE TABLE `users`") | ||
require.NotContains(t, sql, "FOREIGN KEY") | ||
require.Equal(t, expected, string(buf)) | ||
} |
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,7 @@ | ||
CREATE TABLE `events` (`eventId` varchar(191),`locationId` varchar(191),PRIMARY KEY (`eventId`),UNIQUE INDEX `idx_events_location_id` (`locationId`)); | ||
CREATE TABLE `locations` (`locationId` varchar(191),`eventId` varchar(191),PRIMARY KEY (`locationId`),UNIQUE INDEX `idx_locations_event_id` (`eventId`)); | ||
CREATE TABLE `users` (`id` bigint unsigned AUTO_INCREMENT,`created_at` datetime(3) NULL,`updated_at` datetime(3) NULL,`deleted_at` datetime(3) NULL,`name` longtext,PRIMARY KEY (`id`),INDEX `idx_users_deleted_at` (`deleted_at`)); | ||
CREATE TABLE `pets` (`id` bigint unsigned AUTO_INCREMENT,`created_at` datetime(3) NULL,`updated_at` datetime(3) NULL,`deleted_at` datetime(3) NULL,`name` longtext,`user_id` bigint unsigned,PRIMARY KEY (`id`),INDEX `idx_pets_deleted_at` (`deleted_at`)); | ||
ALTER TABLE `events` ADD CONSTRAINT `fk_locations_event` FOREIGN KEY (`locationId`) REFERENCES `locations`(`locationId`); | ||
ALTER TABLE `locations` ADD CONSTRAINT `fk_events_location` FOREIGN KEY (`eventId`) REFERENCES `events`(`eventId`); | ||
ALTER TABLE `pets` ADD CONSTRAINT `fk_users_pets` FOREIGN KEY (`user_id`) REFERENCES `users`(`id`); |
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,2 @@ | ||
CREATE TABLE `events` (`eventId` varchar(191),`locationId` varchar(191),PRIMARY KEY (`eventId`),UNIQUE INDEX `idx_events_location_id` (`locationId`)); | ||
CREATE TABLE `locations` (`locationId` varchar(191),`eventId` varchar(191),PRIMARY KEY (`locationId`),UNIQUE INDEX `idx_locations_event_id` (`eventId`)); |
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,11 @@ | ||
CREATE TABLE "events" ("eventId" text,"locationId" varchar(191),PRIMARY KEY ("eventId")); | ||
CREATE UNIQUE INDEX IF NOT EXISTS "idx_events_location_id" ON "events" ("locationId"); | ||
CREATE TABLE "locations" ("locationId" text,"eventId" varchar(191),PRIMARY KEY ("locationId")); | ||
CREATE UNIQUE INDEX IF NOT EXISTS "idx_locations_event_id" ON "locations" ("eventId"); | ||
CREATE TABLE "users" ("id" bigserial,"created_at" timestamptz,"updated_at" timestamptz,"deleted_at" timestamptz,"name" text,PRIMARY KEY ("id")); | ||
CREATE INDEX IF NOT EXISTS "idx_users_deleted_at" ON "users" ("deleted_at"); | ||
CREATE TABLE "pets" ("id" bigserial,"created_at" timestamptz,"updated_at" timestamptz,"deleted_at" timestamptz,"name" text,"user_id" bigint,PRIMARY KEY ("id")); | ||
CREATE INDEX IF NOT EXISTS "idx_pets_deleted_at" ON "pets" ("deleted_at"); | ||
ALTER TABLE "events" ADD CONSTRAINT "fk_locations_event" FOREIGN KEY ("locationId") REFERENCES "locations"("locationId"); | ||
ALTER TABLE "locations" ADD CONSTRAINT "fk_events_location" FOREIGN KEY ("eventId") REFERENCES "events"("eventId"); | ||
ALTER TABLE "pets" ADD CONSTRAINT "fk_users_pets" FOREIGN KEY ("user_id") REFERENCES "users"("id"); |
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,4 @@ | ||
CREATE TABLE "events" ("eventId" text,"locationId" varchar(191),PRIMARY KEY ("eventId")); | ||
CREATE UNIQUE INDEX IF NOT EXISTS "idx_events_location_id" ON "events" ("locationId"); | ||
CREATE TABLE "locations" ("locationId" text,"eventId" varchar(191),PRIMARY KEY ("locationId")); | ||
CREATE UNIQUE INDEX IF NOT EXISTS "idx_locations_event_id" ON "locations" ("eventId"); |
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,8 @@ | ||
CREATE TABLE `users` (`id` integer,`created_at` datetime,`updated_at` datetime,`deleted_at` datetime,`name` text,PRIMARY KEY (`id`)); | ||
CREATE INDEX `idx_users_deleted_at` ON `users`(`deleted_at`); | ||
CREATE TABLE `pets` (`id` integer,`created_at` datetime,`updated_at` datetime,`deleted_at` datetime,`name` text,`user_id` integer,PRIMARY KEY (`id`),CONSTRAINT `fk_users_pets` FOREIGN KEY (`user_id`) REFERENCES `users`(`id`)); | ||
CREATE INDEX `idx_pets_deleted_at` ON `pets`(`deleted_at`); | ||
CREATE TABLE `locations` (`locationId` text,`eventId` text,PRIMARY KEY (`locationId`),CONSTRAINT `fk_events_location` FOREIGN KEY (`eventId`) REFERENCES `events`(`eventId`)); | ||
CREATE UNIQUE INDEX `idx_locations_event_id` ON `locations`(`eventId`); | ||
CREATE TABLE `events` (`eventId` text,`locationId` text,PRIMARY KEY (`eventId`),CONSTRAINT `fk_locations_event` FOREIGN KEY (`locationId`) REFERENCES `locations`(`locationId`)); | ||
CREATE UNIQUE INDEX `idx_events_location_id` ON `events`(`locationId`); |
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,4 @@ | ||
CREATE TABLE `users` (`id` integer,`created_at` datetime,`updated_at` datetime,`deleted_at` datetime,`name` text,PRIMARY KEY (`id`)); | ||
CREATE INDEX `idx_users_deleted_at` ON `users`(`deleted_at`); | ||
CREATE TABLE `pets` (`id` integer,`created_at` datetime,`updated_at` datetime,`deleted_at` datetime,`name` text,`user_id` integer,PRIMARY KEY (`id`)); | ||
CREATE INDEX `idx_pets_deleted_at` ON `pets`(`deleted_at`); |
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,36 @@ | ||
variable "dialect" { | ||
type = string | ||
} | ||
|
||
locals { | ||
dev_url = { | ||
mysql = "docker://mysql/8/dev" | ||
postgres = "docker://postgres/15" | ||
sqlite = "sqlite://file::memory:?cache=shared" | ||
}[var.dialect] | ||
} | ||
|
||
data "external_schema" "gorm" { | ||
program = [ | ||
"go", | ||
"run", | ||
"-mod=mod", | ||
"ariga.io/atlas-provider-gorm", | ||
"load", | ||
"--path", ".", | ||
"--dialect", var.dialect, | ||
] | ||
} | ||
|
||
env "gorm" { | ||
src = data.external_schema.gorm.url | ||
dev = local.dev_url | ||
migration { | ||
dir = "file://migrations/${var.dialect}" | ||
} | ||
format { | ||
migrate { | ||
diff = "{{ sql . \" \" }}" | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
internal/testdata/circularfks/migrations/mysql/20231217181004.sql
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,18 @@ | ||
-- Create "events" table | ||
CREATE TABLE `events` ( | ||
`eventId` varchar(191) NOT NULL, | ||
`locationId` varchar(191) NULL, | ||
PRIMARY KEY (`eventId`), | ||
UNIQUE INDEX `idx_events_location_id` (`locationId`) | ||
) CHARSET utf8mb4 COLLATE utf8mb4_0900_ai_ci; | ||
-- Create "locations" table | ||
CREATE TABLE `locations` ( | ||
`locationId` varchar(191) NOT NULL, | ||
`eventId` varchar(191) NULL, | ||
PRIMARY KEY (`locationId`), | ||
UNIQUE INDEX `idx_locations_event_id` (`eventId`) | ||
) CHARSET utf8mb4 COLLATE utf8mb4_0900_ai_ci; | ||
-- Modify "events" table | ||
ALTER TABLE `events` ADD CONSTRAINT `fk_locations_event` FOREIGN KEY (`locationId`) REFERENCES `locations` (`locationId`) ON UPDATE NO ACTION ON DELETE NO ACTION; | ||
-- Modify "locations" table | ||
ALTER TABLE `locations` ADD CONSTRAINT `fk_events_location` FOREIGN KEY (`eventId`) REFERENCES `events` (`eventId`) ON UPDATE NO ACTION ON DELETE NO ACTION; |
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,2 @@ | ||
h1:/eDuTTWwc38n5GkA38A1BlSXBzxW2wGyYUwSi36vCn4= | ||
20231217181004.sql h1:XkiJLMZhj3FwEv2QwlAKjLga9LrOhyupMiPMl1LUi/g= |
Oops, something went wrong.