laratusk/view-migrations
Composer 安装命令:
composer require laratusk/view-migrations
包简介
Manage database views via SQL files with automatic content-hash-based change detection
README 文档
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.
统计信息
- 总下载量: 483
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-03-15