Skip to content

Commit

Permalink
Merge pull request #20 from beyondcode/updates
Browse files Browse the repository at this point in the history
3.0.0
  • Loading branch information
mechelon authored Dec 9, 2024
2 parents d13915c + 921c7b0 commit a9d74ba
Show file tree
Hide file tree
Showing 19 changed files with 455 additions and 219 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
build
composer.lock
docs
vendor
coverage
.phpunit.result.cache
11 changes: 5 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
"php": "^7.3 || ^8.0",
"doctrine/dbal": "^2.0|^3.0",
"fakerphp/faker": "^1.13",
"illuminate/console": "^7.0|^8.0|^9.0|^10.0",
"illuminate/support": "^7.0|^8.0|^9.0|^10.0"
"illuminate/console": "^7.0|^8.0|^9.0|^10.0 || ^11.0",
"illuminate/support": "^7.0|^8.0|^9.0|^10.0 || ^11.0"
},
"require-dev": {
"orchestra/testbench": "^6.12|^7.0|^8.0",
"phpunit/phpunit": "^8.0 || ^9.0",
"spatie/phpunit-snapshot-assertions": "^4.2"
"orchestra/testbench": "^6.12|^7.0|^8.0 || ^9.0",
"phpunit/phpunit": "^8.0 || ^9.0 || ^10.5",
"spatie/phpunit-snapshot-assertions": "^4.2 || ^5.1"
},
"autoload": {
"psr-4": {
Expand All @@ -40,7 +40,6 @@
"scripts": {
"test": "vendor/bin/phpunit",
"test-coverage": "vendor/bin/phpunit --coverage-html coverage"

},
"config": {
"sort-packages": true
Expand Down
2 changes: 0 additions & 2 deletions config/masked-dump.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,4 @@
});
$table->mask('password');
})
->schemaOnly('failed_jobs')
->schemaOnly('password_reset_tokens'),
];
4 changes: 4 additions & 0 deletions docs/_index.md
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
---
32 changes: 32 additions & 0 deletions docs/dumping-the-database.md
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`.
19 changes: 19 additions & 0 deletions docs/installation.md
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.
157 changes: 157 additions & 0 deletions docs/schema-definition.md
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'),
];
```
18 changes: 1 addition & 17 deletions phpunit.xml.dist
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>
Loading

0 comments on commit a9d74ba

Please sign in to comment.