@@ -44,11 +44,11 @@ First pull in the package using Composer:
4444composer require calebdw/laravel-sql-entities
4545```
4646
47- <!-- And then publish the package's configuration file: -->
48- <!-- -->
49- <!-- ```bash -->
50- <!-- php artisan vendor:publish --provider="CalebDW\SqlEntities\ServiceProvider" -->
51- <!-- ``` -->
47+ Optionally, publish the configuration file:
48+
49+ ``` bash
50+ php artisan vendor:publish --tag=sql-entities-config
51+ ```
5252
5353The package looks for SQL entities under ` database/entities/ ` so you might need to add
5454a namespace to your ` composer.json ` file, for example:
@@ -71,7 +71,18 @@ a namespace to your `composer.json` file, for example:
7171> base path. This means it should automatically work for a modular setup where
7272> the entities might be spread across multiple directories.
7373
74- <!-- ## Configuration -->
74+ ## Configuration
75+
76+ The package ships with a configuration file that controls automatic syncing behavior:
77+
78+ | Option | Default | Description |
79+ | ---| ---| ---|
80+ | ` sync ` | ` true ` | Automatically sync (refresh) entities whenever migrations run. |
81+ | ` drop_on_migrate ` | ` true ` | Drop all entities before migrations start and recreate them after. When ` false ` , entities are only refreshed after migrations finish. |
82+
83+ When ` drop_on_migrate ` is enabled, all entities are dropped before migrations begin to prevent failures caused by dependent schema changes (e.g., dropping a column that a view references). However, this means entities will be unavailable while migrations are running, which can be problematic if the application is still serving requests.
84+
85+ When disabled, entities are refreshed (using ` CREATE OR REPLACE ` ) after migrations finish. If a refresh fails due to a schema change, the entity is automatically dropped and recreated. For migrations that require specific entities to be absent, you can use the [ ` withoutEntities() ` ] ( #%EF%B8%8F-withoutentities ) method for more granular control.
7586
7687## 🛠️ Usage
7788
@@ -370,13 +381,15 @@ resolve('sql-entities')->create(new RecentOrdersView());
370381// Similarly, you can drop a single entity using the class or instance
371382SqlEntity::drop(RecentOrdersView::class);
372383
373- // Create or drop all entities
384+ // Create, drop, or refresh all entities
374385SqlEntity::createAll();
375386SqlEntity::dropAll();
387+ SqlEntity::refreshAll();
376388
377389// You can also filter by type or connection
378390SqlEntity::createAll(types: View::class, connections: 'reporting');
379391SqlEntity::dropAll(types: View::class, connections: 'reporting');
392+ SqlEntity::refreshAll(types: View::class, connections: 'reporting');
380393```
381394
382395#### ♻️ ` withoutEntities() `
@@ -434,31 +447,28 @@ php artisan sql-entities:create -c reporting
434447
435448# Similarly, drop all entities
436449php artisan sql-entities:drop
450+
451+ # Refresh all entities (attempts CREATE OR REPLACE, falls back to drop + create)
452+ php artisan sql-entities:refresh
437453```
438454
439- ### 🚀 Automatic syncing when migrating (Optional)
455+ ### 🚀 Automatic syncing when migrating
440456
441- You may want to automatically drop all SQL entities before migrating, and then
442- recreate them after the migrations are complete. This is helpful when the entities
443- depend on schema changes. To do this, register the built-in subscriber in a service provider:
457+ By default, SQL entities are automatically synced whenever migrations run.
458+ This is controlled by the ` sync ` config option and is enabled out of the box.
444459
445- ``` php
446- <?php
447- use CalebDW\SqlEntities\Listeners\SyncSqlEntities;
448- use Illuminate\Support\Facades\Event;
449- use Illuminate\Support\ServiceProvider;
460+ When ` drop_on_migrate ` is enabled (the default), all entities are dropped before
461+ migrations start and recreated after they finish. This prevents failures when
462+ migrations alter tables that entities depend on.
450463
451- class AppServiceProvider extends ServiceProvider
452- {
453- public function boot(): void
454- {
455- Event::subscribe(SyncSqlEntities::class);
456- }
457- }
458- ```
464+ When ` drop_on_migrate ` is disabled, entities are only refreshed after migrations
465+ finish. The refresh uses ` CREATE OR REPLACE ` where possible and falls back to
466+ dropping and recreating if that fails (e.g., when a view's columns have changed).
467+
468+ Entities are also refreshed when there are no pending migrations, ensuring any
469+ newly added or updated entities are always created.
459470
460- The listener will also create all entities if there's no pending migrations,
461- ensuring any new entities are created automatically.
471+ To disable automatic syncing entirely, set ` sync ` to ` false ` in the config.
462472
463473## 🤝 Contributing
464474
0 commit comments