Skip to content

Commit

Permalink
support mssql driver (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
ronenlu authored Jan 25, 2024
1 parent d44fbe7 commit 7b3c3b9
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 10 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
integration-tests:
strategy:
matrix:
dialect: [ mysql, postgresql, sqlite ]
dialect: [ mysql, postgresql, sqlite, mssql ]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -55,10 +55,12 @@ jobs:
run: poetry install
- name: Install atlas
uses: ariga/setup-atlas@master
- name: Run Test as Standalone
- name: Run migrate diff
working-directory: ./tests
run: |
atlas migrate diff --env django --var dialect=${{ matrix.dialect }}
env:
ATLAS_TOKEN: ${{ secrets.ATLAS_TOKEN }}
- name: Verify migrations generated
working-directory: ./tests
run: |
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ data "external_schema" "django" {
"python",
"manage.py",
"atlas-provider-django",
"--dialect", "mysql" // mariadb | postgresql | sqlite
"--dialect", "mysql" // mariadb | postgresql | sqlite | mssql
// if you want to only load a subset of your app models, you can specify the apps by adding
// "--apps", "app1", "app2", "app3"
]
Expand Down Expand Up @@ -94,6 +94,7 @@ The provider supports the following databases:
* MariaDB
* PostgreSQL
* SQLite
* Microsoft SQL Server

### Issues

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Dialect(str, Enum):
mariadb = "mariadb"
sqlite = "sqlite"
postgresql = "postgresql"
mssql = "mssql"

def __str__(self):
return self.value
Expand Down Expand Up @@ -110,6 +111,13 @@ def get_connection_by_dialect(dialect):
}, "mysql")
conn.SchemaEditorClass = MockMySQLDatabaseSchemaEditor
conn.features = MockMariaDBDatabaseFeatures(conn)
case Dialect.mssql:
# import dynamically to avoid unixodbc not installed error on mac os for other dialects
from mssql.base import DatabaseWrapper as MSSQLDatabaseWrapper
conn = MSSQLDatabaseWrapper({
"ENGINE": "mssql",
"OPTIONS": {},
}, "mssql")
return conn


Expand Down Expand Up @@ -158,6 +166,7 @@ def collect_sql(self, plan):
# Copied from Django's sqlmigrate command: https://github.com/django/django/blob/8a1727dc7f66db7f0131d545812f77544f35aa57/django/core/management/commands/sqlmigrate.py#L40-L83
# Code licensed under the BSD 3-Clause License: https://github.com/django/django/blob/main/LICENSE
def mock_handle(self, *args, **options):
self.output_transaction = False
connection = get_connection_by_dialect(current_dialect)
loader = MockMigrationLoader(connection, replace_migrations=False, load=False)
loader.build_graph()
Expand Down
69 changes: 68 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ python = "^3.11"
django = ">=4.2"
psycopg2-binary = "^2.9.9"
mysqlclient = "^2.2.1"
mssql-django = "^1.4"
pyodbc = "^5.0.1"

[tool.poetry.group.dev.dependencies]
ruff = "^0.1.14"
Expand Down
1 change: 1 addition & 0 deletions tests/atlas.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ locals {
dev_url = {
mysql = "docker://mysql/8/dev"
postgresql = "docker://postgres/15"
mssql = "docker://sqlserver/2022-latest"
sqlite = "sqlite://?mode=memory&_fk=1"
}[var.dialect]
}
Expand Down
4 changes: 0 additions & 4 deletions tests/expected_all_apps.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
BEGIN;
--
-- Create model Musician
--
Expand All @@ -8,8 +7,6 @@ CREATE TABLE `app1_musician` (`id` bigint AUTO_INCREMENT NOT NULL PRIMARY KEY, `
--
CREATE TABLE `app1_album` (`id` bigint AUTO_INCREMENT NOT NULL PRIMARY KEY, `name` varchar(100) NOT NULL, `release_date` date NOT NULL, `num_stars` integer NOT NULL, `artist_id` bigint NOT NULL);
ALTER TABLE `app1_album` ADD CONSTRAINT `app1_album_artist_id_aed0987a_fk_app1_musician_id` FOREIGN KEY (`artist_id`) REFERENCES `app1_musician` (`id`);
COMMIT;
BEGIN;
--
-- Create model User
--
Expand All @@ -19,4 +16,3 @@ CREATE TABLE `app2_user` (`id` bigint AUTO_INCREMENT NOT NULL PRIMARY KEY, `firs
--
CREATE TABLE `app2_blog` (`id` bigint AUTO_INCREMENT NOT NULL PRIMARY KEY, `name` varchar(100) NOT NULL, `created_at` date NOT NULL, `num_stars` integer NOT NULL, `author_id` bigint NOT NULL);
ALTER TABLE `app2_blog` ADD CONSTRAINT `app2_blog_author_id_1675e606_fk_app2_user_id` FOREIGN KEY (`author_id`) REFERENCES `app2_user` (`id`);
COMMIT;
2 changes: 0 additions & 2 deletions tests/expected_app1.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
BEGIN;
--
-- Create model Musician
--
Expand All @@ -8,4 +7,3 @@ CREATE TABLE `app1_musician` (`id` bigint AUTO_INCREMENT NOT NULL PRIMARY KEY, `
--
CREATE TABLE `app1_album` (`id` bigint AUTO_INCREMENT NOT NULL PRIMARY KEY, `name` varchar(100) NOT NULL, `release_date` date NOT NULL, `num_stars` integer NOT NULL, `artist_id` bigint NOT NULL);
ALTER TABLE `app1_album` ADD CONSTRAINT `app1_album_artist_id_aed0987a_fk_app1_musician_id` FOREIGN KEY (`artist_id`) REFERENCES `app1_musician` (`id`);
COMMIT;
42 changes: 42 additions & 0 deletions tests/migrations/mssql/20240125082659.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
-- Create "app1_musician" table
CREATE TABLE [app1_musician] (
[id] bigint IDENTITY (1, 1) NOT NULL,
[first_name] nvarchar(50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[last_name] nvarchar(50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[instrument] nvarchar(100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
CONSTRAINT [PK_app1_musician] PRIMARY KEY CLUSTERED ([id] ASC)
);
-- Create "app1_album" table
CREATE TABLE [app1_album] (
[id] bigint IDENTITY (1, 1) NOT NULL,
[name] nvarchar(100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[release_date] date NOT NULL,
[num_stars] int NOT NULL,
[artist_id] bigint NOT NULL,
CONSTRAINT [PK_app1_album] PRIMARY KEY CLUSTERED ([id] ASC),

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
);
-- Create index "app1_album_artist_id_aed0987a" to table: "app1_album"
CREATE NONCLUSTERED INDEX [app1_album_artist_id_aed0987a] ON [app1_album] ([artist_id] ASC);
-- Create "app2_user" table
CREATE TABLE [app2_user] (
[id] bigint IDENTITY (1, 1) NOT NULL,
[first_name] nvarchar(50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[last_name] nvarchar(50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[roll] nvarchar(100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
CONSTRAINT [PK_app2_user] PRIMARY KEY CLUSTERED ([id] ASC)
);
-- Create "app2_blog" table
CREATE TABLE [app2_blog] (
[id] bigint IDENTITY (1, 1) NOT NULL,
[name] nvarchar(100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[created_at] date NOT NULL,
[num_stars] int NOT NULL,
[author_id] bigint NOT NULL,
CONSTRAINT [PK_app2_blog] PRIMARY KEY CLUSTERED ([id] ASC),

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
);
-- Create index "app2_blog_author_id_1675e606" to table: "app2_blog"
CREATE NONCLUSTERED INDEX [app2_blog_author_id_1675e606] ON [app2_blog] ([author_id] ASC);
2 changes: 2 additions & 0 deletions tests/migrations/mssql/atlas.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
h1:sRFOptG6Ur6mPNfVzxYWfimbzsOprjUnLDRHQgmlr+4=
20240125082659.sql h1:pSoj2u057jC6SZu95JIzvtbGrJzFuY7AW8Ym2ErCnag=

0 comments on commit 7b3c3b9

Please sign in to comment.