Skip to content

Commit 0229cd2

Browse files
committed
Exclude tables by @k2idev #9
1 parent e4e4354 commit 0229cd2

File tree

4 files changed

+95
-4
lines changed

4 files changed

+95
-4
lines changed

src/DumpSchema.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class DumpSchema
1616

1717
protected $loadAllTables = false;
1818
protected $customizedTables = [];
19+
protected $excludedTables = [];
1920

2021
public function __construct($connectionName = null)
2122
{
@@ -48,6 +49,13 @@ public function allTables()
4849
return $this;
4950
}
5051

52+
public function exclude(string $tableName)
53+
{
54+
$this->excludedTables[] = $tableName;
55+
56+
return $this;
57+
}
58+
5159
/**
5260
* @return \Illuminate\Database\Schema\Builder
5361
*/
@@ -158,9 +166,15 @@ public function load()
158166
$this->loadAvailableTables();
159167

160168
if ($this->loadAllTables) {
161-
$this->dumpTables = collect($this->availableTables)->mapWithKeys(function (Table $table) {
169+
$dumpTables = collect($this->availableTables)->mapWithKeys(function (Table $table) {
162170
return [$table->getName() => new TableDefinition($table)];
163-
})->toArray();
171+
});
172+
173+
$excluded = $this->excludedTables;
174+
$this->dumpTables = $dumpTables
175+
->filter(function ($table, $tableName) use ($excluded) {
176+
return !in_array($tableName, $excluded);
177+
})->toArray();
164178
}
165179

166180
foreach ($this->customizedTables as $tableName => $tableDefinition) {

src/LaravelMaskedDump.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace BeyondCode\LaravelMaskedDumper;
44

5-
use Doctrine\DBAL\Connection;
65
use Doctrine\DBAL\Schema\Schema;
76
use Illuminate\Console\OutputStyle;
87
use BeyondCode\LaravelMaskedDumper\TableDefinitions\TableDefinition;
@@ -125,7 +124,7 @@ protected function dumpTableData(TableDefinition $table)
125124
$row = $this->transformResultForInsert((array)$row, $table);
126125
$tableName = $table->getDoctrineTable()->getName();
127126

128-
$query .= "INSERT INTO `${tableName}` (`" . implode('`, `', array_keys($row)) . '`) VALUES ';
127+
$query .= "INSERT INTO `$tableName` (`" . implode('`, `', array_keys($row)) . '`) VALUES ';
129128
$query .= "(";
130129

131130
$firstColumn = true;

tests/DumperTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,31 @@ public function it_can_dump_certain_tables_as_schema_only()
171171

172172
$this->assertMatchesTextSnapshot(file_get_contents($outputFile));
173173
}
174+
175+
/** @test */
176+
public function it_does_remove_excluded_tables_from_allTables()
177+
{
178+
$this->loadLaravelMigrations();
179+
180+
DB::table('users')
181+
->insert([
182+
'name' => 'Marcel',
183+
'email' => 'marcel@beyondco.de',
184+
'password' => 'test',
185+
'created_at' => '2021-01-01 00:00:00',
186+
'updated_at' => '2021-01-01 00:00:00',
187+
]);
188+
189+
$outputFile = base_path('test.sql');
190+
191+
$this->app['config']['masked-dump.default'] = DumpSchema::define()
192+
->allTables()
193+
->exclude('users');
194+
195+
$this->artisan('db:masked-dump', [
196+
'output' => $outputFile
197+
]);
198+
199+
$this->assertMatchesTextSnapshot(file_get_contents($outputFile));
200+
}
174201
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
DROP TABLE IF EXISTS `cache`;
2+
CREATE TABLE cache ("key" VARCHAR(255) NOT NULL, value CLOB NOT NULL, expiration INTEGER NOT NULL);
3+
LOCK TABLES `cache` WRITE;
4+
ALTER TABLE `cache` DISABLE KEYS;
5+
ALTER TABLE `cache` ENABLE KEYS;
6+
UNLOCK TABLES;
7+
DROP TABLE IF EXISTS `cache_locks`;
8+
CREATE TABLE cache_locks ("key" VARCHAR(255) NOT NULL, owner VARCHAR(255) NOT NULL, expiration INTEGER NOT NULL);
9+
LOCK TABLES `cache_locks` WRITE;
10+
ALTER TABLE `cache_locks` DISABLE KEYS;
11+
ALTER TABLE `cache_locks` ENABLE KEYS;
12+
UNLOCK TABLES;
13+
DROP TABLE IF EXISTS `failed_jobs`;
14+
CREATE TABLE failed_jobs (id INTEGER NOT NULL, uuid VARCHAR(255) NOT NULL, connection CLOB NOT NULL, queue CLOB NOT NULL, payload CLOB NOT NULL, exception CLOB NOT NULL, failed_at DATETIME NOT NULL);
15+
LOCK TABLES `failed_jobs` WRITE;
16+
ALTER TABLE `failed_jobs` DISABLE KEYS;
17+
ALTER TABLE `failed_jobs` ENABLE KEYS;
18+
UNLOCK TABLES;
19+
DROP TABLE IF EXISTS `job_batches`;
20+
CREATE TABLE job_batches (id VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, total_jobs INTEGER NOT NULL, pending_jobs INTEGER NOT NULL, failed_jobs INTEGER NOT NULL, failed_job_ids CLOB NOT NULL, options CLOB NOT NULL, cancelled_at INTEGER NOT NULL, created_at INTEGER NOT NULL, finished_at INTEGER NOT NULL);
21+
LOCK TABLES `job_batches` WRITE;
22+
ALTER TABLE `job_batches` DISABLE KEYS;
23+
ALTER TABLE `job_batches` ENABLE KEYS;
24+
UNLOCK TABLES;
25+
DROP TABLE IF EXISTS `jobs`;
26+
CREATE TABLE jobs (id INTEGER NOT NULL, queue VARCHAR(255) NOT NULL, payload CLOB NOT NULL, attempts INTEGER NOT NULL, reserved_at INTEGER NOT NULL, available_at INTEGER NOT NULL, created_at INTEGER NOT NULL);
27+
LOCK TABLES `jobs` WRITE;
28+
ALTER TABLE `jobs` DISABLE KEYS;
29+
ALTER TABLE `jobs` ENABLE KEYS;
30+
UNLOCK TABLES;
31+
DROP TABLE IF EXISTS `migrations`;
32+
CREATE TABLE migrations (id INTEGER NOT NULL, migration VARCHAR(255) NOT NULL, batch INTEGER NOT NULL);
33+
LOCK TABLES `migrations` WRITE;
34+
ALTER TABLE `migrations` DISABLE KEYS;
35+
INSERT INTO `migrations` (`id`, `migration`, `batch`) VALUES ('1', '0001_01_01_000000_testbench_create_users_table', '1');
36+
INSERT INTO `migrations` (`id`, `migration`, `batch`) VALUES ('2', '0001_01_01_000001_testbench_create_cache_table', '1');
37+
INSERT INTO `migrations` (`id`, `migration`, `batch`) VALUES ('3', '0001_01_01_000002_testbench_create_jobs_table', '1');
38+
ALTER TABLE `migrations` ENABLE KEYS;
39+
UNLOCK TABLES;
40+
DROP TABLE IF EXISTS `password_reset_tokens`;
41+
CREATE TABLE password_reset_tokens (email VARCHAR(255) NOT NULL, token VARCHAR(255) NOT NULL, created_at DATETIME NOT NULL);
42+
LOCK TABLES `password_reset_tokens` WRITE;
43+
ALTER TABLE `password_reset_tokens` DISABLE KEYS;
44+
ALTER TABLE `password_reset_tokens` ENABLE KEYS;
45+
UNLOCK TABLES;
46+
DROP TABLE IF EXISTS `sessions`;
47+
CREATE TABLE sessions (id VARCHAR(255) NOT NULL, user_id INTEGER NOT NULL, ip_address VARCHAR(255) NOT NULL, user_agent CLOB NOT NULL, payload CLOB NOT NULL, last_activity INTEGER NOT NULL);
48+
LOCK TABLES `sessions` WRITE;
49+
ALTER TABLE `sessions` DISABLE KEYS;
50+
ALTER TABLE `sessions` ENABLE KEYS;
51+
UNLOCK TABLES;

0 commit comments

Comments
 (0)