Skip to content

Instantly share code, notes, and snippets.

@a-h-abid
Created April 8, 2022 06:00
Show Gist options
  • Save a-h-abid/dc32b0900419bc6ea19c08a45d702f65 to your computer and use it in GitHub Desktop.
Save a-h-abid/dc32b0900419bc6ea19c08a45d702f65 to your computer and use it in GitHub Desktop.
refresh module db
<?php
namespace Tests\Database;
class DropAllTablesState
{
/**
* Indicates if all tables have been dropped
*
* @var bool
*/
public static $dropped = false;
}
<?php
namespace Tests\Database;
class RefreshMigrationSchemaState
{
/**
* Indicates if the migation schema has been created
*
* @var bool
*/
public static $created = false;
}
<?php
namespace Tests\Traits;
use App\Console\Kernel;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\RefreshDatabaseState;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Tests\Database\DropAllTablesState;
use Tests\Database\RefreshMigrationSchemaState;
use Tests\Database\RefreshModuleDatabaseState;
use Tests\Database\RefreshRootDatabaseState;
trait RefreshModuleDatabase
{
use RefreshDatabase;
/**
* Should migrate all database files, including root & modules.
*
* @var bool
*/
protected function shouldMigrateAllFiles()
{
return false;
}
/**
* Should migrate root database files. Not module ones.
*
* @var bool
*/
protected function shouldMigrateRootFiles()
{
return false;
}
/**
* List of modules to migrate
*
* @var array
*/
protected function migrateModules()
{
return [];
}
/**
* Refresh a conventional test database.
*
* @return void
*/
protected function refreshTestDatabase()
{
if (!RefreshDatabaseState::$migrated) {
$this->dropAllTables();
$this->refreshMigrationSchema();
if ($this->shouldMigrateAllFiles()) {
$this->refreshAllMigrationFiles();
} else {
$this->refreshRootMigrationFiles();
$this->refreshModuleMigrationFiles();
}
}
$this->beginDatabaseTransaction();
}
protected function dropAllTables()
{
if (DropAllTablesState::$dropped) {
return;
}
$this->artisan('db:wipe', [
'--drop-views' => true,
'--force' => true,
]);
$this->app[Kernel::class]->setArtisan(null);
DropAllTablesState::$dropped = true;
}
protected function refreshMigrationSchema()
{
if (RefreshMigrationSchemaState::$created) {
return;
}
$migrationTable = config('database.migrations');
if (Schema::hasTable($migrationTable)) {
DB::statement('TRUNCATE ' . $migrationTable);
} else {
$this->artisan('migrate:install');
$this->app[Kernel::class]->setArtisan(null);
}
RefreshMigrationSchemaState::$created = true;
}
protected function refreshAllMigrationFiles()
{
$this->artisan('migrate');
$this->app[Kernel::class]->setArtisan(null);
RefreshDatabaseState::$migrated = true;
}
protected function refreshRootMigrationFiles()
{
if (!$this->shouldMigrateRootFiles()
|| RefreshRootDatabaseState::$migrated
) {
return;
}
$this->artisan('migrate', [
'--path' => database_path('migrations'),
]);
$this->app[Kernel::class]->setArtisan(null);
RefreshRootDatabaseState::$migrated = true;
}
/**
* Refresh a conventional test database of modules.
*
* @return void
*/
protected function refreshModuleMigrationFiles()
{
foreach ($this->migrateModules() as $moduleName) {
if (!array_key_exists($moduleName, RefreshModuleDatabaseState::$modulesMigrated)) {
RefreshModuleDatabaseState::$modulesMigrated[$moduleName] = false;
}
if (!RefreshModuleDatabaseState::$modulesMigrated[$moduleName]) {
$this->artisan('module:migrate ' . $moduleName);
$this->app[Kernel::class]->setArtisan(null);
RefreshModuleDatabaseState::$modulesMigrated[$moduleName] = true;
}
}
}
}
<?php
namespace Tests\Database;
class RefreshModuleDatabaseState
{
/**
* Indicates if the test modules database has been migrated.
*
* Array Format: ModuleName => true/false,
*
* @var array
*/
public static $modulesMigrated = [];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment