From f5125648f162042d845a254cee2422c212f29354 Mon Sep 17 00:00:00 2001 From: Jasdeep Date: Sun, 18 Jun 2017 21:40:42 +0100 Subject: [PATCH 1/6] Improving the CI coverage - Environment detection - Moving towards the ability for having multiple tests - Adding code comments --- src/DBDiff.php | 13 ++-- src/SQLGen/DiffSorter.php | 4 +- tests/End2EndTest.php | 121 +++++++++++++++++++++++++++----------- 3 files changed, 97 insertions(+), 41 deletions(-) diff --git a/src/DBDiff.php b/src/DBDiff.php index 159f345..7ab31a1 100644 --- a/src/DBDiff.php +++ b/src/DBDiff.php @@ -9,7 +9,8 @@ class DBDiff { - + private $diff; + public function run() { // Increase memory limit @@ -20,14 +21,14 @@ public function run() { // Diff $diffCalculator = new DiffCalculator; - $diff = $diffCalculator->getDiff($params); + $this->diff = $diffCalculator->getDiff($params); // Empty diff - if (empty($diff['schema']) && empty($diff['data'])) { + if (empty($this->diff['schema']) && empty($this->diff['data'])) { Logger::info("Identical resources"); } else { // SQL - $sqlGenerator = new SQLGenerator($diff); + $sqlGenerator = new SQLGenerator($this->diff); $up =''; $down = ''; if ($params->include !== 'down') { $up = $sqlGenerator->getUp(); @@ -53,4 +54,8 @@ public function run() { } } + + public function getDiff() { + return $this->diff; + } } diff --git a/src/SQLGen/DiffSorter.php b/src/SQLGen/DiffSorter.php index 874a11c..b8d9761 100644 --- a/src/SQLGen/DiffSorter.php +++ b/src/SQLGen/DiffSorter.php @@ -63,7 +63,7 @@ public function sort($diff, $type) { usort($diff, [$this, 'compare'.ucfirst($type)]); return $diff; } - + private function compareUp($a, $b) { return $this->compare($this->up_order, $a, $b); } @@ -80,7 +80,7 @@ private function compare($order, $a, $b) { $sqlGenClassB = $reflectionB->getShortName(); $indexA = $order[$sqlGenClassA]; $indexB = $order[$sqlGenClassB]; - + if ($indexA === $indexB) return 0; else if ($indexA > $indexB) return 1; return -1; diff --git a/tests/End2EndTest.php b/tests/End2EndTest.php index 9f7b150..42257f1 100644 --- a/tests/End2EndTest.php +++ b/tests/End2EndTest.php @@ -1,60 +1,111 @@ isContinuousIntegrationServer = getenv('ci'); + $this->host = $this->isContinuousIntegrationServer ? "127.0.0.1" : "localhost"; + $this->user = $this->isContinuousIntegrationServer ? "root" : "dbdiff"; + $this->pass = $this->isContinuousIntegrationServer ? "" : "dbdiff"; + $this->dbh = new PDO("mysql:host=$this->host", $this->user, $this->pass); + + // Set some global arguments for the CLI + $GLOBALS['argv'] = [ + "", + "--server1=$this->user:$this->pass@$this->host:$this->port", + "--template=templates/simple-db-migrate.tmpl", + "--type=all", + "--include=all", + "--nocomments", + "--output=./tests/end2end/$this->migration_actual", + "server1.$this->db1:server1.$this->db2" + ]; + } + + public function setUp() { + // Create databases for test + $this->dbh->exec("CREATE DATABASE $this->db1;"); + $this->dbh->exec("CREATE DATABASE $this->db2;"); + } + + public function tearDown() { + // Cleanup + $this->dbh->exec("DROP DATABASE `$this->db1`;"); + $this->dbh->exec("DROP DATABASE `$this->db2`;"); + + // Remove actual migration file + // unlink("./tests/end2end/$migration_actual"); + } + public function testAll() { - // db config - $host = "127.0.0.1"; - $port = 3306; - $user = "root"; - $pass = ""; - $db1 = "diff1"; - $db2 = "diff2"; - - // db migration - $migration_actual = 'migration_actual'; - $migration_expected = 'migration_expected'; - $dbh = new PDO("mysql:host=$host", $user, $pass); - $dbh->exec("DROP DATABASE `$db1`;"); - $dbh->exec("CREATE DATABASE $db1;"); - $dbh->exec("DROP DATABASE `$db2`;"); - $dbh->exec("CREATE DATABASE $db2;"); - - $db1h = new PDO("mysql:host=$host;dbname=$db1;", $user, $pass); + // Populate databases for test + $db1h = new PDO("mysql:host=$this->host;dbname=$this->db1;", $this->user, $this->pass); $db1h->exec(file_get_contents('tests/end2end/db1-up.sql')); - $db2h = new PDO("mysql:host=$host;dbname=$db2;", $user, $pass); + $db2h = new PDO("mysql:host=$this->host;dbname=$this->db2;", $this->user, $this->pass); $db2h->exec(file_get_contents('tests/end2end/db2-up.sql')); - $GLOBALS['argv'] = [ - "", - "--server1=$user:$pass@$host:$port", - "--template=templates/simple-db-migrate.tmpl", - "--type=all", - "--include=all", - "--nocomments", - "--output=./tests/end2end/$migration_actual", - "server1.$db1:server1.$db2" - ]; + ob_start(); + $dbdiff = new DBDiff\DBDiff; + $dbdiff->run(); + ob_end_clean(); + + $migration_actual_file = file_get_contents("./tests/end2end/$this->migration_actual"); + $migration_expected_file = file_get_contents("./tests/end2end/$this->migration_expected"); + + echo "\nActual migration output should match expected output for the test\n"; + $this->assertEquals($migration_actual_file, $migration_expected_file); + /* + $sqlGenerator = new SQLGenerator($dbdiff->getDiff()); + $db2h->exec($sqlGenerator->getUp()); + + // Apply the migration_actual UP to the target database and expect there to be no differences on the command-line anymore ob_start(); $dbdiff = new DBDiff\DBDiff; $dbdiff->run(); ob_end_clean(); - $migration_actual_file = file_get_contents("./tests/end2end/$migration_actual"); - $migration_expected_file = file_get_contents("./tests/end2end/$migration_expected"); - // unlink("./tests/end2end/$migration_actual"); + $diff = $dbdiff->getDiff(); + echo "\nAfter up migration is applied, up and down diff should be empty\n"; + $this->assertEquals(empty($diff['schema']) && empty($diff['data']), false); + + // Apply the migration actual DOWN to the target database and expect there to be the same expected differences again + ob_start(); + $dbdiff = new DBDiff\DBDiff; + $dbdiff->run(); + ob_end_clean(); - // TODO: Apply the migration_actual UP to the target database and expect there to be no differences on the command-line anymore - // TODO: Apply the migration actual DOWN to the target database and expect there to be the same expected differences again - // TODO: Ensure the database is emptied/reset after each test + $migration_actual_file = file_get_contents("./tests/end2end/$this->migration_actual"); + $migration_expected_file = file_get_contents("./tests/end2end/$this->migration_expected"); + echo "\nAfter the down migration is applied, actual migration output should match expected output for the test\n"; $this->assertEquals($migration_actual_file, $migration_expected_file); + */ } } ?> From 88e143921b4b029fa39efad657644a17a08d3342 Mon Sep 17 00:00:00 2001 From: Jasdeep Date: Fri, 30 Jun 2017 13:00:22 +0100 Subject: [PATCH 2/6] Fixing environment detection --- tests/End2EndTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/End2EndTest.php b/tests/End2EndTest.php index 42257f1..3d051f9 100644 --- a/tests/End2EndTest.php +++ b/tests/End2EndTest.php @@ -27,7 +27,7 @@ function __construct() { parent::__construct(); // Initialise variables - $this->isContinuousIntegrationServer = getenv('ci'); + $this->isContinuousIntegrationServer = getenv('CI'); $this->host = $this->isContinuousIntegrationServer ? "127.0.0.1" : "localhost"; $this->user = $this->isContinuousIntegrationServer ? "root" : "dbdiff"; $this->pass = $this->isContinuousIntegrationServer ? "" : "dbdiff"; From b9e57fe1f300b0c8e07522b8117ca6d96c294431 Mon Sep 17 00:00:00 2001 From: Jasdeep Date: Fri, 30 Jun 2017 23:06:06 +0100 Subject: [PATCH 3/6] Fixing issue of different array ordering when using `usort` in PHP 5.x vs PHP 7.x --- src/SQLGen/DiffSorter.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/SQLGen/DiffSorter.php b/src/SQLGen/DiffSorter.php index b8d9761..190ade8 100644 --- a/src/SQLGen/DiffSorter.php +++ b/src/SQLGen/DiffSorter.php @@ -3,7 +3,7 @@ class DiffSorter { - private $up_order = [ + private $up_order = array( "SetDBCharset", "SetDBCollation", @@ -29,9 +29,9 @@ class DiffSorter { "InsertData", "UpdateData" - ]; + ); - private $down_order = [ + private $down_order = array( "SetDBCharset", "SetDBCollation", @@ -57,7 +57,7 @@ class DiffSorter { "DeleteData", "UpdateData" - ]; + ); public function sort($diff, $type) { usort($diff, [$this, 'compare'.ucfirst($type)]); @@ -81,8 +81,8 @@ private function compare($order, $a, $b) { $indexA = $order[$sqlGenClassA]; $indexB = $order[$sqlGenClassB]; - if ($indexA === $indexB) return 0; - else if ($indexA > $indexB) return 1; - return -1; + if ($indexA > $indexB) return 1; + if ($indexA < $indexB) return -1; + return 0; } } From 7b28e424a0e3d4b9484c0117dc744b454596a9e8 Mon Sep 17 00:00:00 2001 From: Jasdeep Date: Fri, 30 Jun 2017 23:25:14 +0100 Subject: [PATCH 4/6] Fixing issue of different array ordering when using `usort` in PHP 5.x vs PHP 7.x --- src/SQLGen/DiffSorter.php | 2 ++ tests/end2end/migration_expected | 22 +++++++++++----------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/SQLGen/DiffSorter.php b/src/SQLGen/DiffSorter.php index 190ade8..a03c4c7 100644 --- a/src/SQLGen/DiffSorter.php +++ b/src/SQLGen/DiffSorter.php @@ -83,6 +83,8 @@ private function compare($order, $a, $b) { if ($indexA > $indexB) return 1; if ($indexA < $indexB) return -1; + if ($a > $b) return 1; + if ($a < $b) return -1; return 0; } } diff --git a/tests/end2end/migration_expected b/tests/end2end/migration_expected index 960ac73..89a9126 100644 --- a/tests/end2end/migration_expected +++ b/tests/end2end/migration_expected @@ -4,25 +4,25 @@ CREATE TABLE `cc` ( PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; DELETE FROM `asas` WHERE `id` = '2' AND `firstname` = 'x'; -DELETE FROM `asas` WHERE `id` = '7' AND `firstname` = 'e'; DELETE FROM `asas` WHERE `id` = '6' AND `firstname` = 't'; +DELETE FROM `asas` WHERE `id` = '7' AND `firstname` = 'e'; DELETE FROM `zz` WHERE `id` = '1' AND `name` = 'name'; DROP TABLE `zz`; ALTER TABLE `aa` ENGINE = InnoDB; -ALTER TABLE `bb` DEFAULT COLLATE latin1_swedish_ci; ALTER TABLE `aa` DEFAULT COLLATE latin1_swedish_ci; ALTER TABLE `asas` DEFAULT COLLATE latin1_spanish_ci; +ALTER TABLE `bb` DEFAULT COLLATE latin1_swedish_ci; +ALTER TABLE `aa` ADD `as` int(11) NOT NULL; ALTER TABLE `aa` ADD `qw` int(11) NOT NULL; ALTER TABLE `bb` ADD `jj` int(11) NOT NULL; -ALTER TABLE `aa` ADD `as` int(11) NOT NULL; ALTER TABLE `aa` CHANGE `name` `name` varchar(255) NOT NULL; ALTER TABLE `aa` CHANGE `pass` `pass` varchar(255) DEFAULT NULL; ALTER TABLE `asas` CHANGE `firstname` `firstname` varchar(255) COLLATE latin1_spanish_ci NOT NULL; ALTER TABLE `asas` CHANGE `lastname` `lastname` varchar(255) COLLATE latin1_spanish_ci NOT NULL; ALTER TABLE `aa` DROP `zx`; ALTER TABLE `aa` ADD KEY `as` (`as`); -ALTER TABLE `bb` ADD PRIMARY KEY (`id`); ALTER TABLE `aa` ADD UNIQUE KEY `name` (`name`); +ALTER TABLE `bb` ADD PRIMARY KEY (`id`); ALTER TABLE `aa` DROP INDEX `id`; ALTER TABLE `aa` ADD PRIMARY KEY (`id`); ALTER TABLE `aa` DROP INDEX `namekey`; @@ -35,10 +35,10 @@ INSERT INTO `cc` VALUES('11'); UPDATE `asas` SET `lastname` = 'bb' WHERE `id` = '1' AND `firstname` = 'a'; """ SQL_DOWN = u""" -DELETE FROM `cc` WHERE `id` = '11'; -DELETE FROM `asas` WHERE `id` = '4' AND `firstname` = 'v'; -DELETE FROM `asas` WHERE `id` = '3' AND `firstname` = 'x'; DELETE FROM `asas` WHERE `id` = '2' AND `firstname` = 'c'; +DELETE FROM `asas` WHERE `id` = '3' AND `firstname` = 'x'; +DELETE FROM `asas` WHERE `id` = '4' AND `firstname` = 'v'; +DELETE FROM `cc` WHERE `id` = '11'; DROP TABLE `cc`; CREATE TABLE `zz` ( `id` int(11) NOT NULL, @@ -50,16 +50,16 @@ CREATE TABLE `zz` ( KEY `time` (`time`,`bool`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; ALTER TABLE `aa` ENGINE = MyISAM; -ALTER TABLE `asas` DEFAULT COLLATE latin1_bin; ALTER TABLE `aa` DEFAULT COLLATE latin1_spanish_ci; +ALTER TABLE `asas` DEFAULT COLLATE latin1_bin; ALTER TABLE `bb` DEFAULT COLLATE latin1_bin; ALTER TABLE `aa` DROP `as`; ALTER TABLE `aa` DROP `qw`; ALTER TABLE `bb` DROP `jj`; -ALTER TABLE `asas` CHANGE `lastname` `lastname` varchar(255) COLLATE latin1_bin NOT NULL; -ALTER TABLE `asas` CHANGE `firstname` `firstname` varchar(255) COLLATE latin1_bin NOT NULL; -ALTER TABLE `aa` CHANGE `pass` `pass` varchar(255) CHARACTER SET latin1 NOT NULL; ALTER TABLE `aa` CHANGE `name` `name` varchar(255) CHARACTER SET latin1 NOT NULL DEFAULT 'aa'; +ALTER TABLE `aa` CHANGE `pass` `pass` varchar(255) CHARACTER SET latin1 NOT NULL; +ALTER TABLE `asas` CHANGE `firstname` `firstname` varchar(255) COLLATE latin1_bin NOT NULL; +ALTER TABLE `asas` CHANGE `lastname` `lastname` varchar(255) COLLATE latin1_bin NOT NULL; ALTER TABLE `aa` ADD `zx` int(11) NOT NULL; ALTER TABLE `aa` DROP INDEX `as`; ALTER TABLE `aa` DROP INDEX `name`; From 140fa6e0934b21c69d67edec2fbfa6eec038269b Mon Sep 17 00:00:00 2001 From: Jasdeep Date: Sat, 1 Jul 2017 12:19:04 +0100 Subject: [PATCH 5/6] Renaming test files and variables for better clarity --- tests/End2EndTest.php | 96 +++++++++++++----------- tests/end2end/{db1-up.sql => source.sql} | 0 tests/end2end/{db2-up.sql => target.sql} | 0 3 files changed, 51 insertions(+), 45 deletions(-) rename tests/end2end/{db1-up.sql => source.sql} (100%) rename tests/end2end/{db2-up.sql => target.sql} (100%) diff --git a/tests/End2EndTest.php b/tests/End2EndTest.php index 3d051f9..fc112f8 100644 --- a/tests/End2EndTest.php +++ b/tests/End2EndTest.php @@ -1,13 +1,12 @@ host = $this->isContinuousIntegrationServer ? "127.0.0.1" : "localhost"; $this->user = $this->isContinuousIntegrationServer ? "root" : "dbdiff"; $this->pass = $this->isContinuousIntegrationServer ? "" : "dbdiff"; - $this->dbh = new PDO("mysql:host=$this->host", $this->user, $this->pass); + $this->db = new PDO("mysql:host=$this->host", $this->user, $this->pass); // Set some global arguments for the CLI $GLOBALS['argv'] = [ @@ -42,33 +43,30 @@ function __construct() { "--include=all", "--nocomments", "--output=./tests/end2end/$this->migration_actual", - "server1.$this->db1:server1.$this->db2" + "server1.$this->source:server1.$this->target" ]; - } - public function setUp() { // Create databases for test - $this->dbh->exec("CREATE DATABASE $this->db1;"); - $this->dbh->exec("CREATE DATABASE $this->db2;"); + $this->db->exec("CREATE DATABASE `$this->source`;"); + $this->db->exec("CREATE DATABASE `$this->target`;"); + + // Populate databases for test + $this->db_source = new PDO("mysql:host=$this->host;dbname=$this->source;", $this->user, $this->pass); + $this->db_source->exec(file_get_contents("tests/end2end/$this->source.sql")); + $this->db_target = new PDO("mysql:host=$this->host;dbname=$this->target;", $this->user, $this->pass); + $this->db_target->exec(file_get_contents("tests/end2end/$this->target.sql")); } - public function tearDown() { + function __destruct() { // Cleanup - $this->dbh->exec("DROP DATABASE `$this->db1`;"); - $this->dbh->exec("DROP DATABASE `$this->db2`;"); + $this->db->exec("DROP DATABASE `$this->source`;"); + $this->db->exec("DROP DATABASE `$this->target`;"); // Remove actual migration file // unlink("./tests/end2end/$migration_actual"); } - public function testAll() - { - // Populate databases for test - $db1h = new PDO("mysql:host=$this->host;dbname=$this->db1;", $this->user, $this->pass); - $db1h->exec(file_get_contents('tests/end2end/db1-up.sql')); - $db2h = new PDO("mysql:host=$this->host;dbname=$this->db2;", $this->user, $this->pass); - $db2h->exec(file_get_contents('tests/end2end/db2-up.sql')); - + public function testExpectedMigrationMatchesActualMigration() { ob_start(); $dbdiff = new DBDiff\DBDiff; $dbdiff->run(); @@ -77,35 +75,43 @@ public function testAll() $migration_actual_file = file_get_contents("./tests/end2end/$this->migration_actual"); $migration_expected_file = file_get_contents("./tests/end2end/$this->migration_expected"); - echo "\nActual migration output should match expected output for the test\n"; $this->assertEquals($migration_actual_file, $migration_expected_file); - /* $sqlGenerator = new SQLGenerator($dbdiff->getDiff()); - $db2h->exec($sqlGenerator->getUp()); + return $sqlGenerator; + } - // Apply the migration_actual UP to the target database and expect there to be no differences on the command-line anymore - ob_start(); - $dbdiff = new DBDiff\DBDiff; - $dbdiff->run(); - ob_end_clean(); + /** + * @depends testExpectedMigrationMatchesActualMigration + */ + public function testApplyMigrationUpToTargetDatabaseAndExpectNoDifferences($sqlGenerator) { + /* + $this->db_target->exec($sqlGenerator->getUp()); - $diff = $dbdiff->getDiff(); - echo "\nAfter up migration is applied, up and down diff should be empty\n"; - $this->assertEquals(empty($diff['schema']) && empty($diff['data']), false); + $dbdiff = new DBDiff\DBDiff; + $dbdiff->run(); - // Apply the migration actual DOWN to the target database and expect there to be the same expected differences again - ob_start(); - $dbdiff = new DBDiff\DBDiff; - $dbdiff->run(); - ob_end_clean(); + $diff = $dbdiff->getDiff(); + $this->assertEquals(empty($diff['schema']) && empty($diff['data']), true); - $migration_actual_file = file_get_contents("./tests/end2end/$this->migration_actual"); - $migration_expected_file = file_get_contents("./tests/end2end/$this->migration_expected"); + return $sqlGenerator; + */ + } - echo "\nAfter the down migration is applied, actual migration output should match expected output for the test\n"; - $this->assertEquals($migration_actual_file, $migration_expected_file); - */ + /** + * @depends testApplyMigrationUpToTargetDatabaseAndExpectNoDifferences + */ + public function testApplyMigrationDownToTargetDatabaseAndExpectSameMigrationIsProducedAsBefore($sqlGenerator) { + /* + $this->db_target->exec($sqlGenerator->getDown()); + + $dbdiff = new DBDiff\DBDiff; + $dbdiff->run(); + + $migration_actual_file = file_get_contents("./tests/end2end/$this->migration_actual"); + + $this->assertEquals($migration_actual_file, $migration_expected_file); + */ } } ?> diff --git a/tests/end2end/db1-up.sql b/tests/end2end/source.sql similarity index 100% rename from tests/end2end/db1-up.sql rename to tests/end2end/source.sql diff --git a/tests/end2end/db2-up.sql b/tests/end2end/target.sql similarity index 100% rename from tests/end2end/db2-up.sql rename to tests/end2end/target.sql From 8f374abcc0c17664f8158ee0fb8610d64d36ce36 Mon Sep 17 00:00:00 2001 From: Jasdeep Date: Sat, 6 Apr 2019 16:46:27 +0100 Subject: [PATCH 6/6] Removing 'nightly' PHP test --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 2302756..c7cd5e0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,7 +7,6 @@ php: - '5.6' - '7.0' - '7.1' - - nightly install: composer install services: