Skip to content

Commit a9d74ba

Browse files
authored
Merge pull request #20 from beyondcode/updates
3.0.0
2 parents d13915c + 921c7b0 commit a9d74ba

19 files changed

+455
-219
lines changed

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
build
22
composer.lock
3-
docs
43
vendor
54
coverage
65
.phpunit.result.cache

composer.json

+5-6
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919
"php": "^7.3 || ^8.0",
2020
"doctrine/dbal": "^2.0|^3.0",
2121
"fakerphp/faker": "^1.13",
22-
"illuminate/console": "^7.0|^8.0|^9.0|^10.0",
23-
"illuminate/support": "^7.0|^8.0|^9.0|^10.0"
22+
"illuminate/console": "^7.0|^8.0|^9.0|^10.0 || ^11.0",
23+
"illuminate/support": "^7.0|^8.0|^9.0|^10.0 || ^11.0"
2424
},
2525
"require-dev": {
26-
"orchestra/testbench": "^6.12|^7.0|^8.0",
27-
"phpunit/phpunit": "^8.0 || ^9.0",
28-
"spatie/phpunit-snapshot-assertions": "^4.2"
26+
"orchestra/testbench": "^6.12|^7.0|^8.0 || ^9.0",
27+
"phpunit/phpunit": "^8.0 || ^9.0 || ^10.5",
28+
"spatie/phpunit-snapshot-assertions": "^4.2 || ^5.1"
2929
},
3030
"autoload": {
3131
"psr-4": {
@@ -40,7 +40,6 @@
4040
"scripts": {
4141
"test": "vendor/bin/phpunit",
4242
"test-coverage": "vendor/bin/phpunit --coverage-html coverage"
43-
4443
},
4544
"config": {
4645
"sort-packages": true

config/masked-dump.php

-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,4 @@
1919
});
2020
$table->mask('password');
2121
})
22-
->schemaOnly('failed_jobs')
23-
->schemaOnly('password_reset_tokens'),
2422
];

docs/_index.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
packageName: Laravel Masked DB Dump
3+
githubUrl: https://github.com/beyondcode/laravel-masked-db-dump
4+
---

docs/dumping-the-database.md

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
title: Dumping the Database
3+
order: 3
4+
---
5+
# Dumping the Database
6+
7+
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.
8+
The command expects one argument, which is the name of the output file to use.
9+
10+
```
11+
php artisan db:masked-dump output.sql
12+
```
13+
14+
Running this command, will use the `default` dump schema definition and write the resulting dump to a file called `output.sql`.
15+
16+
## Changing Definitions
17+
18+
In case that your configuration file contains multiple dump schema definitions, you can pass the definition to use to the command like this:
19+
20+
```
21+
php artisan db:masked-dump output.sql --definition=sqlite
22+
```
23+
24+
## GZip compression
25+
26+
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:
27+
28+
```
29+
php artisan db:masked-dump output.sql --gzip
30+
```
31+
32+
This will write the compressed output to a file called `output.sql.gz`.

docs/installation.md

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
title: Installation
3+
order: 1
4+
---
5+
# Installation
6+
7+
To install the Laravel Masked DB Dump package, you can use composer:
8+
9+
```
10+
composer require beyondcode/laravel-masked-db-dump
11+
```
12+
13+
Next, you should publish the package configuration file, so that you can configure your dump schema:
14+
15+
```
16+
php artisan vendor:publish --provider=BeyondCode\\LaravelMaskedDumper\\LaravelMaskedDumpServiceProvider
17+
```
18+
19+
This will create a new file called `masked-dump.php` in your config folder.

docs/schema-definition.md

+157
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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+
```

phpunit.xml.dist

+1-17
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,13 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phpunit bootstrap="vendor/autoload.php"
33
backupGlobals="false"
4-
backupStaticAttributes="false"
4+
backupStaticProperties="false"
55
colors="true"
6-
verbose="true"
7-
convertErrorsToExceptions="true"
8-
convertNoticesToExceptions="true"
9-
convertWarningsToExceptions="true"
106
processIsolation="false"
117
stopOnFailure="false">
128
<testsuites>
139
<testsuite name="BeyondCode Test Suite">
1410
<directory>tests</directory>
1511
</testsuite>
1612
</testsuites>
17-
<filter>
18-
<whitelist>
19-
<directory suffix=".php">src/</directory>
20-
</whitelist>
21-
</filter>
22-
<logging>
23-
<log type="tap" target="build/report.tap"/>
24-
<log type="junit" target="build/report.junit.xml"/>
25-
<log type="coverage-html" target="build/coverage" charset="UTF-8" yui="true" highlight="true"/>
26-
<log type="coverage-text" target="build/coverage.txt"/>
27-
<log type="coverage-clover" target="build/logs/clover.xml"/>
28-
</logging>
2913
</phpunit>

0 commit comments

Comments
 (0)