|
| 1 | +--- |
| 2 | +title: Dump Schema Definition |
| 3 | +order: 2 |
| 4 | +--- |
| 5 | +# Dump Schema Definition |
| 6 | + |
| 7 | +Your database dump configuration takes place in the `config/masked-dump.php` file. |
| 8 | + |
| 9 | +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. |
| 10 | + |
| 11 | +This is the basic configuration that you'll receive after installing the package: |
| 12 | + |
| 13 | +```php |
| 14 | + |
| 15 | +use BeyondCode\LaravelMaskedDumper\DumpSchema; |
| 16 | +use BeyondCode\LaravelMaskedDumper\TableDefinitions\TableDefinition; |
| 17 | +use Faker\Generator as Faker; |
| 18 | + |
| 19 | +return [ |
| 20 | + /** |
| 21 | + * Use this dump schema definition to remove, replace or mask certain parts of your database tables. |
| 22 | + */ |
| 23 | + 'default' => DumpSchema::define() |
| 24 | + ->allTables() |
| 25 | + ->table('users', function (TableDefinition $table) { |
| 26 | + $table->replace('name', function (Faker $faker) { |
| 27 | + return $faker->name; |
| 28 | + }); |
| 29 | + $table->replace('email', function (Faker $faker) { |
| 30 | + return $faker->safeEmail; |
| 31 | + }); |
| 32 | + $table->mask('password'); |
| 33 | + }), |
| 34 | +]; |
| 35 | +``` |
| 36 | + |
| 37 | +## Definiting which tables to dump |
| 38 | + |
| 39 | +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. |
| 40 | +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: |
| 41 | + |
| 42 | +```php |
| 43 | +return [ |
| 44 | + 'default' => DumpSchema::define() |
| 45 | + ->allTables(), |
| 46 | +]; |
| 47 | +``` |
| 48 | + |
| 49 | +## Exclude specific tables from dumps |
| 50 | + |
| 51 | +The `exclude()` method allows you to exclude specific tables from the dump. This can be useful if you want to exclude certain tables from the dump: |
| 52 | + |
| 53 | +```php |
| 54 | +return [ |
| 55 | + 'default' => DumpSchema::define() |
| 56 | + ->allTables() |
| 57 | + ->exclude('password_resets'), |
| 58 | +]; |
| 59 | +``` |
| 60 | + |
| 61 | +## Masking table column content |
| 62 | + |
| 63 | +To mask the content of a given table column, you can use the `mask` method on a custom table definition. For example, let's mask the `password` column on our `users` table: |
| 64 | + |
| 65 | +```php |
| 66 | +return [ |
| 67 | + 'default' => DumpSchema::define() |
| 68 | + ->table('users', function ($table) { |
| 69 | + $table->mask('password'); |
| 70 | + }) |
| 71 | +]; |
| 72 | +``` |
| 73 | + |
| 74 | +By default, the data will be masked using the `x` character, but you can also specify your own custom masking character as a second parameter: |
| 75 | + |
| 76 | +```php |
| 77 | +return [ |
| 78 | + 'default' => DumpSchema::define() |
| 79 | + ->table('users', function ($table) { |
| 80 | + $table->mask('password', '-'); |
| 81 | + }) |
| 82 | +]; |
| 83 | +``` |
| 84 | + |
| 85 | +## Replacing table column content |
| 86 | + |
| 87 | +Instead of completely masking the content of a column, you can also replace the column content. The content can either be replaced with a static string, or you can make use of a callable and replace it with custom content - for example faker data. |
| 88 | + |
| 89 | +To replace a column with a static string, you can use the `replace` method and pass the string to use as a replacement as the second argument: |
| 90 | + |
| 91 | +```php |
| 92 | +return [ |
| 93 | + 'default' => DumpSchema::define() |
| 94 | + ->table('users', function ($table) { |
| 95 | + $table->replace('name', 'John Doe'); |
| 96 | + }) |
| 97 | +]; |
| 98 | +``` |
| 99 | + |
| 100 | +This configuration will dump all users and replace their name with "John Doe". |
| 101 | + |
| 102 | +To gain more flexibility over the replacement, you can pass a function as the second argument. This function receives a Faker instance, as well as the original value of the column: |
| 103 | + |
| 104 | +```php |
| 105 | +return [ |
| 106 | + 'default' => DumpSchema::define() |
| 107 | + ->table('users', function (TableDefinition $table) { |
| 108 | + $table->replace('email', function (Faker $faker, $value) { |
| 109 | + return $faker->safeEmail; |
| 110 | + }); |
| 111 | + }) |
| 112 | +]; |
| 113 | +``` |
| 114 | + |
| 115 | +When dumping your data, the dump will now contain a safe, randomly generated email address for every user. |
| 116 | + |
| 117 | +## Optimizing large datasets |
| 118 | + |
| 119 | +The method TableDefinition::outputInChunksOf(int $chunkSize) allows for chunked inserts for large datasets, |
| 120 | +improving performance and reducing memory consumption during the dump process. |
| 121 | + |
| 122 | +```php |
| 123 | +return [ |
| 124 | + 'default' => DumpSchema::define() |
| 125 | + ->allTables() |
| 126 | + ->table('users', function($table) { |
| 127 | + return $table->outputInChunksOf(3); |
| 128 | + }); |
| 129 | +]; |
| 130 | +``` |
| 131 | + |
| 132 | +## Specifying the database connection to use |
| 133 | + |
| 134 | +By default, this package will use your `default` database connection when dumping the tables. |
| 135 | +You can pass the connection to the `DumpSchema::define` method, in order to specify your own database connection string: |
| 136 | + |
| 137 | +```php |
| 138 | +return [ |
| 139 | + 'default' => DumpSchema::define('sqlite') |
| 140 | + ->allTables() |
| 141 | +]; |
| 142 | +``` |
| 143 | + |
| 144 | +## Multiple dump schemas |
| 145 | + |
| 146 | +You can define multiple database dump schemas in the `masked-dump.php` configuration file. |
| 147 | +The key in the configuration array is the identifier that will be used when you dump your tables: |
| 148 | + |
| 149 | +```php |
| 150 | +return [ |
| 151 | + 'default' => DumpSchema::define() |
| 152 | + ->allTables(), |
| 153 | + |
| 154 | + 'sqlite' => DumpSchema::define('sqlite') |
| 155 | + ->schemaOnly('custom_table'), |
| 156 | +]; |
| 157 | +``` |
0 commit comments