Skip to content

Commit

Permalink
Added Interacts with SQL View in Migration
Browse files Browse the repository at this point in the history
  • Loading branch information
nasrulhazim committed Oct 16, 2024
1 parent 027b360 commit fc5ecea
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 0 deletions.
60 changes: 60 additions & 0 deletions src/Concerns/InteractsWithSqlViewMigration.php
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');
}
}
77 changes: 77 additions & 0 deletions tests/SqlViewTest.php
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
});

0 comments on commit fc5ecea

Please sign in to comment.