-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from beyondcode/updates
3.0.0
- Loading branch information
Showing
19 changed files
with
455 additions
and
219 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
build | ||
composer.lock | ||
docs | ||
vendor | ||
coverage | ||
.phpunit.result.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,4 @@ | |
}); | ||
$table->mask('password'); | ||
}) | ||
->schemaOnly('failed_jobs') | ||
->schemaOnly('password_reset_tokens'), | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
packageName: Laravel Masked DB Dump | ||
githubUrl: https://github.com/beyondcode/laravel-masked-db-dump | ||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
--- | ||
title: Dumping the Database | ||
order: 3 | ||
--- | ||
# Dumping the Database | ||
|
||
After you have configured your dump schema, it's time to dump your tables. This can be done using the `db:masked-dump` artisan command. | ||
The command expects one argument, which is the name of the output file to use. | ||
|
||
``` | ||
php artisan db:masked-dump output.sql | ||
``` | ||
|
||
Running this command, will use the `default` dump schema definition and write the resulting dump to a file called `output.sql`. | ||
|
||
## Changing Definitions | ||
|
||
In case that your configuration file contains multiple dump schema definitions, you can pass the definition to use to the command like this: | ||
|
||
``` | ||
php artisan db:masked-dump output.sql --definition=sqlite | ||
``` | ||
|
||
## GZip compression | ||
|
||
The default output is a plain text file - depending on the size of your dump, you might want to enable GZip compression. This can be done by passing the `--gzip` flag to the command: | ||
|
||
``` | ||
php artisan db:masked-dump output.sql --gzip | ||
``` | ||
|
||
This will write the compressed output to a file called `output.sql.gz`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
--- | ||
title: Installation | ||
order: 1 | ||
--- | ||
# Installation | ||
|
||
To install the Laravel Masked DB Dump package, you can use composer: | ||
|
||
``` | ||
composer require beyondcode/laravel-masked-db-dump | ||
``` | ||
|
||
Next, you should publish the package configuration file, so that you can configure your dump schema: | ||
|
||
``` | ||
php artisan vendor:publish --provider=BeyondCode\\LaravelMaskedDumper\\LaravelMaskedDumpServiceProvider | ||
``` | ||
|
||
This will create a new file called `masked-dump.php` in your config folder. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
--- | ||
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: | ||
|
||
```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. | ||
*/ | ||
'default' => 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'); | ||
}), | ||
]; | ||
``` | ||
|
||
## Definiting 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: | ||
|
||
```php | ||
return [ | ||
'default' => DumpSchema::define() | ||
->allTables(), | ||
]; | ||
``` | ||
|
||
## Exclude specific tables from dumps | ||
|
||
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: | ||
|
||
```php | ||
return [ | ||
'default' => DumpSchema::define() | ||
->allTables() | ||
->exclude('password_resets'), | ||
]; | ||
``` | ||
|
||
## Masking table column content | ||
|
||
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: | ||
|
||
```php | ||
return [ | ||
'default' => DumpSchema::define() | ||
->table('users', function ($table) { | ||
$table->mask('password'); | ||
}) | ||
]; | ||
``` | ||
|
||
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: | ||
|
||
```php | ||
return [ | ||
'default' => DumpSchema::define() | ||
->table('users', function ($table) { | ||
$table->mask('password', '-'); | ||
}) | ||
]; | ||
``` | ||
|
||
## Replacing table column content | ||
|
||
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. | ||
|
||
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: | ||
|
||
```php | ||
return [ | ||
'default' => DumpSchema::define() | ||
->table('users', function ($table) { | ||
$table->replace('name', 'John Doe'); | ||
}) | ||
]; | ||
``` | ||
|
||
This configuration will dump all users and replace their name with "John Doe". | ||
|
||
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: | ||
|
||
```php | ||
return [ | ||
'default' => DumpSchema::define() | ||
->table('users', function (TableDefinition $table) { | ||
$table->replace('email', function (Faker $faker, $value) { | ||
return $faker->safeEmail; | ||
}); | ||
}) | ||
]; | ||
``` | ||
|
||
When dumping your data, the dump will now contain a safe, randomly generated email address for every user. | ||
|
||
## Optimizing 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); | ||
}); | ||
]; | ||
``` | ||
|
||
## Specifying the database connection to use | ||
|
||
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 | ||
return [ | ||
'default' => DumpSchema::define('sqlite') | ||
->allTables() | ||
]; | ||
``` | ||
|
||
## Multiple dump schemas | ||
|
||
You can define multiple database dump schemas in the `masked-dump.php` configuration file. | ||
The key in the configuration array is the identifier that will be used when you dump your tables: | ||
|
||
```php | ||
return [ | ||
'default' => DumpSchema::define() | ||
->allTables(), | ||
|
||
'sqlite' => DumpSchema::define('sqlite') | ||
->schemaOnly('custom_table'), | ||
]; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,13 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit bootstrap="vendor/autoload.php" | ||
backupGlobals="false" | ||
backupStaticAttributes="false" | ||
backupStaticProperties="false" | ||
colors="true" | ||
verbose="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnFailure="false"> | ||
<testsuites> | ||
<testsuite name="BeyondCode Test Suite"> | ||
<directory>tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
<filter> | ||
<whitelist> | ||
<directory suffix=".php">src/</directory> | ||
</whitelist> | ||
</filter> | ||
<logging> | ||
<log type="tap" target="build/report.tap"/> | ||
<log type="junit" target="build/report.junit.xml"/> | ||
<log type="coverage-html" target="build/coverage" charset="UTF-8" yui="true" highlight="true"/> | ||
<log type="coverage-text" target="build/coverage.txt"/> | ||
<log type="coverage-clover" target="build/logs/clover.xml"/> | ||
</logging> | ||
</phpunit> |
Oops, something went wrong.