diff --git a/src/Parser.php b/src/Parser.php index 89df455..c19dbf2 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -51,7 +51,7 @@ public function parseTables($sqlScript) $table = new Table($name); $table->setDefinition(trim($definition)); - $table->setCreationScript(trim($creationScript)); + $table->setCreationScript(trim($creationScript) . ';'); if ($engine) { $table->setEngine($engine); diff --git a/src/RegExpPattern.php b/src/RegExpPattern.php index 6b11f8b..7e9d6d6 100644 --- a/src/RegExpPattern.php +++ b/src/RegExpPattern.php @@ -32,11 +32,12 @@ class RegExpPattern public static function tables() { $pattern = '/(?CREATE\s+TABLE\s+`(?\S+)`\s+'; - $pattern .= '\((?[^;]+)\)'; - $pattern .= '(?:\s+ENGINE=(?\S+))?\s*'; + $pattern .= '\((?[^;\/]+)\)'; + $pattern .= '(?:\s+ENGINE=(?[^;\s]+))?\s*'; $pattern .= '(?:AUTO_INCREMENT=(?\d+))?\s*'; - $pattern .= '(?:DEFAULT CHARSET=(?\S+))?\s*'; - $pattern .= ';)/'; + $pattern .= '(?:DEFAULT CHARSET=(?[^;\s]+))?\s*)'; + $pattern .= '(?:\/\*.+?\*\/)?\s*'; + $pattern .= ';/'; $pattern .= 's'; // modifier return $pattern; diff --git a/tests/ParserTest.php b/tests/ParserTest.php index 4fb122b..854809b 100644 --- a/tests/ParserTest.php +++ b/tests/ParserTest.php @@ -262,4 +262,15 @@ public function testIsGeneratingTableCreationScript() $this->assertEquals($table->getCreationScript(), $table->generateCreationScript(false, false)); } } -} \ No newline at end of file + + public function testIsParsingTableWithPartitionDefinitions() + { + $parser = new Parser(); + + $database = $parser->parseDatabase($this->getDatabaseFixture('partition.sql')); + + $this->assertInstanceOf(Database::class, $database); + $this->assertCount(1, $database->getTables()); + $this->assertCount(9, $database->getTableByName('export')->getColumns()); + } +} diff --git a/tests/fixtures/partition.sql b/tests/fixtures/partition.sql new file mode 100644 index 0000000..652a6e7 --- /dev/null +++ b/tests/fixtures/partition.sql @@ -0,0 +1,21 @@ +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `export` ( + `id_export` int(10) unsigned NOT NULL AUTO_INCREMENT, + `export_type` varchar(50) NOT NULL, + `filename` varchar(255) NOT NULL DEFAULT '', + `file_content` longblob NOT NULL, + `generated_at` datetime NOT NULL, + `downloaded_at` datetime DEFAULT NULL, + `generated_by` varchar(50) NOT NULL DEFAULT '', + `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, + `updated_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP, + PRIMARY KEY (`id_export`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8 +/*!50100 PARTITION BY RANGE (id_export) +(PARTITION p0 VALUES LESS THAN (1000) ENGINE = InnoDB, + PARTITION p1 VALUES LESS THAN (2000) ENGINE = InnoDB, + PARTITION p2 VALUES LESS THAN (3000) ENGINE = InnoDB, + PARTITION p3 VALUES LESS THAN (4000) ENGINE = InnoDB, + PARTITION p4 VALUES LESS THAN MAXVALUE ENGINE = InnoDB) */; +/*!40101 SET character_set_client = @saved_cs_client */; \ No newline at end of file