Skip to content

docs: updated docs to include php callables config as preferred method #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 4, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 78 additions & 6 deletions docs/schema-definition.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,81 @@
title: Dump Schema Definition
order: 2
---

# Dump Schema Definition

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

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.

This is the basic configuration that you'll receive after installing the package:
## Configuration Methods

There are two ways to configure your dump schema. For production applications using Laravel's config caching, the callable method is strongly recommended.

### Method 1: Using PHP Callables (Recommended for Production)

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:

```php
<?php

use BeyondCode\LaravelMaskedDumper\DumpSchema;
use App\Support\MaskedDump;

return [
/**
* Use a callable class to define your dump schema
* This method is compatible with Laravel's config caching
*/
'default' => [MaskedDump::class, 'define'],
];
```

Then create the referenced class:

```php
<?php

namespace App\Support;

use BeyondCode\LaravelMaskedDumper\DumpSchema;
use BeyondCode\LaravelMaskedDumper\TableDefinitions\TableDefinition;
use Faker\Generator as Faker;

class MaskedDump
{
public static function define()
{
return DumpSchema::define()
->allTables()
->table('users', function (TableDefinition $table) {
$table->replace('name', function (Faker $faker) {
return $faker->name;
});
$table->replace('email', function (Faker $faker) {
return $faker->safeEmail;
});
$table->mask('password');
})
->schemaOnly('failed_jobs')
->schemaOnly('password_reset_tokens');
}
}
```

### Method 2: Inline Definition

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:

```php
use BeyondCode\LaravelMaskedDumper\DumpSchema;
use BeyondCode\LaravelMaskedDumper\TableDefinitions\TableDefinition;
use Faker\Generator as Faker;

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

## Definiting which tables to dump
## Defining which tables to dump

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.
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:
Expand Down Expand Up @@ -116,22 +174,22 @@ When dumping your data, the dump will now contain a safe, randomly generated ema

## Optimizing large datasets

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

```php
return [
'default' => DumpSchema::define()
->allTables()
->table('users', function($table) {
return $table->outputInChunksOf(3);
->table('users', function($table) {
return $table->outputInChunksOf(3);
});
];
```

## Specifying the database connection to use

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

```php
Expand All @@ -155,3 +213,17 @@ return [
->schemaOnly('custom_table'),
];
```

When using the callable approach with multiple schemas, you can define separate classes for each schema:

```php
<?php

use App\Support\DefaultMaskedDump;
use App\Support\SqliteMaskedDump;

return [
'default' => [DefaultMaskedDump::class, 'define'],
'sqlite' => [SqliteMaskedDump::class, 'define'],
];
```