Skip to content

Commit

Permalink
add CI and integration tests (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
ronenlu authored Jan 24, 2024
1 parent e90d6b9 commit fe2de9a
Show file tree
Hide file tree
Showing 11 changed files with 355 additions and 2 deletions.
69 changes: 69 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
name: Continuous Integration
on:
pull_request:

jobs:
ci-python:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install Python
uses: actions/setup-python@v4
with:
python-version: '3.x'
- name: Install poetry
uses: abatilo/actions-poetry@v2
- name: Setup a local virtual environment (if no poetry.toml file)
run: |
poetry config virtualenvs.create true --local
poetry config virtualenvs.in-project true --local
- uses: actions/cache@v3
name: Define a cache for the virtual environment based on the dependencies lock file
with:
path: ./.venv
key: venv-${{ hashFiles('poetry.lock') }}
- name: Install the project dependencies
run: poetry install
- name: Run lint
run: poetry run ruff --output-format=github .

integration-tests:
strategy:
matrix:
dialect: [ mysql, postgresql, sqlite ]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install Python
uses: actions/setup-python@v4
with:
python-version: '3.x'
- name: Install poetry
uses: abatilo/actions-poetry@v2
- name: Setup a local virtual environment (if no poetry.toml file)
run: |
poetry config virtualenvs.create true --local
poetry config virtualenvs.in-project true --local
- uses: actions/cache@v3
name: Define a cache for the virtual environment based on the dependencies lock file
with:
path: ./.venv
key: venv-${{ hashFiles('poetry.lock') }}
- name: Install the project dependencies
run: poetry install
- name: Install atlas
uses: ariga/setup-atlas@master
- name: Run Test as Standalone
working-directory: ./tests
run: |
atlas migrate diff --env django --var dialect=${{ matrix.dialect }}
- name: Verify migrations generated
working-directory: ./tests
run: |
status=$(git status . --porcelain)
if [ -n "$status" ]; then
echo "you need to run 'atlas migrate diff --env django and commit the changes"
echo "$status"
git --no-pager diff
exit 1
fi
127 changes: 126 additions & 1 deletion poetry.lock

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ readme = "README.md"
python = "^3.11"
django = ">=4.2"

[tool.poetry.group.dev.dependencies]
ruff = "^0.1.14"
psycopg2-binary = "^2.9.9"
mysqlclient = "^2.2.1"

[build-system]
requires = ["poetry-core"]
Expand Down
2 changes: 1 addition & 1 deletion tests/app2/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

class User(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50, null=True)
roll = models.CharField(max_length=100)


Expand Down
35 changes: 35 additions & 0 deletions tests/atlas.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
variable "dialect" {
type = string
}

locals {
dev_url = {
mysql = "docker://mysql/8/dev"
postgresql = "docker://postgres/15"
sqlite = "sqlite://?mode=memory&_fk=1"
}[var.dialect]
}

data "external_schema" "django" {
program = [
"poetry",
"run",
"python3",
"../manage.py",
"atlas-provider-django",
"--dialect", var.dialect, // mysql | postgresql | sqlite
]
}

env "django" {
src = data.external_schema.django.url
dev = local.dev_url
migration {
dir = "file://migrations/${var.dialect}"
}
format {
migrate {
diff = "{{ sql . \" \" }}"
}
}
}
38 changes: 38 additions & 0 deletions tests/migrations/mysql/20240123150434.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
-- Create "app1_musician" table
CREATE TABLE `app1_musician` (
`id` bigint NOT NULL AUTO_INCREMENT,
`first_name` varchar(50) NOT NULL,
`last_name` varchar(50) NOT NULL,
`instrument` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) CHARSET utf8mb4 COLLATE utf8mb4_0900_ai_ci;
-- Create "app1_album" table
CREATE TABLE `app1_album` (
`id` bigint NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
`release_date` date NOT NULL,
`num_stars` int NOT NULL,
`artist_id` bigint NOT NULL,
PRIMARY KEY (`id`),
INDEX `app1_album_artist_id_aed0987a_fk_app1_musician_id` (`artist_id`),
CONSTRAINT `app1_album_artist_id_aed0987a_fk_app1_musician_id` FOREIGN KEY (`artist_id`) REFERENCES `app1_musician` (`id`) ON UPDATE NO ACTION ON DELETE NO ACTION
) CHARSET utf8mb4 COLLATE utf8mb4_0900_ai_ci;
-- Create "app2_user" table
CREATE TABLE `app2_user` (
`id` bigint NOT NULL AUTO_INCREMENT,
`first_name` varchar(50) NOT NULL,
`last_name` varchar(50) NULL,
`roll` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) CHARSET utf8mb4 COLLATE utf8mb4_0900_ai_ci;
-- Create "app2_blog" table
CREATE TABLE `app2_blog` (
`id` bigint NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
`created_at` date NOT NULL,
`num_stars` int NOT NULL,
`author_id` bigint NOT NULL,
PRIMARY KEY (`id`),
INDEX `app2_blog_author_id_1675e606_fk_app2_user_id` (`author_id`),
CONSTRAINT `app2_blog_author_id_1675e606_fk_app2_user_id` FOREIGN KEY (`author_id`) REFERENCES `app2_user` (`id`) ON UPDATE NO ACTION ON DELETE NO ACTION
) CHARSET utf8mb4 COLLATE utf8mb4_0900_ai_ci;
2 changes: 2 additions & 0 deletions tests/migrations/mysql/atlas.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
h1:t+fHYX8aCiiNQ7IoDKRSIdnAeO9Mp7o6Eug9hhCCTTE=
20240123150434.sql h1:wZOaDDiz5ov9fn+bBctKy0/HoDZ4QL6VTzk58TialGg=
40 changes: 40 additions & 0 deletions tests/migrations/postgresql/20240123150442.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
-- Create "app1_musician" table
CREATE TABLE "public"."app1_musician" (
"id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY,
"first_name" character varying(50) NOT NULL,
"last_name" character varying(50) NOT NULL,
"instrument" character varying(100) NOT NULL,
PRIMARY KEY ("id")
);
-- Create "app1_album" table
CREATE TABLE "public"."app1_album" (
"id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY,
"name" character varying(100) NOT NULL,
"release_date" date NOT NULL,
"num_stars" integer NOT NULL,
"artist_id" bigint NOT NULL,
PRIMARY KEY ("id"),
CONSTRAINT "app1_album_artist_id_aed0987a_fk_app1_musician_id" FOREIGN KEY ("artist_id") REFERENCES "public"."app1_musician" ("id") ON UPDATE NO ACTION ON DELETE NO ACTION
);
-- Create index "app1_album_artist_id_aed0987a" to table: "app1_album"
CREATE INDEX "app1_album_artist_id_aed0987a" ON "public"."app1_album" ("artist_id");
-- Create "app2_user" table
CREATE TABLE "public"."app2_user" (
"id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY,
"first_name" character varying(50) NOT NULL,
"last_name" character varying(50) NULL,
"roll" character varying(100) NOT NULL,
PRIMARY KEY ("id")
);
-- Create "app2_blog" table
CREATE TABLE "public"."app2_blog" (
"id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY,
"name" character varying(100) NOT NULL,
"created_at" date NOT NULL,
"num_stars" integer NOT NULL,
"author_id" bigint NOT NULL,
PRIMARY KEY ("id"),
CONSTRAINT "app2_blog_author_id_1675e606_fk_app2_user_id" FOREIGN KEY ("author_id") REFERENCES "public"."app2_user" ("id") ON UPDATE NO ACTION ON DELETE NO ACTION
);
-- Create index "app2_blog_author_id_1675e606" to table: "app2_blog"
CREATE INDEX "app2_blog_author_id_1675e606" ON "public"."app2_blog" ("author_id");
2 changes: 2 additions & 0 deletions tests/migrations/postgresql/atlas.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
h1:wPrdpJIc9fRclW3cMGAqDMqK22dhVkEahLLae2v2Jp4=
20240123150442.sql h1:OssKCxrsi6AnIM5iRKN/lrbHEzgehNTrR0t+AIefm1I=
36 changes: 36 additions & 0 deletions tests/migrations/sqlite/20240123150447.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
-- Create "app1_musician" table
CREATE TABLE `app1_musician` (
`id` integer NOT NULL PRIMARY KEY AUTOINCREMENT,
`first_name` varchar NOT NULL,
`last_name` varchar NOT NULL,
`instrument` varchar NOT NULL
);
-- Create "app1_album" table
CREATE TABLE `app1_album` (
`id` integer NOT NULL PRIMARY KEY AUTOINCREMENT,
`name` varchar NOT NULL,
`release_date` date NOT NULL,
`num_stars` integer NOT NULL,
`artist_id` bigint NOT NULL,
CONSTRAINT `0` FOREIGN KEY (`artist_id`) REFERENCES `app1_musician` (`id`) ON UPDATE NO ACTION ON DELETE NO ACTION
);
-- Create index "app1_album_artist_id_aed0987a" to table: "app1_album"
CREATE INDEX `app1_album_artist_id_aed0987a` ON `app1_album` (`artist_id`);
-- Create "app2_user" table
CREATE TABLE `app2_user` (
`id` integer NOT NULL PRIMARY KEY AUTOINCREMENT,
`first_name` varchar NOT NULL,
`last_name` varchar NULL,
`roll` varchar NOT NULL
);
-- Create "app2_blog" table
CREATE TABLE `app2_blog` (
`id` integer NOT NULL PRIMARY KEY AUTOINCREMENT,
`name` varchar NOT NULL,
`created_at` date NOT NULL,
`num_stars` integer NOT NULL,
`author_id` bigint NOT NULL,
CONSTRAINT `0` FOREIGN KEY (`author_id`) REFERENCES `app2_user` (`id`) ON UPDATE NO ACTION ON DELETE NO ACTION
);
-- Create index "app2_blog_author_id_1675e606" to table: "app2_blog"
CREATE INDEX `app2_blog_author_id_1675e606` ON `app2_blog` (`author_id`);
2 changes: 2 additions & 0 deletions tests/migrations/sqlite/atlas.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
h1:BH7VCgohPrXE2+BAyinbXWinObUasKWvewdQX0rUxJ4=
20240123150447.sql h1:dvS9iDdf9wRJYX8t883ehkjEuXqmkvbOIYE+3x0tmlg=

0 comments on commit fe2de9a

Please sign in to comment.