Skip to content

Commit

Permalink
Add support for "if-not-exists" option (ifsnop#204)
Browse files Browse the repository at this point in the history
Co-authored-by: Edgaras Janušauskas <[email protected]>
  • Loading branch information
smalos and garas authored Aug 17, 2020
1 parent 310ee8b commit 628ac38
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ $dumpSettingsDefault = array(
'compress' => Mysqldump::NONE,
'init_commands' => array(),
'no-data' => array(),
'if-not-exists' => false,
'reset-auto-increment' => false,
'add-drop-database' => false,
'add-drop-table' => false,
Expand Down Expand Up @@ -220,6 +221,8 @@ $this->_dumpSettings = self::array_replace_recursive($dumpSettingsDefault, $dump
- Exclude these tables (array of table names), include all if empty, supports regexps.
- **include-views**
- Only include these views (array of view names), include all if empty. By default, all views named as the include-tables array are included.
- **if-not-exists**
- Only create a new table when a table of the same name does not already exist. No error message is thrown if the table already exists.
- **compress**
- Gzip, Bzip2, None.
- Could be specified using the declared consts: IMysqldump\Mysqldump::GZIP, IMysqldump\Mysqldump::BZIP2 or IMysqldump\Mysqldump::NONE
Expand Down
5 changes: 5 additions & 0 deletions src/Ifsnop/Mysqldump/Mysqldump.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ public function __construct(
'compress' => Mysqldump::NONE,
'init_commands' => array(),
'no-data' => array(),
'if-not-exists' => false,
'reset-auto-increment' => false,
'add-drop-database' => false,
'add-drop-table' => false,
Expand Down Expand Up @@ -1873,6 +1874,10 @@ public function create_table($row)
$replace = "";
$createTable = preg_replace($match, $replace, $createTable);
}

if ($this->dumpSettings['if-not-exists'] ) {
$createTable = preg_replace('/^CREATE TABLE/', 'CREATE TABLE IF NOT EXISTS', $createTable);
}

$ret = "/*!40101 SET @saved_cs_client = @@character_set_client */;".PHP_EOL.
"/*!40101 SET character_set_client = ".$this->dumpSettings['default-character-set']." */;".PHP_EOL.
Expand Down

0 comments on commit 628ac38

Please sign in to comment.