Skip to content

Commit 45da48e

Browse files
committed
feat: add --no-drop-tables option to pdodb dump command
- Add dropTables parameter to dumpSchema() method in DialectInterface and all dialect implementations (SQLite, MySQL, MariaDB, PostgreSQL, MSSQL) - By default, DROP TABLE IF EXISTS is included before CREATE TABLE in dumps - Add --no-drop-tables CLI option to exclude DROP TABLE IF EXISTS statements - Update all dialects to add DROP TABLE IF EXISTS before CREATE TABLE: - SQLite: DROP TABLE IF EXISTS - MySQL/MariaDB: DROP TABLE IF EXISTS - PostgreSQL: DROP TABLE IF EXISTS ... CASCADE - MSSQL: IF OBJECT_ID(...) IS NOT NULL DROP TABLE ... - Add tests for new functionality: - testDumpIncludesDropTableByDefault() - testDumpNoDropTablesOption() - testDumpCommandNoDropTablesOption() - Update documentation (README.md and CLI tools documentation) with new option and behavior explanation
1 parent d10563b commit 45da48e

File tree

12 files changed

+85
-12
lines changed

12 files changed

+85
-12
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2787,7 +2787,7 @@ vendor/bin/pdodb user password john --password newpass123
27872787
Export and import database schema and data:
27882788

27892789
```bash
2790-
# Full database dump to file
2790+
# Full database dump to file (includes DROP TABLE IF EXISTS by default)
27912791
vendor/bin/pdodb dump --output=backup.sql
27922792

27932793
# Dump specific table to file
@@ -2799,6 +2799,9 @@ vendor/bin/pdodb dump --schema-only --output=schema.sql
27992799
# Data only (no schema)
28002800
vendor/bin/pdodb dump --data-only --output=data.sql
28012801

2802+
# Dump without DROP TABLE IF EXISTS statements
2803+
vendor/bin/pdodb dump --no-drop-tables --output=backup.sql
2804+
28022805
# Restore from dump file
28032806
vendor/bin/pdodb dump restore backup.sql
28042807

documentation/05-advanced-features/21-cli-tools.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ vendor/bin/pdodb dump --schema-only --output=schema.sql
3232
# Data only (no schema)
3333
vendor/bin/pdodb dump --data-only --output=data.sql
3434

35+
# Dump without DROP TABLE IF EXISTS statements
36+
vendor/bin/pdodb dump --no-drop-tables --output=backup.sql
37+
3538
# Restore from dump file
3639
vendor/bin/pdodb dump restore backup.sql
3740

@@ -44,8 +47,13 @@ vendor/bin/pdodb dump restore backup.sql --force
4447
- `--schema-only` - Dump only schema (CREATE TABLE, indexes, etc.)
4548
- `--data-only` - Dump only data (INSERT statements)
4649
- `--output=<file>` - Write dump to file instead of stdout
50+
- `--no-drop-tables` - Do not add DROP TABLE IF EXISTS before CREATE TABLE (by default, DROP TABLE IF EXISTS is included)
4751
- `--force` - Skip confirmation prompt (for restore)
4852

53+
### Behavior
54+
55+
By default, `pdodb dump` includes `DROP TABLE IF EXISTS` statements before each `CREATE TABLE` statement. This ensures that restoring a dump will replace existing tables. Use the `--no-drop-tables` option to exclude these statements if you want to preserve existing tables.
56+
4957
### Notes
5058

5159
- Dump format is SQL-compatible across all supported dialects

src/cli/DumpManager.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ class DumpManager
1919
* @param string|null $table Table name (null = entire database)
2020
* @param bool $schemaOnly Only dump schema (no data)
2121
* @param bool $dataOnly Only dump data (no schema)
22+
* @param bool $dropTables Whether to add DROP TABLE IF EXISTS before CREATE TABLE
2223
*
2324
* @return string SQL dump content
2425
* @throws ResourceException If operation is not supported or fails
2526
*/
26-
public static function dump(PdoDb $db, ?string $table = null, bool $schemaOnly = false, bool $dataOnly = false): string
27+
public static function dump(PdoDb $db, ?string $table = null, bool $schemaOnly = false, bool $dataOnly = false, bool $dropTables = true): string
2728
{
2829
$dialect = $db->schema()->getDialect();
2930
$driver = $dialect->getDriverName();
@@ -43,7 +44,7 @@ public static function dump(PdoDb $db, ?string $table = null, bool $schemaOnly =
4344
$output[] = '';
4445

4546
if (!$dataOnly) {
46-
$schema = $dialect->dumpSchema($db, $table);
47+
$schema = $dialect->dumpSchema($db, $table, $dropTables);
4748
if ($schema !== '') {
4849
$output[] = $schema;
4950
$output[] = '';

src/cli/commands/DumpCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,15 @@ protected function dump(): int
7272
$schemaOnly = (bool)$this->getOption('schema-only', false);
7373
$dataOnly = (bool)$this->getOption('data-only', false);
7474
$output = $this->getOption('output');
75+
$dropTables = !(bool)$this->getOption('no-drop-tables', false);
7576

7677
if ($schemaOnly && $dataOnly) {
7778
return $this->showError('Cannot use --schema-only and --data-only together');
7879
}
7980

8081
try {
8182
$db = $this->getDb();
82-
$sql = DumpManager::dump($db, is_string($table) ? $table : null, $schemaOnly, $dataOnly);
83+
$sql = DumpManager::dump($db, is_string($table) ? $table : null, $schemaOnly, $dataOnly, $dropTables);
8384

8485
if (is_string($output) && $output !== '') {
8586
$written = file_put_contents($output, $sql);
@@ -151,6 +152,7 @@ protected function showHelp(): int
151152
echo " --schema-only Dump only schema (CREATE TABLE, indexes, etc.)\n";
152153
echo " --data-only Dump only data (INSERT statements)\n";
153154
echo " --output=<file> Write dump to file instead of stdout\n";
155+
echo " --no-drop-tables Do not add DROP TABLE IF EXISTS before CREATE TABLE\n";
154156
echo " --force Skip confirmation (for restore)\n\n";
155157
echo "Examples:\n";
156158
echo " pdodb dump --output=backup.sql\n";

src/dialects/DialectAbstract.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -991,7 +991,7 @@ public function changeUserPassword(string $username, string $newPassword, ?strin
991991
/**
992992
* {@inheritDoc}
993993
*/
994-
public function dumpSchema(\tommyknocker\pdodb\PdoDb $db, ?string $table = null): string
994+
public function dumpSchema(\tommyknocker\pdodb\PdoDb $db, ?string $table = null, bool $dropTables = true): string
995995
{
996996
throw new \tommyknocker\pdodb\exceptions\ResourceException(
997997
'Database dump is not supported for ' . $this->getDriverName()

src/dialects/DialectInterface.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1569,11 +1569,12 @@ public function changeUserPassword(string $username, string $newPassword, ?strin
15691569
*
15701570
* @param \tommyknocker\pdodb\PdoDb $db Database instance
15711571
* @param string|null $table Table name (null = all tables)
1572+
* @param bool $dropTables Whether to add DROP TABLE IF EXISTS before CREATE TABLE
15721573
*
15731574
* @return string SQL schema dump
15741575
* @throws \tommyknocker\pdodb\exceptions\ResourceException If operation fails or not supported
15751576
*/
1576-
public function dumpSchema(\tommyknocker\pdodb\PdoDb $db, ?string $table = null): string;
1577+
public function dumpSchema(\tommyknocker\pdodb\PdoDb $db, ?string $table = null, bool $dropTables = true): string;
15771578

15781579
/**
15791580
* Dump database data (INSERT statements).

src/dialects/mariadb/MariaDBDialect.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1286,7 +1286,7 @@ public function changeUserPassword(string $username, string $newPassword, ?strin
12861286
/**
12871287
* {@inheritDoc}
12881288
*/
1289-
public function dumpSchema(\tommyknocker\pdodb\PdoDb $db, ?string $table = null): string
1289+
public function dumpSchema(\tommyknocker\pdodb\PdoDb $db, ?string $table = null, bool $dropTables = true): string
12901290
{
12911291
$output = [];
12921292
$tables = [];
@@ -1311,6 +1311,11 @@ public function dumpSchema(\tommyknocker\pdodb\PdoDb $db, ?string $table = null)
13111311
continue;
13121312
}
13131313

1314+
if ($dropTables) {
1315+
$quotedTable = $this->quoteTable($tableName);
1316+
$output[] = "DROP TABLE IF EXISTS {$quotedTable};";
1317+
}
1318+
13141319
// Get CREATE TABLE statement (includes indexes in MariaDB)
13151320
try {
13161321
$createRows = $db->rawQuery('SHOW CREATE TABLE ' . $this->quoteTable($tableName));

src/dialects/mssql/MSSQLDialect.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1537,7 +1537,7 @@ public function changeUserPassword(string $username, string $newPassword, ?strin
15371537
/**
15381538
* {@inheritDoc}
15391539
*/
1540-
public function dumpSchema(\tommyknocker\pdodb\PdoDb $db, ?string $table = null): string
1540+
public function dumpSchema(\tommyknocker\pdodb\PdoDb $db, ?string $table = null, bool $dropTables = true): string
15411541
{
15421542
$output = [];
15431543
$tables = [];
@@ -1554,6 +1554,10 @@ public function dumpSchema(\tommyknocker\pdodb\PdoDb $db, ?string $table = null)
15541554
foreach ($tables as $tableName) {
15551555
// Build CREATE TABLE from INFORMATION_SCHEMA
15561556
$quotedTable = $this->quoteTable($tableName);
1557+
1558+
if ($dropTables) {
1559+
$output[] = "IF OBJECT_ID('{$quotedTable}', 'U') IS NOT NULL DROP TABLE {$quotedTable};";
1560+
}
15571561
$columns = $db->rawQuery(
15581562
'SELECT COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, NUMERIC_PRECISION, NUMERIC_SCALE, IS_NULLABLE, COLUMN_DEFAULT
15591563
FROM INFORMATION_SCHEMA.COLUMNS

src/dialects/mysql/MySQLDialect.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1328,7 +1328,7 @@ public function changeUserPassword(string $username, string $newPassword, ?strin
13281328
/**
13291329
* {@inheritDoc}
13301330
*/
1331-
public function dumpSchema(\tommyknocker\pdodb\PdoDb $db, ?string $table = null): string
1331+
public function dumpSchema(\tommyknocker\pdodb\PdoDb $db, ?string $table = null, bool $dropTables = true): string
13321332
{
13331333
$output = [];
13341334
$tables = [];
@@ -1353,6 +1353,11 @@ public function dumpSchema(\tommyknocker\pdodb\PdoDb $db, ?string $table = null)
13531353
continue;
13541354
}
13551355

1356+
if ($dropTables) {
1357+
$quotedTable = $this->quoteTable($tableName);
1358+
$output[] = "DROP TABLE IF EXISTS {$quotedTable};";
1359+
}
1360+
13561361
// Get CREATE TABLE statement (includes indexes in MySQL)
13571362
try {
13581363
$createRows = $db->rawQuery('SHOW CREATE TABLE ' . $this->quoteTable($tableName));

src/dialects/postgresql/PostgreSQLDialect.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1679,7 +1679,7 @@ public function changeUserPassword(string $username, string $newPassword, ?strin
16791679
/**
16801680
* {@inheritDoc}
16811681
*/
1682-
public function dumpSchema(\tommyknocker\pdodb\PdoDb $db, ?string $table = null): string
1682+
public function dumpSchema(\tommyknocker\pdodb\PdoDb $db, ?string $table = null, bool $dropTables = true): string
16831683
{
16841684
$output = [];
16851685
$tables = [];
@@ -1696,8 +1696,13 @@ public function dumpSchema(\tommyknocker\pdodb\PdoDb $db, ?string $table = null)
16961696
}
16971697

16981698
foreach ($tables as $tableName) {
1699-
// Build CREATE TABLE from information_schema
17001699
$quotedTable = $this->quoteTable($tableName);
1700+
1701+
if ($dropTables) {
1702+
$output[] = "DROP TABLE IF EXISTS {$quotedTable} CASCADE;";
1703+
}
1704+
1705+
// Build CREATE TABLE from information_schema
17011706
$columns = $db->rawQuery(
17021707
"SELECT column_name, data_type, character_maximum_length, numeric_precision, numeric_scale, is_nullable, column_default
17031708
FROM information_schema.columns

0 commit comments

Comments
 (0)