Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add mssql support #21

Merged
merged 20 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 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 @@ -24,16 +25,35 @@ jobs:
MARIADB_DATABASE: test_db
MARIADB_USER: test_user
MARIADB_PASSWORD: password
MARIADB_ALLOW_EMPTY_ROOT_PASSWORD : 'yes'
MARIADB_ALLOW_EMPTY_ROOT_PASSWORD: 'yes'
MARIADB_PORT: 33062
MARIADB_MYSQL_LOCALHOST_USER: true
POSTGRES_HOST: 127.0.0.1
POSTGRES_DB: test_db
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: 1433
services:
# Label used to access the service container
mssql:
image: mcr.microsoft.com/mssql/server:2019-latest
env:
MSSQL_SA_PASSWORD: ${{env.MSSQL_PASSWORD}}
ACCEPT_EULA: Y
ports:
- 1433:1433
# todo: dont hardcode password
# options: >-
# --health-cmd "/opt/mssql-tools/bin/sqlcmd -U sa -P yourStrong(!)Password -Q 'SELECT 1' -b -o /dev/null"
# --health-interval 60s
# --health-timeout 30s
# --health-start-period 20s
# --health-retries 3
mysql:
image: mysql
env:
Expand All @@ -48,11 +68,11 @@ jobs:
mariadb:
image: mariadb
env:
MARIADB_ALLOW_EMPTY_ROOT_PASSWORD : ${{env.MARIADB_ALLOW_EMPTY_ROOT_PASSWORD}}
MARIADB_ALLOW_EMPTY_ROOT_PASSWORD: ${{env.MARIADB_ALLOW_EMPTY_ROOT_PASSWORD}}
MARIADB_USER: ${{env.MARIADB_USER}}
MARIADB_PASSWORD: ${{env.MARIADB_PASSWORD}}
MARIADB_DATABASE: ${{env.MARIADB_DATABASE}}
MARIADB_MYSQL_LOCALHOST_USER: ${{env.MARIADB_MYSQL_LOCALHOST_USER}}
MARIADB_MYSQL_LOCALHOST_USER: ${{env.MARIADB_MYSQL_LOCALHOST_USER}}
ports:
- 33062:3306
options: --health-cmd="healthcheck.sh --su-mysql --connect --innodb_initialized" --health-interval=10s --health-timeout=5s --health-retries=3
Expand Down Expand Up @@ -110,7 +130,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
8 changes: 8 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,11 @@ RUN apt-get -y install net-tools mysql-client default-libmysqlclient-dev

# MariaDB
RUN apt-get -y install libmariadb-dev libssl-dev

# MSSQL (beta)
RUN curl -fsSL https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor -o /usr/share/keyrings/microsoft-prod.gpg
RUN curl https://packages.microsoft.com/config/debian/12/prod.list | tee /etc/apt/sources.list.d/mssql-release.list

RUN apt-get update
RUN ACCEPT_EULA=Y apt-get -y install msodbcsql17 msodbcsql18
RUN apt-get install -y unixodbc-dev
73 changes: 46 additions & 27 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 ([see](#mssql-beta)) | |
|----------------------------|----|
| `>=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,16 @@ django-tests:

## Feedback

If you have any problems with or questions about this image, please open an issue on GitHub.

## MSSQL Beta

⚠️ The MSSQL support is currently in beta. If you have any problems with or questions about this image, please open an
issue on GitHub.

MSSQL is currently only supported/tested with the following versions:

- [`^mssql-server-linux:2019-CU23-ubuntu-20.04`](https://hub.docker.com/_/microsoft-mssql-server)
- [`mssql-django===1.3`](https://pypi.org/project/mssql-django/)
- [`Microsoft ODBC Driver 17 for SQL Server` & `Microsoft ODBC Driver 18 for SQL Server`](https://learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver16&tabs=debian18-install%2Calpine17-install%2Cdebian8-install%2Credhat7-13-install%2Crhel7-offline) ('Microsoft ODBC Driver 18 for SQL Server' does currently not work with mssql as ci service with self-signed ssl certs. If you have any solution, feel free to open an Issue or PR)

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_MSSQL]

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
4 changes: 2 additions & 2 deletions tests/tests/base_db_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ class BaseDBRouter:

def __init__(self):
if self.route_app_labels is None:
warnings.warn(f"Router {self.__class__.__name__}: {self.route_app_labels=}")
warnings.warn(f"Router {self.__class__.__name__}: {self.route_app_labels}")
if self.db is None:
warnings.warn(f"Router {self.__class__.__name__}: {self.db=}")
warnings.warn(f"Router {self.__class__.__name__}: {self.db}")

def db_for_read(self, model, **hints):
if model._meta.app_label in self.route_app_labels:
Expand Down
11 changes: 11 additions & 0 deletions tests/tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"app_mysql",
"app_mariadb",
"app_postgres",
"app_mssql",
]

MIDDLEWARE = [
Expand Down Expand Up @@ -112,11 +113,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