Track and respond to database schema changes in your Laravel application through a simple event system.
- Installation
- Quick Start
- Configuration
- Usage
- Contributing
- Credits
- License
- Security Vulnerabilities
- About Plank
You can install the package via composer:
composer require plank/laravel-schema-events
You can publish the config file with:
php artisan vendor:publish --tag="schema-events-config"
- Install the package
- Set up an event listener:
<?php
namespace App\Listeners;
use Plank\LaravelSchemaEvents\Events\TableCreated;
class LogTableCreation
{
public function handle(TableCreated $event)
{
\Log::info("Table {$event->table} was created with columns: " . implode(', ', $event->columns->toArray()));
}
}
- Register your listener in
EventServiceProvider.php
:
protected $listen = [
TableCreated::class => [
LogTableCreation::class,
],
];
The configuration file allows you to customize:
- Which migration events to listen for
- Which commands are tracked for different schema operations
return [
'listeners' => [
'ran' => MigrationRan::class,
'finished' => MigrationsFinished::class,
],
'commands' => [
'renamed_columns' => ['renameColumn'],
'dropped_columns' => ['dropColumn'],
'added_indexes' => [
'primary',
'unique',
'index',
'fulltext',
'spatialIndex',
],
// ... additional commands
],
];
The package provides four main events:
TableCreated
- Emitted when a new table is createdTableChanged
- Emitted when an existing table is modifiedTableDropped
- Emitted when a table is droppedTableRenamed
- Emitted when a table is renamed
Each event includes basic connection information:
connection
- The name of the database connectiondatabaseName
- The name of the databasedriverName
- The database driver being used
public readonly string $table;
public readonly Collection $columns; // Added columns
public readonly Collection $indexes; // Added indexes
public readonly Collection $foreignKeys; // Added foreign keys
public readonly string $table;
public readonly Collection $addedColumns;
public readonly Collection $droppedColumns;
public readonly Collection $renamedColumns; // Contains [from => x, to => y]
public readonly Collection $modifiedColumns;
public readonly Collection $addedIndexes;
public readonly Collection $droppedIndexes;
public readonly Collection $renamedIndexes; // Contains [from => x, to => y]
public readonly Collection $addedForeignKeys;
public readonly Collection $droppedForeignKeys;
public readonly string $table;
public readonly string $from;
public readonly string $to;
The event repository collects the schema events that occur during the migrations which can be retrieved after the MigrationsEnded
event is fired by the Migrator.
If your application wants to handle dispatching and flushing the events, you can set the schema-events.listeners.finished
listener to null
and listen to the MigrationsEnded
event in your application.
The schema event repository can be controlled using the SchemaEvents
facade.
Retrieve all schema events that were fired during the course of the migrations.
Retrieve all TableCreated
events that were fired during the course of the migrations.
Retrieve all TableChanged
events that were fired during the course of the migrations.
Retrieve all TableRenamed
events that were fired during the course of the migrations.
Retrieve all TableDropped
events that were fired during the course of the migrations.
Clear all schema events stored in the schema event repository.
Please see CONTRIBUTING for details.
The MIT License (MIT). Please see License File for more information.
If you discover a security vulnerability within siren, please send an e-mail to [email protected]. All security vulnerabilities will be promptly addressed.
Plank focuses on impactful solutions that deliver engaging experiences to our clients and their users. We're committed to innovation, inclusivity, and sustainability in the digital space. Learn more about our mission to improve the web.