laratusk/view-migrations

Manage database views via SQL files with automatic content-hash-based change detection

Maintainers

Package info

github.com/laratusk/view-migrations

pkg:composer/laratusk/view-migrations

Statistics

Installs: 31

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v0.2.0 2026-03-16 18:54 UTC

This package is auto-updated.

Last update: 2026-03-16 18:55:22 UTC


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

  1. You write plain SQL SELECT statements in database/views/*.sql
  2. The package computes a content hash for each file
  3. On view:migrate, it compares the file hash with the hash stored in the migrations table
  4. 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.