Skip to content

Commit

Permalink
Merge pull request #7 from druidfi/settings-class
Browse files Browse the repository at this point in the history
DumpSettings class
  • Loading branch information
back-2-95 authored Aug 23, 2022
2 parents bd3d8c4 + 540a34f commit de3b364
Show file tree
Hide file tree
Showing 15 changed files with 652 additions and 390 deletions.
19 changes: 0 additions & 19 deletions .github/renovate.json

This file was deleted.

39 changes: 28 additions & 11 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,34 @@ jobs:

tests:

runs-on: ubuntu-latest
runs-on: ${{ matrix.os }}

strategy:
fail-fast: false
matrix:
#mysql-versions: ['5.7', '8.0']
mysql-versions: ['5.7']
php-versions: ['7.4', '8.0', '8.1']
include:
- os: ubuntu-18.04
mysql-version: '5.7'
php-version: '7.4'
- os: ubuntu-18.04
mysql-version: '5.7'
php-version: '8.0'
- os: ubuntu-18.04
mysql-version: '5.7'
php-version: '8.1'
- os: ubuntu-latest
mysql-version: '8.0'
php-version: '7.4'
- os: ubuntu-latest
mysql-version: '8.0'
php-version: '8.0'
- os: ubuntu-latest
mysql-version: '8.0'
php-version: '8.1'

services:
db:
image: druidfi/mysql:${{ matrix.mysql-versions }}-drupal
image: druidfi/mysql:${{ matrix.mysql-version }}-drupal
ports:
- 3306:3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
Expand All @@ -37,7 +54,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
php-version: ${{ matrix.php-version }}

- name: Validate composer.json
run: composer validate
Expand All @@ -48,8 +65,8 @@ jobs:
- name: Run PHPunit tests
run: vendor/bin/phpunit

# - name: Create user and databases for testing
# run: ./tests/scripts/create_users.sh 127.0.0.1
#
# - name: Run test script
# run: ./tests/scripts/test.sh 127.0.0.1
- name: Create user and databases for testing
run: cd tests/scripts && ./create_users.sh 127.0.0.1

- name: Run test script
run: cd tests/scripts && ./test.sh 127.0.0.1
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@
**/*.checksum
/composer.lock
/composer.phar
/tests/scripts/output/*.checksum
/tests/scripts/output/*.sql
/vendor/
/.phpunit.result.cache
21 changes: 16 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
FROM php:8.1-alpine
ARG PHP_SHORT_VERSION

RUN apk --update add --no-cache \
bash mysql-client \
&& rm -rf /var/cache/apk/*
FROM druidfi/php:7.4 as php-74

RUN docker-php-ext-install mysqli pdo pdo_mysql
RUN sudo apk --update -X https://dl-cdn.alpinelinux.org/alpine/edge/testing --no-cache add php7-pdo php7-pdo_mysql

FROM druidfi/php:8.0 as php-80

RUN sudo apk --update --no-cache add php8-pdo php8-pdo_mysql

FROM druidfi/php:8.1 as php-81

RUN sudo apk --update --no-cache add php81-pdo php81-pdo_mysql

FROM php-${PHP_SHORT_VERSION}

RUN sudo apk --update add --no-cache bash mysql-client \
&& sudo rm -rf /var/cache/apk/*

WORKDIR /app

Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,11 @@ Local setup for tests:

```
docker-compose up -d --build
docker-compose exec php /app/tests/scripts/create_users.sh
docker-compose exec -w /app/tests/scripts php ./test.sh
docker-compose exec php74 /app/tests/scripts/create_users.sh
docker-compose exec php74 /app/tests/scripts/create_users.sh db2
docker-compose exec -w /app/tests/scripts php74 ./test.sh
docker-compose exec -w /app/tests/scripts php80 ./test.sh
docker-compose exec -w /app/tests/scripts php81 ./test.sh
```

## Bugs (from mysqldump, not from mysqldump-php)
Expand Down
45 changes: 40 additions & 5 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,54 @@
version: '3.7'

services:

db:
container_name: "mysqldump-php-db"
container_name: "mysqldump-php-mysql-57"
image: druidfi/mysql:5.7-drupal
ports:
- 3306

db2:
container_name: "mysqldump-php-db2"
container_name: "mysqldump-php-mysql-80"
image: druidfi/mysql:8.0-drupal
ports:
- 3306

php74:
container_name: "mysqldump-php-74"
image: mysqldump-php-tester:php-7.4
build:
context: .
args:
PHP_SHORT_VERSION: "74"
volumes:
- .:/app
depends_on:
- db
- db2

php80:
container_name: "mysqldump-php-80"
image: mysqldump-php-tester:php-8.0
build:
context: .
args:
PHP_SHORT_VERSION: "80"
volumes:
- .:/app
depends_on:
- db
- db2

php:
container_name: "mysqldump-php"
image: tester:latest
php81:
container_name: "mysqldump-php-81"
image: mysqldump-php-tester:php-8.1
build:
context: .
args:
PHP_SHORT_VERSION: "81"
volumes:
- .:/app
depends_on:
- db
- db2
165 changes: 165 additions & 0 deletions src/DumpSettings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
<?php

namespace Druidfi\Mysqldump;

use Druidfi\Mysqldump\Compress\CompressManagerFactory;
use Exception;

class DumpSettings
{
// List of available connection strings.
const UTF8 = 'utf8';
const UTF8MB4 = 'utf8mb4';

private static array $defaults = [
'include-tables' => [],
'exclude-tables' => [],
'include-views' => [],
'compress' => 'None',
'init_commands' => [],
'no-data' => [],
'if-not-exists' => false,
'reset-auto-increment' => false,
'add-drop-database' => false,
'add-drop-table' => false,
'add-drop-trigger' => true,
'add-locks' => true,
'complete-insert' => false,
'databases' => false,
'default-character-set' => self::UTF8,
'disable-keys' => true,
'extended-insert' => true,
'events' => false,
'hex-blob' => true, /* faster than escaped content */
'insert-ignore' => false,
'net_buffer_length' => 1000000,
'no-autocommit' => true,
'no-create-info' => false,
'lock-tables' => true,
'routines' => false,
'single-transaction' => true,
'skip-triggers' => false,
'skip-tz-utc' => false,
'skip-comments' => false,
'skip-dump-date' => false,
'skip-definer' => false,
'where' => '',
/* deprecated */
'disable-foreign-keys-check' => true
];
private array $settings;

/**
* @throws Exception
*/
public function __construct(array $settings)
{
$this->settings = array_replace_recursive(self::$defaults, $settings);

$this->settings['init_commands'][] = "SET NAMES " . $this->get('default-character-set');

if (false === $this->settings['skip-tz-utc']) {
$this->settings['init_commands'][] = "SET TIME_ZONE='+00:00'";
}

$diff = array_diff(array_keys($this->settings), array_keys(self::$defaults));

if (count($diff) > 0) {
throw new Exception("Unexpected value in dumpSettings: (" . implode(",", $diff) . ")");
}

if (!is_array($this->settings['include-tables']) || !is_array($this->settings['exclude-tables'])) {
throw new Exception('Include-tables and exclude-tables should be arrays');
}

// If no include-views is passed in, dump the same views as tables, mimic mysqldump behaviour.
if (!isset($settings['include-views'])) {
$this->settings['include-views'] = $this->settings['include-tables'];
}
}

public function getCompressMethod(): string
{
return $this->settings['compress'] ?? CompressManagerFactory::NONE;
}

public function getDefaultCharacterSet(): string
{
return $this->settings['default-character-set'];
}

public function getDefaults(): array
{
return self::$defaults;
}

public function getExcludedTables(): array
{
return $this->settings['exclude-tables'] ?? [];
}

public function getIncludedTables(): array
{
return $this->settings['include-tables'] ?? [];
}

public function getIncludedViews(): array
{
return $this->settings['include-views'] ?? [];
}

public function getInitCommands(): array
{
return $this->settings['init_commands'] ?? [];
}

public function getNetBufferLength(): int
{
return $this->settings['net_buffer_length'];
}

public function getNoData(): array
{
return $this->settings['no-data'] ?? [];
}

public function isEnabled(string $option): bool
{
return isset($this->settings[$option]) && $this->settings[$option] === true;
}

public function setCompleteInsert(bool $value = true)
{
$this->settings['complete-insert'] = $value;
}

public function skipComments(): bool
{
return $this->isEnabled('skip-comments');
}

public function skipDefiner(): bool
{
return $this->isEnabled('skip-definer');
}

public function skipDumpDate(): bool
{
return $this->isEnabled('skip-dump-date');
}

public function skipTriggers(): bool
{
return $this->isEnabled('skip-triggers');
}

public function skipTzUtc(): bool
{
return $this->isEnabled('skip-tz-utc');
}

public function get(string $option): string
{
return (string) $this->settings[$option];
}
}
Loading

0 comments on commit de3b364

Please sign in to comment.