Skip to content
Permalink
Browse files

added the ability for dry runs!

  • Loading branch information...
OwenMelbz committed Jun 25, 2018
1 parent 6520a30 commit edeef1ccb806e6b60a9cfd91b2ae8d3cbf8cbc1a
Showing with 82 additions and 5 deletions.
  1. +21 βˆ’1 README.md
  2. +3 βˆ’3 app/Commands/ForgetMeNowCommand.php
  3. +14 βˆ’1 app/Services/ForgetDbService.php
  4. +44 βˆ’0 app/Table.php
@@ -78,11 +78,31 @@ Recently we introduced the ability to add modifiers to your column definitions,

Once you have your config complete you can run `forget-db forget ./path/to/config.yml` the wizard will ask for your connection details, and then will start the cleanse.

If forget-db finds a .env file within your current working directory, it will try to populate the default options with what is within it. Typically Laravel style connections work out of the box!

After its completed you should get a notification to let you know!

## Dry-run

We do not have a full dry-run system, however you can preview the query that selects your data set, and a table of the data that it has found, simply specify the `--dry` arg after your command e.g `forget-db forget ./config --dry` - this will only run "selects" on your database and will not write any changes! You will see something similar to:

```
🧠 forget-db :: 2 rows found to process.
🧠 forget-db :: Query run... select `users`.`id`, `users`.`email`, `users`.`password` from `users`
+----+-------------------------+------------------+
| id | email | password |
+----+-------------------------+------------------+
| 1 | arvel.bradtke@auer.com | 371817583255573 |
| 2 | remington54@volkman.org | 6011543368953199 |
+----+-------------------------+------------------+
```

> Warning - When doing a dry run, remember that it will output to your terminal, so if you are exposing sensitive data make sure you're taking the correct precautions!
# Warnings / Notes
- Due to syntax and Laravel requirements this must be run via a php 7.1 binary
- There is no dry run, I recommend you test this on a temporary database first.
- There is no full dry run, I recommend you test this on a temporary database first or test your conditions using the --dry arg
- Not all faker field types are yet supported, e.g `date($format = 'Y-m-d', $max = 'now')`
- The system that is running the tool, must have a connection to the database server.
- Due to trying to keep optimial server compatibility, updates are not done in bulk, but are done one at a time, so make sure you're aware of any row/table locking on your server.
@@ -20,7 +20,7 @@ class ForgetMeNowCommand extends Command
*
* @var string
*/
protected $signature = 'forget { config : Path to a config file }';
protected $signature = 'forget { config : Path to a config file } { --dry : Will show you the queries and the results but will not do the replacements }';
/**
* The description of the command.
@@ -46,7 +46,7 @@ public function handle(): void
try {
$config = UtilityService::parseConfig($configPath);
$forgetdb = new ForgetDbService($config);
$forgetdb = new ForgetDbService($config, $this->option('dry'));
} catch (\Exception $e) {
$this->notify('Whoops', 'Looks like something didn\'t go to plan...');
$this->fail($e->getMessage());
@@ -78,7 +78,7 @@ public function handle(): void
$this->message('database connection established, we have lift off! πŸš€');
if (!$this->confirm('Are you ready to start? This is your last chance to bail πŸ’¦', !config('app.production'))) {
if (!$this->confirm('Are you ready to start? This is your last chance to bail, you can always try out --dry first! πŸ’¦', !config('app.production'))) {
$this->notify('Whoops', 'Bailing! πŸ’¦πŸ’¦πŸ’¦');
$this->fail('Bailing! πŸ’¦πŸ’¦πŸ’¦');
exit(0);
@@ -38,14 +38,22 @@ class ForgetDbService
*/
protected $tables;
/**
* Defines if we're running in dry-run mode
*
* @var boolean
*/
protected $dry = false;
/**
* ForgetDbService constructor.
*
* @param array $config
* @throws Exception
*/
public function __construct(array $config)
public function __construct(array $config, bool $dry = false)
{
$this->dry = $dry;
$this->config = $config;
$this->tables = $this->generateTablesFromConfig($config);
}
@@ -68,6 +76,11 @@ public function forget(Command $messenger): void
$this->messenger = $messenger;
foreach ($this->getTables() as $table) {
if ($this->dry) {
$table->preview($this->messenger);
continue;
}
$table->forget($this->messenger);
}
}
@@ -106,6 +106,50 @@ public function forget(Command $messenger): void
);
}
/**
* This is the preview function which shows which rows will be affected
*
* @param Command $messenger
* @throws Exception
*/
public function preview(Command $messenger): void
{
$this->setMessenger($messenger);
DB::enableQueryLog();
$rows = $this->getRows();
$queryLog = DB::getQueryLog();
if ($query = end($queryLog)) {
$messenger->message('Query run... ' . $query['query']);
}
$maxRowsBeforePrompt = 50;
if ($rows->count() > $maxRowsBeforePrompt) {
if (!$messenger->confirm('There are more than ' . $maxRowsBeforePrompt . ' results, do you want to display them all? It might crash... πŸ’₯ ', 'yes')) {
$messenger->message($this->name . ' Done!');
return;
}
}
if ($rows->count() === 0) {
$headers = [$this->getPrefixedPrimaryKey()] + $this->getColumnNames()->toArray();
$table = [];
} else {
$firstRow = (array) current($rows->toArray());
$headers = array_keys($firstRow);
$table = $rows->map(function ($row) {
return (array) $row;
})->toArray();
}
$messenger->info(''); // just a blank line for readability :)
$messenger->table($headers, $table);
$messenger->info('');
}
/**
* Here we take a Collection of transformed rows
* to insert back into the database based

0 comments on commit edeef1c

Please sign in to comment.
You can’t perform that action at this time.