laratusk / view-migrations
Manage database views via SQL files with automatic content-hash-based change detection
Requires
- php: ^8.2
- illuminate/contracts: ^10.0|^11.0|^12.0|^13.0
- illuminate/database: ^10.0|^11.0|^12.0|^13.0
- illuminate/support: ^10.0|^11.0|^12.0|^13.0
- staudenmeir/laravel-migration-views: ^1.0
Requires (Dev)
- larastan/larastan: ^2.0|^3.0
- laravel/pint: ^1.18
- orchestra/testbench: ^8.0|^9.0|^10.0|^11.0
- pestphp/pest: ^3.0
- pestphp/pest-plugin-laravel: ^3.0
- rector/rector: ^2.0
README
Manage database views via plain SQL files with automatic change detection. The package computes a content hash for each view file and tracks it in Laravel's migrations table.
Supports MySQL, PostgreSQL, and SQLite.
Installation
composer require laratusk/view-migrations
Publish the config file:
php artisan vendor:publish --tag=view-migrations-config
How It Works
- You write plain SQL
SELECTstatements indatabase/views/*.sql - The package computes a content hash for each file
- On
view:migrate, it compares the file hash with the hash stored in the migrations table - Views are created, updated, or skipped accordingly
The hash is stored in Laravel's migrations table as vm:{view_name}:{hash}, keeping the tracking mechanism consistent across all database drivers.
Quick Start
Create a view file:
php artisan view:make user_stats
Edit database/views/user_stats.sql:
SELECT
user_id,
COUNT(*) as total_orders,
SUM(amount) as total_spent
FROM orders
GROUP BY user_id;
Apply it:
php artisan view:migrate
Commands
view:migrate
Syncs all SQL files to database views.
php artisan view:migrate
php artisan view:migrate --dry-run # Preview changes without executing
php artisan view:migrate --drop-orphans # Drop views with no matching SQL file
view:status
Shows the current state of all views.
php artisan view:status
+-------------+--------------+--------------+--------+
| View | File Hash | DB Hash | Status |
+-------------+--------------+--------------+--------+
| user_stats | a1b2c3d4e5f6 | 9f8e7d6c5b4a | Stale |
| daily_sales | c3d4e5f6a1b2 | c3d4e5f6a1b2 | OK |
| new_report | e5f6a1b2c3d4 | — | New |
| old_view | — | 7d6c5b4a9f8e | Orphan |
+-------------+--------------+--------------+--------+
view:make {name}
Creates a new SQL file in database/views/.
php artisan view:make analytics_summary
Usage in Migrations
You can trigger view migrations from Laravel migrations:
use Laratusk\ViewMigrations\Facades\ViewMigrator;
return new class extends Migration
{
public function up(): void
{
ViewMigrator::migrateView('analytics_summary');
}
public function down(): void
{
ViewMigrator::dropView('analytics_summary');
}
};
Configuration
// config/view-migrations.php
return [
// Directory where .sql view files are stored
'path' => database_path('views'),
// Only manage views matching this prefix (null = manage all)
'prefix' => null,
];
Events
| Event | Properties |
|---|---|
ViewCreated |
viewName, hash |
ViewUpdated |
viewName, oldHash, newHash |
ViewDropped |
viewName |
ViewMigrationCompleted |
result (ViewMigrationResult DTO) |
Testing
# SQLite (default)
vendor/bin/pest
# MySQL
DB_CONNECTION=mysql DB_DATABASE=testing DB_USERNAME=root DB_PASSWORD=secret vendor/bin/pest
# PostgreSQL
DB_CONNECTION=pgsql DB_DATABASE=testing DB_USERNAME=postgres DB_PASSWORD=secret vendor/bin/pest
License
MIT License. See LICENSE.md.