Skip to content

Commit

Permalink
issue #124 - fix mysql 5.7 support
Browse files Browse the repository at this point in the history
  • Loading branch information
pounard committed Apr 25, 2024
1 parent b9b309e commit a40f9ba
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 36 deletions.
1 change: 1 addition & 0 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ jobs:
php-version:
- "8.1"
mysql-version:
- "5.7"
- "8.0"
- "8.2"
- "8.3"
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"require": {
"php": ">=8.1",
"doctrine/doctrine-bundle": "^2.10.0",
"makinacorpus/query-builder": "^1.5.5",
"makinacorpus/query-builder": "^1.6.1",
"symfony/config": "^6.0|^7.0",
"symfony/console": "^6.0|^7.0",
"symfony/dependency-injection": "^6.0|^7.0",
Expand Down
8 changes: 2 additions & 6 deletions dev.sh
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,7 @@ do_test_mariadb11() {
}

do_test_mysql() {
# @todo Temporary deactivated MySQL 5.7 due to a bug.
# https://github.com/makinacorpus/DbToolsBundle/issues/124
# do_test_mysql57
do_test_mysql57 "$@"
do_test_mysql80 "$@"
do_test_mysql83 "$@"
do_test_mariadb11 "$@"
Expand Down Expand Up @@ -256,9 +254,7 @@ do_test_sqlite() {
do_test_all() {
do_composer_update

# @todo Temporary deactivated MySQL 5.7 due to a bug.
# https://github.com/makinacorpus/DbToolsBundle/issues/124
# do_test_mysql57
do_test_mysql57 "$@"
do_test_mysql80 "$@"
do_test_mysql83 "$@"
do_test_mariadb11 "$@"
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ services:
restart: 'no'
environment:
MYSQL_DATABASE: test_db
MYSQL_ROOT_USER: root
MYSQL_ROOT_PASSWORD: password
ports:
- 9501:3306
Expand Down
2 changes: 1 addition & 1 deletion docs/content/getting-started/database-vendors.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Fully supported database vendors are:
- PostgreSQL 10 and above
<br><small>(previous versions from 9.5 are untested but should work)</small>
- MariaDB 10.11 and above
- MySQL 8.0 and above
- MySQL 5.7, 8.0 and above

Partially supported database vendors are:

Expand Down
2 changes: 1 addition & 1 deletion docs/content/getting-started/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Currently supported database vendors:
- PostgreSQL 10 and above
<br><small>(previous versions from 9.5 are untested but should work)</small>
- MariaDB 10.11 and above
- MySQL 8.0 and above
- MySQL 5.7, 8.0 and above
- SQLite 3.0 and above
- SQL Server 2019 and above
<br><small>(previous versions from 2015 are untested but should work)</small>
Expand Down
54 changes: 37 additions & 17 deletions src/Anonymization/Anonymizator.php
Original file line number Diff line number Diff line change
Expand Up @@ -630,23 +630,43 @@ protected function addAnonymizerIdColumnMySql(string $table): void
);

try {
$this->databaseSession->executeStatement(
<<<SQL
CREATE FUNCTION IF NOT EXISTS ?::id() RETURNS BIGINT
DETERMINISTIC
BEGIN
SELECT `value` + 1 INTO @value FROM ?::table LIMIT 1;
UPDATE ?::table SET value = @value;
RETURN @value;
END;
;;
SQL,
[
$functionName,
$sequenceTableName,
$sequenceTableName,
],
);
if ($this->databaseSession->vendorVersionIs('8.0', '<')) {
$this->databaseSession->executeStatement(
<<<SQL
CREATE FUNCTION ?::id() RETURNS INTEGER
DETERMINISTIC
BEGIN
SELECT `value` + 1 INTO @value FROM ?::table LIMIT 1;
UPDATE ?::table SET value = @value;
RETURN @value;
END;
;;
SQL,
[
$functionName,
$sequenceTableName,
$sequenceTableName,
],
);
} else {
$this->databaseSession->executeStatement(
<<<SQL
CREATE FUNCTION IF NOT EXISTS ?::id() RETURNS BIGINT
DETERMINISTIC
BEGIN
SELECT `value` + 1 INTO @value FROM ?::table LIMIT 1;
UPDATE ?::table SET value = @value;
RETURN @value;
END;
;;
SQL,
[
$functionName,
$sequenceTableName,
$sequenceTableName,
],
);
}

$this->databaseSession->executeStatement(
<<<SQL
Expand Down
25 changes: 20 additions & 5 deletions src/Anonymization/Anonymizer/AbstractEnumAnonymizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace MakinaCorpus\DbToolsBundle\Anonymization\Anonymizer;

use MakinaCorpus\QueryBuilder\Vendor;
use MakinaCorpus\QueryBuilder\Query\Select;
use MakinaCorpus\QueryBuilder\Query\Update;

Expand Down Expand Up @@ -50,11 +51,25 @@ public function anonymize(Update $update): void
$sampleCount = $this->countTable($this->sampleTableName);

$joinAlias = $this->sampleTableName . '_' . $this->columnName;
$join = (new Select($this->sampleTableName))
->column('value')
->columnRaw('ROW_NUMBER() OVER (ORDER BY ?)', 'rownum', [$expr->random()])
->range($targetCount) // Avoid duplicate rows.
;

if ($this->databaseSession->vendorIs(Vendor::MYSQL, '8.0', '<')) {
$inner = (new Select($this->sampleTableName))
->column('value')
->orderBy($expr->random())
->range($targetCount) // Avoid duplicate rows.
;
$join = (new Select($inner))
->from($expr->raw('(SELECT @rownum := 0)'))
->column('value')
->columnRaw('@rownum := @rownum + 1', 'rownum')
;
} else {
$join = (new Select($this->sampleTableName))
->column('value')
->columnRaw('ROW_NUMBER() OVER (ORDER BY ?)', 'rownum', [$expr->random()])
->range($targetCount) // Avoid duplicate rows.
;
}

$update->leftJoin(
$join,
Expand Down
25 changes: 20 additions & 5 deletions src/Anonymization/Anonymizer/AbstractMultipleColumnAnonymizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use MakinaCorpus\QueryBuilder\Query\Select;
use MakinaCorpus\QueryBuilder\Query\Update;
use MakinaCorpus\QueryBuilder\Vendor;

/**
* Class of anonymizers that work on a Table target, and allow updating
Expand Down Expand Up @@ -95,11 +96,25 @@ public function anonymize(Update $update): void
$sampleCount = $this->countTable($this->sampleTableName);

$joinAlias = $this->sampleTableName . '_' . $this->columnName;
$join = (new Select($this->sampleTableName))
->columns($columnOptions)
->columnRaw('ROW_NUMBER() OVER (ORDER BY ?)', 'rownum', [$expr->random()])
->range($targetCount) // Avoid duplicate rows.
;

if ($this->databaseSession->vendorIs(Vendor::MYSQL, '8.0', '<')) {
$inner = (new Select($this->sampleTableName))
->columns($columnOptions)
->orderBy($expr->random())
->range($targetCount) // Avoid duplicate rows.
;
$join = (new Select($inner))
->from($expr->raw('(SELECT @rownum := 0)'))
->columns($columnOptions)
->columnRaw('@rownum := @rownum + 1', 'rownum')
;
} else {
$join = (new Select($this->sampleTableName))
->columns($columnOptions)
->columnRaw('ROW_NUMBER() OVER (ORDER BY ?)', 'rownum', [$expr->random()])
->range($targetCount) // Avoid duplicate rows.
;
}

$update->leftJoin(
$join,
Expand Down

0 comments on commit a40f9ba

Please sign in to comment.