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

Add mysql decode support for negative ints #37

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
32 changes: 32 additions & 0 deletions .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# This workflow will install Python dependencies, run tests and lint with a single version of Python
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python

name: Python application

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

permissions:
contents: read

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Set up Python 3.10
uses: actions/setup-python@v3
with:
python-version: "3.10"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements-test.txt
- name: Test with pytest
run: |
pytest
34 changes: 34 additions & 0 deletions database_sanitizer/tests/test_dump_mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,26 @@
--- Final line after `INSERT INTO` statement.
"""

MOCK_MYSQLDUMP_OUTPUT_WITH_NEGINT = b"""
--- Fake MySQL database dump

DROP TABLE IF EXISTS `test`;

CREATE TABLE `test` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`created_at` date NOT NULL,
`notes` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT INTO `test` (`id`, `created_at`, `notes`) VALUES \
(-1,'2018-01-01','Test data 1'),\
(2,'2018-01-02','Test data 2'),\
(3,'2018-01-03','Test data 3');

--- Final line after `INSERT INTO` statement.
"""

MOCK_MYSQLDUMP_OUTPUT_WITH_U2028 = b"""
--- Fake MySQL database dump

Expand Down Expand Up @@ -87,6 +107,20 @@ def test_sanitize_from_stream():
(3,'2018-01-03','Sanitized');\
""" in dump_output_lines

def test_sanitize_with_negint_from_stream():
stream = io.BytesIO(MOCK_MYSQLDUMP_OUTPUT_WITH_NEGINT)
config = Configuration()
config.sanitizers["test.notes"] = lambda value: "Sanitized"
dump_output_lines = list(sanitize_from_stream(stream, config))

assert "--- Fake MySQL database dump" in dump_output_lines
assert "--- Final line after `INSERT INTO` statement." in dump_output_lines
assert """INSERT INTO `test` (`id`, `created_at`, `notes`) VALUES \
(-1,'2018-01-01','Sanitized'),\
(2,'2018-01-02','Sanitized'),\
(3,'2018-01-03','Sanitized');\
""" in dump_output_lines

def test_sanitize_with_u2028_from_stream():
stream = io.BytesIO(MOCK_MYSQLDUMP_OUTPUT_WITH_U2028)
config = Configuration()
Expand Down
2 changes: 1 addition & 1 deletion database_sanitizer/utils/mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def get_mysqldump_args_and_env_from_url(url):
MYSQL_NULL_PATTERN = re.compile(r"^NULL$", re.IGNORECASE)
MYSQL_BOOLEAN_PATTERN = re.compile(r"^(TRUE|FALSE)$", re.IGNORECASE)
MYSQL_FLOAT_PATTERN = re.compile(r"^[+-]?\d*\.\d+([eE][+-]?\d+)?$")
MYSQL_INT_PATTERN = re.compile(r"^\d+$")
MYSQL_INT_PATTERN = re.compile(r"^[+-]?\d+$")
MYSQL_STRING_PATTERN = re.compile(r"'(?:[^']|''|\\')*(?<![\\])'")


Expand Down