Skip to content

Commit

Permalink
internal: integration tests (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
rotemtam committed Jun 27, 2023
1 parent 9fd06b0 commit 245fee0
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 3 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion gormschema/gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
36 changes: 36 additions & 0 deletions internal/testdata/atlas.hcl
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", "./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 . \" \" }}"
}
}
}
23 changes: 23 additions & 0 deletions internal/testdata/migrations/mysql/20230627123246.sql
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 2 additions & 0 deletions internal/testdata/migrations/mysql/atlas.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
h1:+pZfwCMvS/gKqnOmiZuH5hVicgjUCn1eiV/m8xwCjP4=
20230627123246.sql h1:+bgzC3WJyyIR6Rv/FUvaNXJ1gkbKJlYcEMgp69yORIY=
24 changes: 24 additions & 0 deletions internal/testdata/migrations/postgres/20230627123049.sql
Original file line number Diff line number Diff line change
@@ -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");
2 changes: 2 additions & 0 deletions internal/testdata/migrations/postgres/atlas.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
h1:pXrvfywo43u1iE6XyKM8H/22j6XzEbkl+H8vJv2dEqM=
20230627123049.sql h1:1jYJM2+VCr9152vg6gayCrcEvuT/FE7ufOyZ86VLaOE=
24 changes: 24 additions & 0 deletions internal/testdata/migrations/sqlite/20230627123228.sql
Original file line number Diff line number Diff line change
@@ -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`);
2 changes: 2 additions & 0 deletions internal/testdata/migrations/sqlite/atlas.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
h1:cm5y8e8m7yaqwWh9/So2zgxG+kuzUnMSdoOGLLxKnLk=
20230627123228.sql h1:YfwJdN73sWz1G5/0tU2BtGLyzJCfRQr8blTSquUZ+qo=
7 changes: 5 additions & 2 deletions internal/testdata/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 245fee0

Please sign in to comment.