Skip to content

Commit

Permalink
New AbstractDatabase::confirm()
Browse files Browse the repository at this point in the history
To confirm destructive action in non-local environment
  • Loading branch information
PowerKiKi committed May 17, 2024
1 parent a7380df commit 381412b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"ext-fileinfo": "*",
"ext-json": "*",
"ext-pdo": "*",
"ext-readline": "*",
"cakephp/chronos": "^3.0.3",
"doctrine/dbal": "^3.6",
"doctrine/migrations": "^3.6",
Expand Down
5 changes: 3 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 37 additions & 1 deletion src/Service/AbstractDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ final public static function loadData(string $dumpFile): void
$mariadbArgs = self::getMariadbArgs();
$dumpFile = self::absolutePath($dumpFile);

self::confirm('DROP the entire database `' . self::getDatabaseName() . "` and load dump $dumpFile ?");

echo "loading dump $dumpFile...\n";
$database = self::getDatabaseName();

Expand All @@ -88,7 +90,7 @@ final public static function loadData(string $dumpFile): void
static::loadTestUsers();
}

private static function getDatabaseName(): string
protected static function getDatabaseName(): string
{
/** @var array<string,string> $dbConfig */
$dbConfig = _em()->getConnection()->getParams();
Expand Down Expand Up @@ -155,6 +157,8 @@ final public static function executeLocalCommand(string $command): void
*/
public static function loadTestData(): void
{
self::confirm('DROP the entire database `' . self::getDatabaseName() . '` and load test data ?');

self::executeLocalCommand(PHP_BINARY . ' ./bin/doctrine orm:schema-tool:drop --ansi --full-database --force');
self::executeLocalCommand(PHP_BINARY . ' ./bin/doctrine migrations:migrate --ansi --no-interaction');
static::loadTriggers();
Expand Down Expand Up @@ -209,4 +213,36 @@ private static function absolutePath(string $file): string

return $absolutePath;
}

/**
* Ask confirmation to user (y/n), unless if we are really sure that we are in local development,
* or if command argument `--no-interaction` is given.
*
* If the user does not confirm, the program will exist immediately.
*/
public static function confirm(string $question): void
{
global $argv;
$path = getcwd() ?: '';
if (preg_match('~^/sistes/[^/]+\.lan$~', $path) || in_array('--no-interaction', $argv, true)) {
return;
}

echo <<<STRING
⚠️ ⚠️ ⚠️ ⚠️ ⚠️ ⚠️ ⚠️ ⚠️ ⚠️ ⚠️ ⚠️ ⚠️
It looks like this is a\033[01;31m PRODUCTION SITE\033[0m: $path
$question (y)es (n)o
⚠️ ⚠️ ⚠️ ⚠️ ⚠️ ⚠️ ⚠️ ⚠️ ⚠️ ⚠️ ⚠️ ⚠️
STRING;

$confirm = readline() ?: '';
if (!preg_match('/^y(es)?$/', $confirm)) {
exit();
}
}
}

0 comments on commit 381412b

Please sign in to comment.