Skip to content

Commit 49d48ae

Browse files
authored
Merge pull request #25 from LordSimal/mariadb
add MariaDB support + custom port support
2 parents 5f40fec + 66ebdc1 commit 49d48ae

7 files changed

Lines changed: 59 additions & 23 deletions

File tree

.github/workflows/ci.yml

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,21 @@ jobs:
2020
fail-fast: false
2121
matrix:
2222
php-version: [ '8.1', '8.2', '8.3', '8.4' ]
23-
db-type: [ sqlite, mysql, pgsql ]
23+
db-type: [ sqlite, mysql, mariadb, pgsql ]
2424

2525
services:
26+
mariadb:
27+
image: mariadb:11
28+
env:
29+
MARIADB_ROOT_PASSWORD: root
30+
MARIADB_DATABASE: cakephp
31+
ports:
32+
- 3307:3306
33+
options: >-
34+
--health-cmd "mariadb-admin ping -h localhost -proot"
35+
--health-interval 10s
36+
--health-timeout 5s
37+
--health-retries 5
2638
postgres:
2739
image: postgres:14
2840
env:
@@ -44,6 +56,10 @@ jobs:
4456
sudo service mysql start
4557
mysql -h 127.0.0.1 -u root -proot -e 'CREATE DATABASE cakephp;'
4658
59+
- name: Install MariaDB dump tools
60+
if: matrix.db-type == 'mariadb'
61+
run: sudo apt-get install mariadb-client
62+
4763
- name: Setup Postgres
4864
if: matrix.db-type == 'pgsql'
4965
run: |
@@ -57,7 +73,7 @@ jobs:
5773
uses: shivammathur/setup-php@v2
5874
with:
5975
php-version: ${{ matrix.php-version }}
60-
extensions: mbstring, intl, pdo_${{ matrix.db-type }}
76+
extensions: mbstring, intl, ${{ matrix.db-type == 'sqlite' && 'pdo_sqlite' || matrix.db-type == 'pgsql' && 'pdo_pgsql' || 'pdo_mysql' }}
6177
ini-values: zend.assertions=1
6278
coverage: pcov
6379

@@ -75,6 +91,9 @@ jobs:
7591
if [[ ${{ matrix.db-type }} == 'mysql' ]]; then
7692
export DB_URL=mysql://root:root@127.0.0.1/cakephp
7793
fi
94+
if [[ ${{ matrix.db-type }} == 'mariadb' ]]; then
95+
export DB_URL=mysql://root:root@127.0.0.1:3307/cakephp
96+
fi
7897
if [[ ${{ matrix.db-type }} == 'pgsql' ]]; then
7998
export DB_URL=postgres://postgres:postgres@127.0.0.1/postgres
8099
fi
@@ -107,7 +126,7 @@ jobs:
107126
uses: ramsey/composer-install@v3
108127

109128
- name: Install PHP tools with phive.
110-
run: "phive install --trust-gpg-keys 'CF1A108D0E7AE720,51C67305FFC2E5C0,12CE0F1D262429A5'"
129+
run: "phive install --trust-gpg-keys '51C67305FFC2E5C0,12CE0F1D262429A5,99BF4D9A33D65E1E'"
111130

112131
- name: Run phpcs
113132
if: always()
@@ -120,4 +139,3 @@ jobs:
120139
- name: Run phpstan
121140
if: always()
122141
run: tools/phpstan analyse --error-format=github
123-

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ public function bootstrap(): void
4747

4848
For each DBMS you need to have its respective dump tool installed.
4949

50-
- MySQL/MariaDB => `mysqldump`
50+
- MySQL => `mysqldump`
51+
- MariaDB => `mariadb-dump`
5152
- SQLite => `sqlite3`
5253
- PostgreSQL => `pg_dump`
5354

phpunit.xml.dist

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
<php>
1111
<ini name="memory_limit" value="-1"/>
12-
<ini name="apc.enable_cli" value="1"/>
1312
<env name="FIXTURE_SCHEMA_METADATA" value="./tests/schema.php"/>
1413
<!-- SQLite
1514
<env name="DB_URL" value="sqlite:///:memory:"/>

src/Command/DumpSqlCommand.php

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,15 @@ public function execute(Arguments $args, ConsoleIo $io): int
6767
}
6868

6969
$driver = $connection->getDriver();
70-
switch (get_class($driver)) {
71-
case Mysql::class:
72-
$object = new CakeDumpMySQL($connection->config());
73-
break;
74-
case Sqlite::class:
75-
$object = new CakeDumpSqlite($connection->config());
76-
break;
77-
case Postgres::class:
78-
$object = new CakeDumpPostgres($connection->config());
79-
break;
80-
default:
81-
$message = sprintf('Unknown driver "%s" given.', get_class($driver));
82-
throw new UnknownDriverException($message);
70+
if ($driver instanceof Mysql) {
71+
$object = new CakeDumpMySQL($connection->config(), $driver);
72+
} elseif ($driver instanceof Sqlite) {
73+
$object = new CakeDumpSqlite($connection->config());
74+
} elseif ($driver instanceof Postgres) {
75+
$object = new CakeDumpPostgres($connection->config());
76+
} else {
77+
$message = sprintf('Unknown driver "%s" given.', get_class($driver));
78+
throw new UnknownDriverException($message);
8379
}
8480

8581
$object->setIo($io);

src/Sql/MySQL.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,27 @@
33

44
namespace CakeDumpSql\Sql;
55

6+
use Cake\Database\Driver\Mysql as MysqlDriver;
67
use CakeDumpSql\Error\BinaryNotFoundException;
78
use Symfony\Component\Process\Process;
89

910
class MySQL extends SqlBase
1011
{
1112
protected string $command = 'mysqldump';
1213

14+
/**
15+
* @param array<string, mixed> $config The config array from the connection object
16+
* @param \Cake\Database\Driver\Mysql $driver The current mysql driver instance
17+
*/
18+
public function __construct(array $config, protected MysqlDriver $driver)
19+
{
20+
parent::__construct($config);
21+
22+
if ($driver->isMariadb()) {
23+
$this->command = 'mariadb-dump';
24+
}
25+
}
26+
1327
/**
1428
* @return string
1529
* @throws \CakeDumpSql\Error\BinaryNotFoundException
@@ -26,10 +40,17 @@ public function dump(): string
2640
'--password="' . ($this->config['password'] ?? '') . '"',
2741
'--default-character-set=' . ($this->config['encoding'] ?? 'utf8mb4'),
2842
'--host=' . ($this->config['host'] ?? 'localhost'),
43+
'--port=' . ($this->config['port'] ?? 3306),
2944
'--databases',
3045
$this->config['database'],
31-
'--no-create-db',
3246
];
47+
48+
if ($this->driver->isMariadb()) {
49+
$command[] = '--skip-create-options';
50+
} else {
51+
$command[] = '--no-create-db';
52+
}
53+
3354
if ($this->isDataOnly()) {
3455
$command[] = '--no-create-info';
3556
}

src/Sql/PostgreSQL.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public function dump(): string
2828
'PGPASSFILE=' . $passFile,
2929
$this->command,
3030
'--host=' . ($this->config['host'] ?? 'localhost'),
31+
'--port=' . ($this->config['port'] ?? 5432),
3132
'--username=' . ($this->config['username'] ?? ''),
3233
'--dbname="' . ($this->config['database'] ?? '') . '"',
3334
];

tests/TestCase/Command/DumpSqlCommandTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function testCommand(): void
4747
$this->assertOutputContains('INSERT INTO posts VALUES(');
4848
} elseif ($this->isDBType(Mysql::class)) {
4949
$this->assertOutputContains('CREATE TABLE `posts` (');
50-
$this->assertOutputContains('INSERT INTO `posts` VALUES (');
50+
$this->assertOutputContains('INSERT INTO `posts` VALUES');
5151
} elseif ($this->isDBType(Postgres::class)) {
5252
$this->assertOutputContains('CREATE TABLE public.posts');
5353
$this->assertOutputContains('COPY public.posts (id, title, created, modified) FROM stdin;');
@@ -72,7 +72,7 @@ public function testCommandDataOnly(): void
7272
$this->assertOutputContains('INSERT INTO posts VALUES(');
7373
} elseif ($this->isDBType(Mysql::class)) {
7474
$this->assertOutputNotContains('CREATE TABLE `posts` (');
75-
$this->assertOutputContains('INSERT INTO `posts` VALUES (');
75+
$this->assertOutputContains('INSERT INTO `posts` VALUES');
7676
} elseif ($this->isDBType(Postgres::class)) {
7777
$this->assertOutputNotContains('CREATE TABLE public.posts');
7878
$this->assertOutputContains('COPY public.posts (id, title, created, modified) FROM stdin;');
@@ -103,7 +103,7 @@ public function testCommandGzipped(): void
103103
$this->assertStringContainsString('INSERT INTO posts VALUES(', $sql);
104104
} elseif ($this->isDBType(Mysql::class)) {
105105
$this->assertStringContainsString('CREATE TABLE `posts` (', $sql);
106-
$this->assertStringContainsString('INSERT INTO `posts` VALUES (', $sql);
106+
$this->assertStringContainsString('INSERT INTO `posts` VALUES', $sql);
107107
} elseif ($this->isDBType(Postgres::class)) {
108108
$this->assertStringContainsString('CREATE TABLE public.posts', $sql);
109109
$this->assertStringContainsString('COPY public.posts (id, title, created, modified) FROM stdin;', $sql);

0 commit comments

Comments
 (0)