Skip to content

Commit

Permalink
feat: add mssql support (beta)
Browse files Browse the repository at this point in the history
  • Loading branch information
OrbisK committed Dec 7, 2023
1 parent 2dca634 commit 5890d37
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 30 deletions.
18 changes: 16 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ on:
push:
branches:
- 'main'
- 'feature/*'

jobs:
build:
Expand All @@ -32,8 +33,21 @@ jobs:
POSTGRES_USER: test_user
POSTGRES_PASSWORD: password
POSTGRES_PORT: 5432
MSSQL_HOST: 127.0.0.1
MSSQL_DATABASE: test_db
MSSQL_USER: sa
MSSQL_PASSWORD: yourStrong(!)Password
MSSQL_PORT: 1443
services:
# Label used to access the service container
mssql:
image: mcr.microsoft.com/mssql/server:2019-latest
env:
MSSQL_SA_PASSWORD: ${{secrets.MSSQL_PASSWORD}}
ACCEPT_EULA: Y
ports:
- 1433:1433
options: --health-cmd="sqlcmd -U sa -P ${{secrets.MSSQL_PASSWORD}} -Q 'SELECT 1'" --health-interval=10s --health-timeout=5s --health-retries=3
mysql:
image: mysql
env:
Expand Down Expand Up @@ -110,7 +124,7 @@ jobs:
- name: Test Databases
if: steps.changes.outputs.src == 'true' || steps.changes.outputs.docker == 'true' || github.event.inputs.version == 'all' || github.event.inputs.version == matrix.version
run: |
docker run --network host -v /"$(pwd)/tests":/app -w /app -e MYSQL_HOST=$MYSQL_HOST -e MYSQL_DATABASE=$MYSQL_DATABASE -e MYSQL_USER=$MYSQL_USER -e MYSQL_PORT=$MYSQL_PORT -e MYSQL_PASSWORD=$MYSQL_PASSWORD -e MARIADB_HOST=$MARIADB_HOST -e MARIADB_DATABASE=$MARIADB_DATABASE -e MARIADB_USER=$MARIADB_USER -e MARIADB_PORT=$MARIADB_PORT -e MARIADB_PASSWORD=$MARIADB_PASSWORD -e POSTGRES_HOST=$POSTGRES_HOST -e POSTGRES_DB=$POSTGRES_DB -e POSTGRES_USER=$POSTGRES_USER -e POSTGRES_PORT=$POSTGRES_PORT -e POSTGRES_PASSWORD=$POSTGRES_PASSWORD orbisk/django-test:${{env.PYTHON_VERSION}} sh -c "cd /app && pip install -r requirements.txt && python manage.py test"
docker run --network host -v /"$(pwd)/tests":/app -w /app -e MYSQL_HOST=$MYSQL_HOST -e MYSQL_DATABASE=$MYSQL_DATABASE -e MYSQL_USER=$MYSQL_USER -e MYSQL_PORT=$MYSQL_PORT -e MYSQL_PASSWORD=$MYSQL_PASSWORD -e MARIADB_HOST=$MARIADB_HOST -e MARIADB_DATABASE=$MARIADB_DATABASE -e MARIADB_USER=$MARIADB_USER -e MARIADB_PORT=$MARIADB_PORT -e MARIADB_PASSWORD=$MARIADB_PASSWORD -e POSTGRES_HOST=$POSTGRES_HOST -e POSTGRES_DB=$POSTGRES_DB -e POSTGRES_USER=$POSTGRES_USER -e POSTGRES_PORT=$POSTGRES_PORT -e POSTGRES_PASSWORD=$POSTGRES_PASSWORD -e MSSQL_HOST -e MSSQL_PORT -e MSSQL_DATABASE -e MSSQL_USER -e MSSQL_PASSWORD orbisk/django-test:${{env.PYTHON_VERSION}} sh -c "cd /app && pip install -r requirements.txt && python manage.py test"
- name: Push Image
if: steps.changes.outputs.src == 'true' || steps.changes.outputs.docker == 'true' || github.event.inputs.version == 'all' || github.event.inputs.version == matrix.version
if: (steps.changes.outputs.src == 'true' || steps.changes.outputs.docker == 'true' || github.event.inputs.version == 'all' || github.event.inputs.version == matrix.version) && github.ref == 'refs/heads/main'
run: docker push -a orbisk/django-test
63 changes: 35 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,46 +31,55 @@ started.

- 🟢 Supported/Tested
- 🟡 Unknown/Untested
- 🟠 Beta/Experimental (please share your experiences)
- 🔴 Currently not Supported (open an Issue or Pull request if needed)

| Python version | |
|----------------|-----|
| `^2.x` | 🔴 |
| `^3.7.16` | 🟢 |
| `^3.8.16` | 🟢 |
| `^3.9.16` | 🟢 |
| `^3.10.10` | 🟢 |
| `^3.11.2` | 🟢 |

| MySQL*️⃣ | |
|----------|-----|
| `^8.0.0` | 🟢 |
| `<=5.7` | 🟡 |

| MariaDB*️⃣ | |
|------------|-----|
| `^11.0.0` | 🟡 |
| `^10.7.8` | 🟢 |
| `<10.7.8` | 🟡 |

| Postgres | |
|----------|-----|
| `^15.2` | 🟢 |
| `<15.2` | 🟡 |
| Python version | |
|--------------|----|
| `^3.12` | 🔴 |
| `^3.11.2` | 🟢 |
| `^3.10.10` | 🟢 |
| `^3.9.16` | 🟢 |
| `^3.8.16` | 🟢 |
| `^3.7.16` | 🟢 |
| `^2.x` | 🔴 |

| MySQL*️⃣ | |
|----------|----|
| `^8.0.0` | 🟢 |
| `<=5.7` | 🟡 |

| MariaDB*️⃣ | |
|------------|----|
| `^11.0.0` | 🟡 |
| `^10.7.8` | 🟢 |
| `<10.7.8` | 🟡 |

| Postgres | |
|----------|----|
| `^15.2` | 🟢 |
| `<15.2` | 🟡 |

| MSSQL | |
|---------------------------|----|
| `>=2022` | 🟠 |
| `^2019-CU23-ubuntu-20.04` | 🟠 |
| `<=2017` | 🟠 |

*️⃣ Additional step required see: [Error creating the test database](#error-creating-the-test-database)

## Common issues

### Error creating the test database

`Got an error creating the test database: (1044, "1044 (42000): Access denied for user '<test_user>'@'%' to database 'test_<my_db>'", '42000')`

The MYSQL user is only granted permissions for the MYSQL_DB, which means that Django is unable to create a test database.
The MYSQL user is only granted permissions for the MYSQL_DB, which means that Django is unable to create a test
database.
To use MySQL and MariaDB with a non-root user, you need to grant privileges to your test user.

`echo "GRANT ALL on *.* to '$MYSQL_USER';"| mysql -u root --password="$MYSQL_ROOT_PASSWORD" -h <host> -p <port>`


## CI Examples

### GitLab CI
Expand Down Expand Up @@ -98,6 +107,4 @@ django-tests:

## Feedback



If you have any problems with or questions about this image, please open an issue on GitHub.
Empty file added tests/app_mssql/__init__.py
Empty file.
21 changes: 21 additions & 0 deletions tests/app_mssql/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 4.1.7 on 2023-03-14 13:46

from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='TestModel',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('value', models.IntegerField()),
],
),
]
Empty file.
8 changes: 8 additions & 0 deletions tests/app_mssql/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.db import models


# Create your models here.


class TestModel(models.Model):
value = models.IntegerField()
8 changes: 8 additions & 0 deletions tests/app_mssql/router.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.conf import settings

from tests.base_db_router import BaseDBRouter


class DBRouter(BaseDBRouter):
route_app_labels = {"app_mssql"}
db = settings.DB_MSSQL
9 changes: 9 additions & 0 deletions tests/app_mssql/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.conf import settings
from django.test import TestCase


class Test(TestCase):
databases = ['default', settings.DB_POSTGRES]

def test(self):
self.assertTrue(True)
1 change: 1 addition & 0 deletions tests/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ coverage
mysqlclient
django-environ
psycopg2
mssql-django
10 changes: 10 additions & 0 deletions tests/tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,21 @@
"USER": env.str("POSTGRES_USER"),
"PASSWORD": env.str("POSTGRES_PASSWORD"),
},
DB_MSSQL: {
"ENGINE": "mssql",
"HOST": env.str("MSSQL_HOST"),
"PORT": env.int("MSSQL_PORT"),
"NAME": env.str("MSSQL_DATABASE"),
"USER": env.str("MSSQL_USER"),
"PASSWORD": env.str("MSSQL_PASSWORD"),
}
}

DATABASE_ROUTERS = [
'app_mysql.router.DBRouter',
'app_mariadb.router.DBRouter',
'app_postgres.router.DBRouter',
'app_mssql.router.DBRouter',
]

# Password validation
Expand Down

0 comments on commit 5890d37

Please sign in to comment.