Skip to content

Commit 171961f

Browse files
authored
Merge pull request #24 from kswilliames/main
docs: updated docs to include php callables config as preferred method
2 parents dc3df4d + ddc0f72 commit 171961f

File tree

1 file changed

+78
-6
lines changed

1 file changed

+78
-6
lines changed

docs/schema-definition.md

Lines changed: 78 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,81 @@
22
title: Dump Schema Definition
33
order: 2
44
---
5+
56
# Dump Schema Definition
67

78
Your database dump configuration takes place in the `config/masked-dump.php` file.
89

910
You can use the package's fluent API to define which tables should be dumped and which information should be replaced or masked during the dump process.
1011

11-
This is the basic configuration that you'll receive after installing the package:
12+
## Configuration Methods
13+
14+
There are two ways to configure your dump schema. For production applications using Laravel's config caching, the callable method is strongly recommended.
15+
16+
### Method 1: Using PHP Callables (Recommended for Production)
17+
18+
When using Laravel's config caching feature, the default inline configuration approach may cause serialization errors. To avoid this issue, use PHP callables in your configuration:
1219

1320
```php
21+
<?php
22+
23+
use BeyondCode\LaravelMaskedDumper\DumpSchema;
24+
use App\Support\MaskedDump;
25+
26+
return [
27+
/**
28+
* Use a callable class to define your dump schema
29+
* This method is compatible with Laravel's config caching
30+
*/
31+
'default' => [MaskedDump::class, 'define'],
32+
];
33+
```
34+
35+
Then create the referenced class:
36+
37+
```php
38+
<?php
39+
40+
namespace App\Support;
41+
42+
use BeyondCode\LaravelMaskedDumper\DumpSchema;
43+
use BeyondCode\LaravelMaskedDumper\TableDefinitions\TableDefinition;
44+
use Faker\Generator as Faker;
1445

46+
class MaskedDump
47+
{
48+
public static function define()
49+
{
50+
return DumpSchema::define()
51+
->allTables()
52+
->table('users', function (TableDefinition $table) {
53+
$table->replace('name', function (Faker $faker) {
54+
return $faker->name;
55+
});
56+
$table->replace('email', function (Faker $faker) {
57+
return $faker->safeEmail;
58+
});
59+
$table->mask('password');
60+
})
61+
->schemaOnly('failed_jobs')
62+
->schemaOnly('password_reset_tokens');
63+
}
64+
}
65+
```
66+
67+
### Method 2: Inline Definition
68+
69+
This is the basic configuration that you'll receive after installing the package. While simpler for development, this method is not compatible with Laravel's config caching:
70+
71+
```php
1572
use BeyondCode\LaravelMaskedDumper\DumpSchema;
1673
use BeyondCode\LaravelMaskedDumper\TableDefinitions\TableDefinition;
1774
use Faker\Generator as Faker;
1875

1976
return [
2077
/**
2178
* Use this dump schema definition to remove, replace or mask certain parts of your database tables.
79+
* NOTE: This approach is not compatible with Laravel's config caching.
2280
*/
2381
'default' => DumpSchema::define()
2482
->allTables()
@@ -34,7 +92,7 @@ return [
3492
];
3593
```
3694

37-
## Definiting which tables to dump
95+
## Defining which tables to dump
3896

3997
The dump configuration allows you to specify which tables you want to dump. The simplest form of dumping your database can be achieved by using the `allTables()` method.
4098
This ensures that all of your database tables will be represented in the dump. You can then go and customize how certain tables should be dumped:
@@ -116,22 +174,22 @@ When dumping your data, the dump will now contain a safe, randomly generated ema
116174

117175
## Optimizing large datasets
118176

119-
The method TableDefinition::outputInChunksOf(int $chunkSize) allows for chunked inserts for large datasets,
177+
The method TableDefinition::outputInChunksOf(int $chunkSize) allows for chunked inserts for large datasets,
120178
improving performance and reducing memory consumption during the dump process.
121179

122180
```php
123181
return [
124182
'default' => DumpSchema::define()
125183
->allTables()
126-
->table('users', function($table) {
127-
return $table->outputInChunksOf(3);
184+
->table('users', function($table) {
185+
return $table->outputInChunksOf(3);
128186
});
129187
];
130188
```
131189

132190
## Specifying the database connection to use
133191

134-
By default, this package will use your `default` database connection when dumping the tables.
192+
By default, this package will use your `default` database connection when dumping the tables.
135193
You can pass the connection to the `DumpSchema::define` method, in order to specify your own database connection string:
136194

137195
```php
@@ -155,3 +213,17 @@ return [
155213
->schemaOnly('custom_table'),
156214
];
157215
```
216+
217+
When using the callable approach with multiple schemas, you can define separate classes for each schema:
218+
219+
```php
220+
<?php
221+
222+
use App\Support\DefaultMaskedDump;
223+
use App\Support\SqliteMaskedDump;
224+
225+
return [
226+
'default' => [DefaultMaskedDump::class, 'define'],
227+
'sqlite' => [SqliteMaskedDump::class, 'define'],
228+
];
229+
```

0 commit comments

Comments
 (0)