|
| 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 | + ->schemaOnly('failed_jobs') |
| 35 | + ->schemaOnly('password_resets'), |
| 36 | +]; |
| 37 | +``` |
| 38 | + |
| 39 | +## Definiting which tables to dump |
| 40 | + |
| 41 | +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. |
| 42 | +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: |
| 43 | + |
| 44 | +```php |
| 45 | +return [ |
| 46 | + 'default' => DumpSchema::define() |
| 47 | + ->allTables(), |
| 48 | +]; |
| 49 | +``` |
| 50 | + |
| 51 | +## Dumping table schemas only |
| 52 | + |
| 53 | +For certain tables, you do not need to dump the data, but only need the structure of the table itself - like a `password_reset` table. To instruct the masked dumper to only dump the schema, you may use the `schemaOnly` method: |
| 54 | + |
| 55 | +```php |
| 56 | +return [ |
| 57 | + 'default' => DumpSchema::define() |
| 58 | + ->allTables() |
| 59 | + ->schemaOnly('password_resets'), |
| 60 | +]; |
| 61 | +``` |
| 62 | + |
| 63 | +This configuration will dump all of your tables - but for the `password_resets` table, it will not create any `INSERT` statements and only dumps the schema of this table. |
| 64 | + |
| 65 | +## Masking table column content |
| 66 | + |
| 67 | +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: |
| 68 | + |
| 69 | +```php |
| 70 | +return [ |
| 71 | + 'default' => DumpSchema::define() |
| 72 | + ->table('users', function ($table) { |
| 73 | + $table->mask('password'); |
| 74 | + }) |
| 75 | +]; |
| 76 | +``` |
| 77 | + |
| 78 | +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: |
| 79 | + |
| 80 | +```php |
| 81 | +return [ |
| 82 | + 'default' => DumpSchema::define() |
| 83 | + ->table('users', function ($table) { |
| 84 | + $table->mask('password', '-'); |
| 85 | + }) |
| 86 | +]; |
| 87 | +``` |
| 88 | + |
| 89 | +## Replacing table column content |
| 90 | + |
| 91 | +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. |
| 92 | + |
| 93 | +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: |
| 94 | + |
| 95 | +```php |
| 96 | +return [ |
| 97 | + 'default' => DumpSchema::define() |
| 98 | + ->table('users', function ($table) { |
| 99 | + $table->replace('name', 'John Doe'); |
| 100 | + }) |
| 101 | +]; |
| 102 | +``` |
| 103 | + |
| 104 | +This configuration will dump all users and replace their name with "John Doe". |
| 105 | + |
| 106 | +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: |
| 107 | + |
| 108 | +```php |
| 109 | + |
| 110 | +return [ |
| 111 | + 'default' => DumpSchema::define() |
| 112 | + ->table('users', function (TableDefinition $table) { |
| 113 | + $table->replace('email', function (Faker $faker, $value) { |
| 114 | + return $faker->safeEmail; |
| 115 | + }); |
| 116 | + }) |
| 117 | +]; |
| 118 | +``` |
| 119 | + |
| 120 | +When dumping your data, the dump will now contain a safe, randomly generated email address for every user. |
| 121 | + |
| 122 | +## Specifying the database connection to use |
| 123 | + |
| 124 | +By default, this package will use your `default` database connection when dumping the tables. |
| 125 | +You can pass the connection to the `DumpSchema::define` method, in order to specify your own database connection string: |
| 126 | + |
| 127 | +```php |
| 128 | +return [ |
| 129 | + 'default' => DumpSchema::define('sqlite') |
| 130 | + ->allTables() |
| 131 | +]; |
| 132 | +``` |
| 133 | + |
| 134 | +## Multiple dump schemas |
| 135 | + |
| 136 | +You can define multiple database dump schemas in the `masked-dump.php` configuration file. |
| 137 | +The key in the configuration array is the identifier that will be used when you dump your tables: |
| 138 | + |
| 139 | +```php |
| 140 | +return [ |
| 141 | + 'default' => DumpSchema::define() |
| 142 | + ->allTables(), |
| 143 | + |
| 144 | + 'sqlite' => DumpSchema::define('sqlite') |
| 145 | + ->schemaOnly('custom_table'), |
| 146 | +]; |
| 147 | +``` |
0 commit comments