Skip to content

Commit

Permalink
dump of events. Closes ifsnop#99
Browse files Browse the repository at this point in the history
  • Loading branch information
ifsnop committed Apr 8, 2017
1 parent 411fd80 commit 9ec5e1e
Show file tree
Hide file tree
Showing 6 changed files with 244 additions and 55 deletions.
66 changes: 34 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,58 +158,60 @@ Refer to the [wiki](https://github.com/ifsnop/mysqldump-php/wiki/full-example) f
- **compress**
- Gzip, Bzip2, None.
- Could be specified using the declared consts: IMysqldump\Mysqldump::GZIP, IMysqldump\Mysqldump::BZIP2 or IMysqldump\Mysqldump::NONE
- **no-data**
- http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_no-data
- Do not dump data for these tables (array of table names), support regexps, `true` to ignore all tables
- **reset-auto-increment**
- Removes the AUTO_INCREMENT option from the database definition
- Useful when used with no-data, so when db is recreated, it will start from 1 instead of using an old value
- **add-drop-database**
- http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_add-drop-database
- **add-drop-table**
- http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_add-drop-table
- **single-transaction**
- http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_single-transaction
- **lock-tables**
- http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_lock-tables
- **add-drop-triggers**
- http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_add-drop-trigger
- **add-locks**
- http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_add-locks
- **extended-insert**
- http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_extended-insert
- **complete-insert**
- http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_complete-insert
- **disable-keys**
- http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_disable-keys
- **where**
- http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_where
- **no-create-info**
- http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_no-create-info
- **skip-triggers**
- http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_triggers
- **add-drop-triggers**
- http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_add-drop-trigger
- **routines**
- http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_routines
- **hex-blob**
- http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_hex-blob
- **databases**
- http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_databases
- **add-drop-database**
- http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_add-drop-database
- **skip-tz-utc**
- http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_tz-utc
- **no-autocommit**
- Option to disable autocommit (faster inserts, no problems with index keys)
- http://dev.mysql.com/doc/refman/4.1/en/commit.html
- **default-character-set**
- utf8 (default, compatible option), utf8mb4 (for full utf8 compliance)
- Could be specified using the declared consts: IMysqldump\Mysqldump::UTF8 or IMysqldump\Mysqldump::UTF8MB4BZIP2
- http://dev.mysql.com/doc/refman/5.5/en/charset-unicode-utf8mb4.html
- https://mathiasbynens.be/notes/mysql-utf8mb4
- **disable-keys**
- http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_disable-keys
- **events**
- https://dev.mysql.com/doc/refman/5.7/en/mysqldump.html#option_mysqldump_events
- **extended-insert**
- http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_extended-insert
- **hex-blob**
- http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_hex-blob
- **lock-tables**
- http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_lock-tables
- **net_buffer_length**
- http://dev.mysql.com/doc/refman/5.7/en/mysqldump.html#option_mysqldump_net_buffer_length
- **no-autocommit**
- Option to disable autocommit (faster inserts, no problems with index keys)
- http://dev.mysql.com/doc/refman/4.1/en/commit.html
- **no-create-info**
- http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_no-create-info
- **no-data**
- http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_no-data
- Do not dump data for these tables (array of table names), support regexps, `true` to ignore all tables
- **routines**
- http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_routines
- **single-transaction**
- http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_single-transaction
- **skip-comments**
- http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_comments
- **skip-dump-date**
- http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_dump-date
- **net_buffer_length**
- http://dev.mysql.com/doc/refman/5.7/en/mysqldump.html#option_mysqldump_net_buffer_length
- **skip-triggers**
- http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_triggers
- **skip-tz-utc**
- http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_tz-utc
- **where**
- http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_where

The following options are now enabled by default, and there is no way to disable them since
they should always be used.
Expand Down
154 changes: 139 additions & 15 deletions src/Ifsnop/Mysqldump/Mysqldump.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class Mysqldump
private $views = array();
private $triggers = array();
private $procedures = array();
private $events = array();
private $dbHandler = null;
private $dbType;
private $compressManager;
Expand Down Expand Up @@ -116,30 +117,31 @@ public function __construct(
'include-tables' => array(),
'exclude-tables' => array(),
'compress' => Mysqldump::NONE,
'init_commands' => array(),
'no-data' => array(),
'reset-auto-increment' => false,
'add-drop-database' => false,
'add-drop-table' => false,
'single-transaction' => true,
'lock-tables' => true,
'add-drop-trigger' => true,
'add-locks' => true,
'extended-insert' => true,
'complete-insert' => false,
'databases' => false,
'default-character-set' => Mysqldump::UTF8,
'disable-keys' => true,
'where' => '',
'extended-insert' => true,
'events' => false,
'hex-blob' => true, /* faster than escaped content */
'net_buffer_length' => self::MAXLINESIZE,
'no-autocommit' => true,
'no-create-info' => false,
'skip-triggers' => false,
'add-drop-trigger' => true,
'lock-tables' => true,
'routines' => false,
'hex-blob' => true, /* faster than escaped content */
'databases' => false,
'add-drop-database' => false,
'single-transaction' => true,
'skip-triggers' => false,
'skip-tz-utc' => false,
'no-autocommit' => true,
'default-character-set' => Mysqldump::UTF8,
'skip-comments' => false,
'skip-dump-date' => false,
'init_commands' => array(),
'net_buffer_length' => self::MAXLINESIZE,
'where' => '',
/* deprecated */
'disable-foreign-keys-check' => true
);
Expand Down Expand Up @@ -366,6 +368,7 @@ public function start($filename = '')
$this->exportViews();
$this->exportTriggers();
$this->exportProcedures();
$this->exportEvents();

// Restore saved parameters
$this->compressManager->write(
Expand Down Expand Up @@ -483,6 +486,13 @@ private function getDatabaseStructure()
array_push($this->procedures, $row['procedure_name']);
}
}

// Listing all events from database
if ($this->dumpSettings['events']) {
foreach ($this->dbHandler->query($this->typeAdapter->show_events($this->dbName)) as $row) {
array_push($this->events, $row['event_name']);
}
}
}

/**
Expand Down Expand Up @@ -581,6 +591,19 @@ private function exportProcedures()
}
}

/**
* Exports all the events found in database
*
* @return null
*/
private function exportEvents()
{
// Exporting triggers one by one
foreach ($this->events as $event) {
$this->getEventStructure($event);
}
}

/**
* Table structure extractor
*
Expand Down Expand Up @@ -770,6 +793,29 @@ private function getProcedureStructure($procedureName)
}
}

/**
* Event structure extractor
*
* @param string $eventName Name of event to export
* @return null
*/
private function getEventStructure($eventName)
{
if (!$this->dumpSettings['skip-comments']) {
$ret = "--" . PHP_EOL .
"-- Dumping events for database '" . $this->dbName . "'" . PHP_EOL .
"--" . PHP_EOL . PHP_EOL;
$this->compressManager->write($ret);
}
$stmt = $this->typeAdapter->show_create_event($eventName);
foreach ($this->dbHandler->query($stmt) as $r) {
$this->compressManager->write(
$this->typeAdapter->create_event($r, $this->dumpSettings)
);
return;
}
}

/**
* Escape values with quotes when needed
*
Expand Down Expand Up @@ -1259,6 +1305,11 @@ public function show_procedures()
return "";
}

public function show_events()
{
return "";
}

public function setup_transaction()
{
return "";
Expand Down Expand Up @@ -1457,6 +1508,11 @@ public function show_create_procedure($procedureName)
return "SHOW CREATE PROCEDURE `$procedureName`";
}

public function show_create_event($eventName)
{
return "SHOW CREATE EVENT `$eventName`";
}

public function create_table( $row, $dumpSettings )
{
if ( !isset($row['Create Table']) ) {
Expand Down Expand Up @@ -1533,11 +1589,11 @@ public function create_trigger($row)
$triggerStmtReplaced
);
if ( false === $triggerStmtReplaced ) {
$triggerStmtReplaced = $triggerStmt;
$triggerStmtReplaced = $triggerStmt . " /* ";
}

$ret .= "DELIMITER ;;" . PHP_EOL .
$triggerStmtReplaced . "*/;;" . PHP_EOL .
$triggerStmtReplaced . " */ ;;" . PHP_EOL .
"DELIMITER ;" . PHP_EOL . PHP_EOL;
return $ret;
}
Expand All @@ -1563,6 +1619,59 @@ public function create_procedure($row, $dumpSettings)
return $ret;
}

public function create_event($row, $dumpSettings)
{
$ret = "";
if ( !isset($row['Create Event']) ) {
throw new Exception("Error getting event code, unknown output. " .
"Please check 'http://stackoverflow.com/questions/10853826/mysql-5-5-create-event-gives-syntax-error'");
}
$eventName = $row['Event'];
$eventStmt = $row['Create Event'];
$sqlMode = $row['sql_mode'];

$eventStmtReplaced = str_replace(
"CREATE DEFINER",
"/*!50106 CREATE*/ /*!50117 DEFINER",
$eventStmt
);
$eventStmtReplaced = str_replace(
" EVENT ",
"*/ /*!50106 EVENT ",
$eventStmtReplaced
);

if ( false === $eventStmtReplaced ) {
$eventStmtReplaced = $eventStmt . " /* ";
}

$ret .= "/*!50106 SET @save_time_zone= @@TIME_ZONE */ ;" . PHP_EOL .
"/*!50106 DROP EVENT IF EXISTS `" . $eventName . "` */;" . PHP_EOL .
"DELIMITER ;;" . PHP_EOL .
"/*!50003 SET @saved_cs_client = @@character_set_client */ ;;" . PHP_EOL .
"/*!50003 SET @saved_cs_results = @@character_set_results */ ;;" . PHP_EOL .
"/*!50003 SET @saved_col_connection = @@collation_connection */ ;;" . PHP_EOL .
"/*!50003 SET character_set_client = utf8 */ ;;" . PHP_EOL .
"/*!50003 SET character_set_results = utf8 */ ;;" . PHP_EOL .
"/*!50003 SET collation_connection = utf8_general_ci */ ;;" . PHP_EOL .
"/*!50003 SET @saved_sql_mode = @@sql_mode */ ;;" . PHP_EOL .
"/*!50003 SET sql_mode = '" . $sqlMode . "' */ ;;" . PHP_EOL .
"/*!50003 SET @saved_time_zone = @@time_zone */ ;;" . PHP_EOL .
"/*!50003 SET time_zone = 'SYSTEM' */ ;;" . PHP_EOL .
$eventStmtReplaced . " */ ;;" . PHP_EOL .
"/*!50003 SET time_zone = @saved_time_zone */ ;;" . PHP_EOL .
"/*!50003 SET sql_mode = @saved_sql_mode */ ;;" . PHP_EOL .
"/*!50003 SET character_set_client = @saved_cs_client */ ;;" . PHP_EOL .
"/*!50003 SET character_set_results = @saved_cs_results */ ;;" . PHP_EOL .
"/*!50003 SET collation_connection = @saved_col_connection */ ;;" . PHP_EOL .
"DELIMITER ;" . PHP_EOL .
"/*!50106 SET TIME_ZONE= @save_time_zone */ ;" . PHP_EOL . PHP_EOL;
// Commented because we are doing this in restore_parameters()
// "/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;" . PHP_EOL . PHP_EOL;

return $ret;
}

public function show_tables()
{
$this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__);
Expand Down Expand Up @@ -1604,6 +1713,21 @@ public function show_procedures()
"WHERE ROUTINE_TYPE='PROCEDURE' AND ROUTINE_SCHEMA='${args[0]}'";
}

/**
* Get query string to ask for names of events from current database.
*
* @param string Name of database
* @return string
*/
public function show_events()
{
$this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__);
$args = func_get_args();
return "SELECT EVENT_NAME AS event_name " .
"FROM INFORMATION_SCHEMA.EVENTS " .
"WHERE EVENT_SCHEMA='${args[0]}'";
}

public function setup_transaction()
{
return "SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ";
Expand Down
2 changes: 2 additions & 0 deletions tests/create_users.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ mysql -e "CREATE DATABASE test006a;"
mysql -e "CREATE DATABASE test006b;"
mysql -e "CREATE DATABASE test008;"
mysql -e "CREATE DATABASE test009;"
mysql -e "CREATE DATABASE test010;"
mysql -e "GRANT ALL PRIVILEGES ON test001.* TO 'travis'@'%' WITH GRANT OPTION;"
mysql -e "GRANT ALL PRIVILEGES ON test002.* TO 'travis'@'%' WITH GRANT OPTION;"
mysql -e "GRANT ALL PRIVILEGES ON test005.* TO 'travis'@'%' WITH GRANT OPTION;"
mysql -e "GRANT ALL PRIVILEGES ON test006a.* TO 'travis'@'%' WITH GRANT OPTION;"
mysql -e "GRANT ALL PRIVILEGES ON test006b.* TO 'travis'@'%' WITH GRANT OPTION;"
mysql -e "GRANT ALL PRIVILEGES ON test008.* TO 'travis'@'%' WITH GRANT OPTION;"
mysql -e "GRANT ALL PRIVILEGES ON test009.* TO 'travis'@'%' WITH GRANT OPTION;"
mysql -e "GRANT ALL PRIVILEGES ON test010.* TO 'travis'@'%' WITH GRANT OPTION;"
mysql -e "GRANT SUPER,LOCK TABLES ON *.* TO 'travis'@'%';"
mysql -e "GRANT SELECT ON mysql.proc to 'travis'@'%';"
mysql -e "FLUSH PRIVILEGES;"
Loading

0 comments on commit 9ec5e1e

Please sign in to comment.