Skip to content

Commit

Permalink
Allow Snipe to seed the database after migrating it and auto-update o…
Browse files Browse the repository at this point in the history
…n seed file change (drfraker#26)

* Neatened up config file and added new settings for database seeding

* Neatened up SnipeClearCommand and removed error suppression

* Implemented database seeding capability when the configuration requires it

* Add automatic snapshot update when seeding enabled and seed files changed

* Fixing styling issue highlight from StyleCI
  • Loading branch information
da-n authored and drfraker committed Jul 15, 2019
1 parent c0a4f3c commit 2338c63
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 13 deletions.
26 changes: 24 additions & 2 deletions config/snipe.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
<?php

return [
'snapshot-location' => __DIR__.'/../snapshots/snipe_snapshot.sql',
'snipefile-location' => __DIR__.'/../snapshots/.snipe',

/*
|--------------------------------------------------------------------------
| Storage Locations
|--------------------------------------------------------------------------
| By default, SnipeMigrations will store snipe files and database snapshots
| in /vendor/drfraker/snipe-migrations/snapshots. If you would like to
| change the location of the files, update the paths below.
*/
'snapshot-location' => base_path('vendor/drfraker/snipe-migrations/snapshots/').'snipe_snapshot.sql',
'snipefile-location' => base_path('vendor/drfraker/snipe-migrations/snapshots/').'.snipe',

/*
|--------------------------------------------------------------------------
| Database Seeding
|--------------------------------------------------------------------------
| By default SnipeMigrations will refresh the database, run all migrations,
| and start each test with an empty database. If you would like to seed
| the database after refreshing it, enable the setting below. A custom
| class can be set, otherwise, the default DatabaseSeeder will run.
*/
'seed-database' => false,
'seed-class' => 'DatabaseSeeder',

];
41 changes: 33 additions & 8 deletions src/Snipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function importSnapshot()
return;
}

$this->migrationChanges()
$this->databaseFileChanges()
? $this->newSnapshot()
: $this->importDatabase();
}
Expand All @@ -36,20 +36,22 @@ protected function usingInMemoryDatabase()
}

/**
* Determine if there have been migration changes since the last time the snapshot was updated.
* Determine if there have been migration or (if enabled) seeder file changes since the last time the snapshot was updated.
*
* @return bool
*/
protected function migrationChanges()
protected function databaseFileChanges()
{
if (! SnipeDatabaseState::$checkedForMigrationChanges) {
$timeSum = $this->migrationFileTimeSum();
if (! SnipeDatabaseState::$checkedForDatabaseFileChanges) {
$timeSum = config('snipe.seed-database', false)
? $this->migrationFileTimeSum() + $this->seederFileTimeSum()
: $this->migrationFileTimeSum();

if ($hasChanges = $this->migrationFilesHaveChanged($timeSum)) {
if ($hasChanges = $this->databaseFilesHaveChanged($timeSum)) {
file_put_contents(config('snipe.snipefile-location'), $timeSum);
}

SnipeDatabaseState::$checkedForMigrationChanges = true;
SnipeDatabaseState::$checkedForDatabaseFileChanges = true;

return $hasChanges;
}
Expand All @@ -62,6 +64,13 @@ protected function newSnapshot()
{
Artisan::call('migrate:fresh');

// Seed the database if required
if (config('snipe.seed-database', false)) {
Artisan::call('db:seed', [
'--class' => config('snipe.seed-class', 'DatabaseSeeder'),
]);
}

$storageLocation = config('snipe.snapshot-location');

// Store a snapshot of the db after migrations run.
Expand All @@ -85,14 +94,30 @@ protected function migrationFileTimeSum()
})->sum();
}

/**
* Scan seeder files for sum of last modified times.
*
* @return int
*/
protected function seederFileTimeSum()
{
return collect([database_path('seeds')])
->map(function ($path) {
return collect(File::allFiles($path))
->sum(function ($file) {
return $file->getMTime();
});
})->sum();
}

/**
* Determine if any of the application's migration files have been updated since the last time a snapshot
* was created.
*
* @param $timeSum
* @return bool
*/
protected function migrationFilesHaveChanged($timeSum): bool
protected function databaseFilesHaveChanged($timeSum): bool
{
$snipeFile = config('snipe.snipefile-location');

Expand Down
26 changes: 24 additions & 2 deletions src/SnipeClearCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,33 @@

class SnipeClearCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'snipe:clear';

/**
* Execute the console command.
*
* @return void
*/
public function handle()
{
@unlink(config('snipe.snipefile-location'));
$this->info('Cleared snipe migration snapshot.');
$snipefile = config('snipe.snipefile-location');

if ($snipefile && file_exists($snipefile)) {
try {
unlink($snipefile);
$this->info('Cleared snipe migration snapshot.');
} catch (\Exception $exception) {
$this->warn("Could not delete snipe migration file: {$exception->getMessage()}");
}

return;
}

$this->info('No Snipe migration snapshot found (it may have been cleared already).');
}
}
2 changes: 1 addition & 1 deletion src/SnipeDatabaseState.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ class SnipeDatabaseState
{
public static $importedDatabase = false;

public static $checkedForMigrationChanges = false;
public static $checkedForDatabaseFileChanges = false;
}

0 comments on commit 2338c63

Please sign in to comment.