From 245fee0b57e03454c85a11b791b8bd3bdb888c9a Mon Sep 17 00:00:00 2001 From: Rotem Tamir Date: Tue, 27 Jun 2023 15:47:31 +0300 Subject: [PATCH] internal: integration tests (#6) --- .github/workflows/ci.yaml | 24 +++++++++++++ gormschema/gorm.go | 2 +- internal/testdata/atlas.hcl | 36 +++++++++++++++++++ .../migrations/mysql/20230627123246.sql | 23 ++++++++++++ internal/testdata/migrations/mysql/atlas.sum | 2 ++ .../migrations/postgres/20230627123049.sql | 24 +++++++++++++ .../testdata/migrations/postgres/atlas.sum | 2 ++ .../migrations/sqlite/20230627123228.sql | 24 +++++++++++++ internal/testdata/migrations/sqlite/atlas.sum | 2 ++ internal/testdata/models/models.go | 7 ++-- 10 files changed, 143 insertions(+), 3 deletions(-) create mode 100644 internal/testdata/atlas.hcl create mode 100644 internal/testdata/migrations/mysql/20230627123246.sql create mode 100644 internal/testdata/migrations/mysql/atlas.sum create mode 100644 internal/testdata/migrations/postgres/20230627123049.sql create mode 100644 internal/testdata/migrations/postgres/atlas.sum create mode 100644 internal/testdata/migrations/sqlite/20230627123228.sql create mode 100644 internal/testdata/migrations/sqlite/atlas.sum diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index ce2c17a..c0f1b62 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -26,3 +26,27 @@ jobs: go-version: '1.20' - name: Run tests run: go test -race ./... + integration-test: + strategy: + matrix: + dialect: [mysql, postgres, sqlite] + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-go@v4 + with: + go-version: '1.20' + - run: | + curl -sSf 'https://atlasgo.sh?test=1' | env ATLAS_DEBUG=true sh + - working-directory: internal/testdata + run: | + atlas migrate diff --env gorm --var dialect=${{ matrix.dialect }} + - name: Verify migrations generated + run: | + status=$(git status --porcelain) + if [ -n "$status" ]; then + echo "you need to run 'atlas migrate diff --env gorm' and commit the changes" + echo "$status" + git --no-pager diff + exit 1 + fi \ No newline at end of file diff --git a/gormschema/gorm.go b/gormschema/gorm.go index 07d7767..48a875f 100644 --- a/gormschema/gorm.go +++ b/gormschema/gorm.go @@ -56,7 +56,7 @@ func (l *Loader) Load(models ...any) (string, error) { if err != nil { return "", err } - if err := db.Migrator().CreateTable(models...); err != nil { + if err := db.AutoMigrate(models...); err != nil { return "", err } s, ok := recordriver.Session("gorm") diff --git a/internal/testdata/atlas.hcl b/internal/testdata/atlas.hcl new file mode 100644 index 0000000..a9bd906 --- /dev/null +++ b/internal/testdata/atlas.hcl @@ -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", "./models", + "--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 . \" \" }}" + } + } +} diff --git a/internal/testdata/migrations/mysql/20230627123246.sql b/internal/testdata/migrations/mysql/20230627123246.sql new file mode 100644 index 0000000..b93196f --- /dev/null +++ b/internal/testdata/migrations/mysql/20230627123246.sql @@ -0,0 +1,23 @@ +-- Create "users" table +CREATE TABLE `users` ( + `id` bigint unsigned NOT NULL AUTO_INCREMENT, + `created_at` datetime(3) NULL, + `updated_at` datetime(3) NULL, + `deleted_at` datetime(3) NULL, + `name` longtext NULL, + PRIMARY KEY (`id`), + INDEX `idx_users_deleted_at` (`deleted_at`) +) CHARSET utf8mb4 COLLATE utf8mb4_0900_ai_ci; +-- Create "pets" table +CREATE TABLE `pets` ( + `id` bigint unsigned NOT NULL AUTO_INCREMENT, + `created_at` datetime(3) NULL, + `updated_at` datetime(3) NULL, + `deleted_at` datetime(3) NULL, + `name` longtext NULL, + `user_id` bigint unsigned NULL, + PRIMARY KEY (`id`), + INDEX `fk_users_pets` (`user_id`), + INDEX `idx_pets_deleted_at` (`deleted_at`), + CONSTRAINT `fk_users_pets` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON UPDATE NO ACTION ON DELETE NO ACTION +) CHARSET utf8mb4 COLLATE utf8mb4_0900_ai_ci; diff --git a/internal/testdata/migrations/mysql/atlas.sum b/internal/testdata/migrations/mysql/atlas.sum new file mode 100644 index 0000000..12d9fe9 --- /dev/null +++ b/internal/testdata/migrations/mysql/atlas.sum @@ -0,0 +1,2 @@ +h1:+pZfwCMvS/gKqnOmiZuH5hVicgjUCn1eiV/m8xwCjP4= +20230627123246.sql h1:+bgzC3WJyyIR6Rv/FUvaNXJ1gkbKJlYcEMgp69yORIY= diff --git a/internal/testdata/migrations/postgres/20230627123049.sql b/internal/testdata/migrations/postgres/20230627123049.sql new file mode 100644 index 0000000..f315a62 --- /dev/null +++ b/internal/testdata/migrations/postgres/20230627123049.sql @@ -0,0 +1,24 @@ +-- Create "users" table +CREATE TABLE "public"."users" ( + "id" bigserial NOT NULL, + "created_at" timestamptz NULL, + "updated_at" timestamptz NULL, + "deleted_at" timestamptz NULL, + "name" text NULL, + PRIMARY KEY ("id") +); +-- Create index "idx_users_deleted_at" to table: "users" +CREATE INDEX "idx_users_deleted_at" ON "public"."users" ("deleted_at"); +-- Create "pets" table +CREATE TABLE "public"."pets" ( + "id" bigserial NOT NULL, + "created_at" timestamptz NULL, + "updated_at" timestamptz NULL, + "deleted_at" timestamptz NULL, + "name" text NULL, + "user_id" bigint NULL, + PRIMARY KEY ("id"), + CONSTRAINT "fk_users_pets" FOREIGN KEY ("user_id") REFERENCES "public"."users" ("id") ON UPDATE NO ACTION ON DELETE NO ACTION +); +-- Create index "idx_pets_deleted_at" to table: "pets" +CREATE INDEX "idx_pets_deleted_at" ON "public"."pets" ("deleted_at"); diff --git a/internal/testdata/migrations/postgres/atlas.sum b/internal/testdata/migrations/postgres/atlas.sum new file mode 100644 index 0000000..af47b93 --- /dev/null +++ b/internal/testdata/migrations/postgres/atlas.sum @@ -0,0 +1,2 @@ +h1:pXrvfywo43u1iE6XyKM8H/22j6XzEbkl+H8vJv2dEqM= +20230627123049.sql h1:1jYJM2+VCr9152vg6gayCrcEvuT/FE7ufOyZ86VLaOE= diff --git a/internal/testdata/migrations/sqlite/20230627123228.sql b/internal/testdata/migrations/sqlite/20230627123228.sql new file mode 100644 index 0000000..dfdb973 --- /dev/null +++ b/internal/testdata/migrations/sqlite/20230627123228.sql @@ -0,0 +1,24 @@ +-- Create "users" table +CREATE TABLE `users` ( + `id` integer NULL, + `created_at` datetime NULL, + `updated_at` datetime NULL, + `deleted_at` datetime NULL, + `name` text NULL, + PRIMARY KEY (`id`) +); +-- Create index "idx_users_deleted_at" to table: "users" +CREATE INDEX `idx_users_deleted_at` ON `users` (`deleted_at`); +-- Create "pets" table +CREATE TABLE `pets` ( + `id` integer NULL, + `created_at` datetime NULL, + `updated_at` datetime NULL, + `deleted_at` datetime NULL, + `name` text NULL, + `user_id` integer NULL, + PRIMARY KEY (`id`), + CONSTRAINT `fk_users_pets` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON UPDATE NO ACTION ON DELETE NO ACTION +); +-- Create index "idx_pets_deleted_at" to table: "pets" +CREATE INDEX `idx_pets_deleted_at` ON `pets` (`deleted_at`); diff --git a/internal/testdata/migrations/sqlite/atlas.sum b/internal/testdata/migrations/sqlite/atlas.sum new file mode 100644 index 0000000..3ce4c7d --- /dev/null +++ b/internal/testdata/migrations/sqlite/atlas.sum @@ -0,0 +1,2 @@ +h1:cm5y8e8m7yaqwWh9/So2zgxG+kuzUnMSdoOGLLxKnLk= +20230627123228.sql h1:YfwJdN73sWz1G5/0tU2BtGLyzJCfRQr8blTSquUZ+qo= diff --git a/internal/testdata/models/models.go b/internal/testdata/models/models.go index cf47f22..31dd8da 100644 --- a/internal/testdata/models/models.go +++ b/internal/testdata/models/models.go @@ -5,11 +5,14 @@ import "gorm.io/gorm" type User struct { gorm.Model Name string + Pets []Pet } type Pet struct { - ID uint `gorm:"column:foo"` - Name string + gorm.Model + Name string + User User + UserID uint } type Toy struct {