Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pre and Post SQL #14

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/Mysqldump.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ class Mysqldump
private array $tableWheres = [];
private array $tableLimits = [];

// pended SQL defaults to nothing
private string $prependSQL = '';
private string $appendSQL = '';

/**
* Constructor of Mysqldump.
*
Expand Down Expand Up @@ -163,6 +167,30 @@ private function write(string $data): int
return $this->io->write($data);
}

/**
* Add SQL to the start of the backup
*
* @param string $sql to add at start of backup. Caller is responsible for ensuring the SQL is valid the the backup.
* */
public function addPrependSQL(string $sql)
{
$this->prependSQL .= $sql;

return $this;
}

/**
* Add SQL to the end of the backup
*
* @param string $sql to add at end of backup. Caller is responsible for ensuring the SQL is valid the the backup.
*/
public function addAppendSQL(string $sql)
{
$this->appendSQL .= $sql;

return $this;
}

/**
* Primary function, triggers dumping.
*
Expand Down Expand Up @@ -192,6 +220,13 @@ public function start(?string $filename = '')
$this->write($this->getDumpFileHeader());
}

// Write prepend SQL to output file
if ($this->prependSQL) {
$this->write("-- mysqldump-php prepended SQL". PHP_EOL.
$this->prependSQL. PHP_EOL.
"-- mysqldump-php end prepended SQL". PHP_EOL. PHP_EOL);
}

// Store server settings and use saner defaults to dump
$this->write($this->db->backupParameters());

Expand Down Expand Up @@ -233,6 +268,13 @@ public function start(?string $filename = '')
// Restore saved parameters.
$this->write($this->db->restoreParameters());

// Write append SQL to output file
if ($this->appendSQL) {
$this->write("-- mysqldump-php appended SQL". PHP_EOL.
$this->appendSQL. PHP_EOL.
"-- mysqldump-php end appended SQL". PHP_EOL. PHP_EOL);
}

// Write some stats to output file.
if (!$this->settings->skipComments()) {
$this->write($this->getDumpFileFooter());
Expand Down