generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Interacts with SQL View in Migration
- Loading branch information
1 parent
027b360
commit fc5ecea
Showing
2 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
namespace CleaniqueCoders\Traitify\Concerns; | ||
|
||
use Illuminate\Support\Facades\DB; | ||
|
||
trait InteractsWithSqlViewMigration | ||
{ | ||
protected string $up_filename = 'create-sql-views.sql'; | ||
|
||
protected string $down_filename = 'drop-sql-views.sql'; | ||
|
||
public function up() | ||
{ | ||
$this->down(); | ||
$this->run( | ||
$this->getUpFilename() | ||
); | ||
} | ||
|
||
public function down() | ||
{ | ||
$this->run( | ||
$this->getDownFilename() | ||
); | ||
} | ||
|
||
protected function getUpFilename(): string | ||
{ | ||
return $this->up_filename; | ||
} | ||
|
||
protected function getDownFilename(): string | ||
{ | ||
return $this->down_filename; | ||
} | ||
|
||
protected function run($filename) | ||
{ | ||
$path = $this->getPath($filename); | ||
|
||
if (! file_exists($path)) { | ||
throw new \Exception("$path file not found."); | ||
} | ||
|
||
$content = file_get_contents($path); | ||
|
||
DB::unprepared($content); | ||
} | ||
|
||
protected function getPath($filename): string | ||
{ | ||
return $this->getStoragePath().DIRECTORY_SEPARATOR.$filename; | ||
} | ||
|
||
protected function getStoragePath(): string | ||
{ | ||
return database_path('sql'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Support\Facades\DB; | ||
use Illuminate\Support\Facades\File; | ||
|
||
// Your custom migration class | ||
class CreateTestViews extends Migration | ||
{ | ||
use CleaniqueCoders\Traitify\Concerns\InteractsWithSqlViewMigration; | ||
|
||
// Override the methods to provide custom filenames | ||
protected function getUpFilename(): string | ||
{ | ||
return 'test-create-views.sql'; | ||
} | ||
|
||
protected function getDownFilename(): string | ||
{ | ||
return 'test-drop-views.sql'; | ||
} | ||
} | ||
|
||
beforeEach(function () { | ||
// Create the 'database/sql' directory and add SQL files | ||
if (! is_dir(database_path('sql'))) { | ||
mkdir(database_path('sql'), 0755, true); | ||
} | ||
|
||
// Prepare SQL files with content | ||
file_put_contents(database_path('sql/test-create-views.sql'), 'CREATE VIEW test_view AS SELECT * FROM test_table;'); | ||
file_put_contents(database_path('sql/test-drop-views.sql'), 'DROP VIEW IF EXISTS test_view;'); | ||
}); | ||
|
||
afterEach(function () { | ||
// Clean up the SQL files after each test | ||
@unlink(database_path('sql/test-create-views.sql')); | ||
@unlink(database_path('sql/test-drop-views.sql')); | ||
@rmdir(database_path('sql')); | ||
}); | ||
|
||
it('runs the up migration with the correct SQL file', function () { | ||
// Mock both DROP VIEW and CREATE VIEW during the up method | ||
DB::shouldReceive('unprepared') | ||
->once() | ||
->with('DROP VIEW IF EXISTS test_view;'); // from the down() call | ||
|
||
DB::shouldReceive('unprepared') | ||
->once() | ||
->with('CREATE VIEW test_view AS SELECT * FROM test_table;'); // from the up() call | ||
|
||
$migration = new CreateTestViews; | ||
$migration->up(); // Should call both DROP VIEW and CREATE VIEW SQL | ||
}); | ||
|
||
it('runs the down migration with the correct SQL file', function () { | ||
// Mock only the DROP VIEW call during the down method | ||
DB::shouldReceive('unprepared') | ||
->once() | ||
->with('DROP VIEW IF EXISTS test_view;'); | ||
|
||
$migration = new CreateTestViews; | ||
$migration->down(); // Should call only the DROP VIEW SQL | ||
}); | ||
|
||
it('throws an exception if the SQL file is not found', function () { | ||
// Delete the up file to simulate file not found | ||
@unlink(database_path('sql/test-create-views.sql')); | ||
|
||
$migration = new CreateTestViews; | ||
|
||
// Expect an exception when the SQL file is not found | ||
$this->expectException(\Exception::class); | ||
$this->expectExceptionMessage(database_path('sql'.DIRECTORY_SEPARATOR.'test-create-views.sql').' file not found.'); | ||
|
||
$migration->up(); // Should throw exception | ||
}); |