Skip to content

Commit

Permalink
Merge pull request #46 from howyi/add-maria-db
Browse files Browse the repository at this point in the history
add Support MariaDB
  • Loading branch information
howyi authored Jan 15, 2020
2 parents ab1dde1 + d0208a9 commit e9f3a85
Show file tree
Hide file tree
Showing 10 changed files with 165 additions and 51 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"authors": [
{
"name": "Hayashi Takuya",
"email": "qrbys@outlook.com"
"email": "howyi.lq@gmail.com"
}
],
"scripts": {
Expand All @@ -28,7 +28,7 @@
"repositories": [
{
"type": "vcs",
"url": "https://github.com/laminaria/conv-test-suite.git",
"url": "https://github.com/howyi/conv-test-suite.git",
"no-api": true
}
],
Expand Down
61 changes: 50 additions & 11 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,36 @@ services:
build: ./docker
working_dir: /usr/src/app
environment:
DB_HOST: mysql-56:3306,mysql-57:3306,mysql-80:3306
command: ./docker/wait-for-it.sh -t 0 mysql-80:3306 -- vendor/bin/phpunit
DB_HOST: mysql-56:3306,mysql-57:3306,mysql-80:3306,maria-100:3306,maria-101:3306,maria-102:3306,maria-103:3306
command: ./docker/wait-for-it.sh -t 0 maria-103:3306 -- vendor/bin/phpunit
volumes:
- .:/usr/src/app
depends_on:
- mysql-56
- mysql-57
- mysql-80
- maria-103

travis:
build: ./docker
working_dir: /usr/src/app
environment:
TRAVIS:
TRAVIS_JOB_ID:
DB_HOST: mysql-56:3306,mysql-57:3306,mysql-80:3306
command: ./docker/wait-for-it.sh -t 0 mysql-80:3306 -- ./docker/ci.sh
DB_HOST: mysql-56:3306,mysql-57:3306,mysql-80:3306,maria-100:3306,maria-101:3306,maria-102:3306,maria-103:3306
command: ./docker/wait-for-it.sh -t 0 maria-103:3306 -- ./docker/ci.sh
volumes:
- .:/usr/src/app
depends_on:
- mysql-56
- mysql-57
- mysql-80
- maria-103

testdb:
image: mysql:8.0
command: mysqld --default-authentication-plugin=mysql_native_password
security_opt:
- seccomp:unconfined
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
MYSQL_ROOT_PASSWORD: ''
ports:
- 33060:3306

mysql-56:
image: mysql:5.6
Expand All @@ -51,4 +58,36 @@ services:
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
MYSQL_ROOT_PASSWORD: ''
depends_on:
- mysql-57
- mysql-57

maria-100:
image: mariadb:10.0
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
MYSQL_ROOT_PASSWORD: ''
depends_on:
- mysql-80

maria-101:
image: mariadb:10.1
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
MYSQL_ROOT_PASSWORD: ''
depends_on:
- maria-100

maria-102:
image: mariadb:10.2
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
MYSQL_ROOT_PASSWORD: ''
depends_on:
- maria-101

maria-103:
image: mariadb:10.3
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
MYSQL_ROOT_PASSWORD: ''
depends_on:
- maria-102
4 changes: 2 additions & 2 deletions src/DebugCommand/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ abstract class AbstractCommand extends Command
public function getPDO($dbname = null): \PDO
{
if (is_null($dbname)) {
$pdo = new \PDO('mysql:host=127.0.0.1;charset=utf8;', 'root', '');
$pdo = new \PDO('mysql:host=127.0.0.1;port=33060;charset=utf8;', 'root', '');
} else {
$pdo = new \PDO("mysql:host=127.0.0.1;dbname=$dbname;charset=utf8;", 'root', '');
$pdo = new \PDO("mysql:host=127.0.0.1;port=33060;dbname=$dbname;charset=utf8;", 'root', '');
}
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
return $pdo;
Expand Down
2 changes: 1 addition & 1 deletion src/DebugCommand/CheckAlterCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$actualStructure = DatabaseStructureFactory::fromPDO($this->getPDO('conv'), 'conv');
$expectStructure = DatabaseStructureFactory::fromSqlDir(
$this->getPDO(),
'tests/Retort/check_schema',
'tests/schema',
new DropOnlySilentOperator()
);
$alter = MigrationGenerator::generate(
Expand Down
53 changes: 26 additions & 27 deletions src/DebugCommand/SetupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,31 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
// $operator = $this->getOperator($input, $output);
//
// $pdo = $this->getPDO();
// $pdo->exec('DROP DATABASE IF EXISTS conv');
// $pdo->exec('CREATE DATABASE conv');
//
// $databaseStructure = DatabaseStructureFactory::fromDir('tests/Retort/test_schema/008');
// $empty = new DatabaseStructure([]);
// $alter = MigrationGenerator::generate(
// $empty,
// $databaseStructure,
// $operator
// );
//
// $pdo = $this->getPDO('conv');
// foreach ($alter->getMigrationList() as $migration) {
// $operator->output('<fg=green>実行クエリ</>');
// $operator->output($migration->getUp());
// $pdo->exec($migration->getUp());
// }
// CreateQueryReflector::fromPDO(
// $pdo,
// 'conv',
// 'tests/Retort/test_schema_sql/008',
// $operator
// );
// $output->writeln('<fg=cyan>setup success</>');
$operator = $this->getOperator($input, $output);

$pdo = $this->getPDO();
$pdo->exec('DROP DATABASE IF EXISTS conv');
$pdo->exec('CREATE DATABASE conv');

$databaseStructure = DatabaseStructureFactory::fromSqlDir(
$pdo,
'tests/schema',
$operator
);
$empty = new DatabaseStructure([]);
$alter = MigrationGenerator::generate(
$empty,
$databaseStructure,
$operator
);

$pdo = $this->getPDO('conv');
foreach ($alter->getMigrationList() as $migration) {
$operator->output('<fg=green>実行クエリ</>');
$operator->output($migration->getUp());
$pdo->exec($migration->getUp());
}

$output->writeln('<fg=cyan>setup success</>');
}
}
40 changes: 33 additions & 7 deletions src/Driver/DriverAllocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,55 @@

class DriverAllocator
{
const TYPE_MYSQL = 'mysql';
const TYPE_MARIA_DB = 'mariadb';

/**
* @param \PDO $PDO
* @return DriverInterface
*/
public static function fromPDO(\PDO $PDO): DriverInterface
{
$driverName = $PDO->getAttribute(\PDO::ATTR_DRIVER_NAME);
$version = $PDO->getAttribute(\PDO::ATTR_SERVER_VERSION);
preg_match("/^[0-9\.]+/", $version, $match);
$version = $match[0];
$rawVersion = $PDO->getAttribute(\PDO::ATTR_SERVER_VERSION);
preg_match(
"/^[0-9\.]+/",
$rawVersion,
$match
);

$type = $version = null;

if ($match[0] === $rawVersion and strtolower($driverName) === 'mysql') {
$type = self::TYPE_MYSQL;
$version = $match[0];
} elseif (strpos($rawVersion, 'MariaDB') !== false) {
$type = self::TYPE_MARIA_DB;
$split = explode('-', $rawVersion);
$version = $split[1];
}

switch (strtolower($driverName)) {
case 'mysql':
switch ($type) {
case self::TYPE_MYSQL:
switch (true) {
case Semver::satisfies($version, '8.0.*'):
case Semver::satisfies($version, '>= 8.0.0'):
return new MySQL80Driver($PDO);
case Semver::satisfies($version, '5.7.*'):
return new MySQL57Driver($PDO);
case Semver::satisfies($version, '5.6.*'):
return new MySQL56Driver($PDO);
}
case self::TYPE_MARIA_DB:
switch (true) {
case Semver::satisfies($version, '>= 10.2.0'):
return new MariaDB102Driver($PDO);
case Semver::satisfies($version, '10.1.*'):
return new MySQL57Driver($PDO);
case Semver::satisfies($version, '10.0.*'):
return new MySQL56Driver($PDO);
}
}

throw new \RuntimeException('Unsupported driver. conv supported MySQL 5.6.*|5.7.*|8.0.*');
throw new \RuntimeException('Unsupported driver. conv supported MySQL 5.6~ and MariaDB 10.0~');
}
}
17 changes: 17 additions & 0 deletions src/Driver/MariaDB102Driver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Howyi\Conv\Driver;

use Howyi\Conv\Structure\ColumnStructure\MariaDB102ColumnStructure;

class MariaDB102Driver extends MySQL57Driver
{

/**
* {@inheritdoc}
*/
protected function generateColumnStructure(...$values)
{
return new MariaDB102ColumnStructure(...$values);
}
}
12 changes: 11 additions & 1 deletion src/Driver/MySQL57Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Howyi\Conv\Structure\Attribute;
use Howyi\Conv\Structure\ColumnStructure\ColumnStructureInterface;
use Howyi\Conv\Structure\ColumnStructure\MySQL57ColumnStructure;
use Howyi\Conv\Structure\ColumnStructure\MySQLColumnStructureInterface;

class MySQL57Driver extends MySQL56Driver
{
Expand All @@ -27,7 +28,7 @@ protected function createColumnStructure(array $rawColumn): ColumnStructureInter
$collationName = $rawColumn['COLLATION_NAME'];
$generationExpression = empty($rawColumn['GENERATION_EXPRESSION']) ? null : $rawColumn['GENERATION_EXPRESSION'];

return new MySQL57ColumnStructure(
return $this->generateColumnStructure(
$rawColumn['COLUMN_NAME'],
str_replace(' unsigned', '', $rawColumn['COLUMN_TYPE']),
$rawColumn['COLUMN_DEFAULT'],
Expand All @@ -38,4 +39,13 @@ protected function createColumnStructure(array $rawColumn): ColumnStructureInter
[]
);
}

/**
* @param mixed[] $values
* @return MySQLColumnStructureInterface
*/
protected function generateColumnStructure(...$values)
{
return new MySQL57ColumnStructure(...$values);
}
}
17 changes: 17 additions & 0 deletions src/Structure/ColumnStructure/MariaDB102ColumnStructure.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Howyi\Conv\Structure\ColumnStructure;

use Howyi\Conv\Structure\Attribute;
use Howyi\Conv\Util\SchemaKey;

class MariaDB102ColumnStructure extends MySQL57ColumnStructure implements MySQLColumnStructureInterface
{
/**
* @return string
*/
public function getDefault(): string
{
return $this->default;
}
}
6 changes: 6 additions & 0 deletions tests/schema/score.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE `score` (
`user_id` int(11) NOT NULL COMMENT 'User id',
`score` int(11) NOT NULL COMMENT 'score',
`created_date` datetime DEFAULT NULL COMMENT '作成日時',
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='Score table';

0 comments on commit e9f3a85

Please sign in to comment.