Skip to content

Commit

Permalink
add excludeTables support for sqlite (#177)
Browse files Browse the repository at this point in the history
* add example DB

* add exclude support for sqlite

* cleanup

* Update .gitignore

* Fix styling

Co-authored-by: Freek Van der Herten <[email protected]>
Co-authored-by: freekmurze <[email protected]>
  • Loading branch information
3 people committed Sep 1, 2022
1 parent ba3e386 commit 129b825
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 5 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@ docs
vendor
.phpunit.result.cache
.php-cs-fixer.cache

21 changes: 19 additions & 2 deletions src/Databases/Sqlite.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Spatie\DbDumper\Databases;

use Spatie\DbDumper\DbDumper;
use SQLite3;
use Symfony\Component\Process\Process;

class Sqlite extends DbDumper
Expand All @@ -16,10 +17,26 @@ public function dumpToFile(string $dumpFile): void
$this->checkIfDumpWasSuccessFul($process, $dumpFile);
}

public function getDumpCommand(string $dumpFile): string
public function getDbTables(): array
{
$includeTables = rtrim(' '.implode(' ', $this->includeTables));
$db = new SQLite3($this->dbName);
$query = $db->query("SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%';");
$tables = [];
while ($table = $query->fetchArray(SQLITE3_ASSOC)) {
$tables[] = $table['name'];
}
$db->close();

return $tables;
}

public function getDumpCommand(string $dumpFile): string
{
$includeTables = rtrim(' ' . implode(' ', $this->includeTables));
if (empty($includeTables) && ! empty($this->excludeTables)) {
$tables = $this->getDbTables();
$includeTables = rtrim(' ' . implode(' ', array_diff($tables, $this->excludeTables)));
}
$dumpInSqlite = "echo 'BEGIN IMMEDIATE;\n.dump{$includeTables}'";
if ($this->isWindows()) {
$dumpInSqlite = "(echo BEGIN IMMEDIATE; & echo .dump{$includeTables})";
Expand Down
31 changes: 29 additions & 2 deletions tests/SqliteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,33 @@ public function it_can_generate_a_dump_command_with_only_specific_tables_include
$this->assertEquals($expected, $dumpCommand);
}

/** @test */
public function it_can_generate_a_dump_command_without_excluded_tables_included()
{
$dbPath = __DIR__ . '/stubs/testDB.sqlite';
$dumpCommand = Sqlite::create()
->setDbName($dbPath)
->excludeTables(['tb2', 'tb3'])
->getDumpCommand('dump.sql');

$expected = "echo 'BEGIN IMMEDIATE;\n.dump tb1 tb4' | 'sqlite3' --bail '{$dbPath}' > \"dump.sql\"";

$this->assertEquals($expected, $dumpCommand);
}

/** @test */
public function it_can_return_current_db_table_list()
{
$dbPath = __DIR__ . '/stubs/testDB.sqlite';
$dumpCommand = Sqlite::create()
->setDbName($dbPath)
->getDbTables();

$expected = ['tb1', 'tb2', 'tb3', 'tb4'];

$this->assertEquals($expected, $dumpCommand);
}

/** @test */
public function it_can_generate_a_dump_command_with_absolute_paths()
{
Expand Down Expand Up @@ -97,8 +124,8 @@ public function it_can_generate_a_dump_command_with_absolute_paths_having_space_
/** @test */
public function it_successfully_creates_a_backup()
{
$dbPath = __DIR__.'/stubs/database.sqlite';
$dbBackupPath = __DIR__.'/temp/backup.sql';
$dbPath = __DIR__ . '/stubs/database.sqlite';
$dbBackupPath = __DIR__ . '/temp/backup.sql';

Sqlite::create()
->setDbName($dbPath)
Expand Down
Binary file added tests/stubs/testDB.sqlite
Binary file not shown.

0 comments on commit 129b825

Please sign in to comment.