aldoggutierrez / laravel-schema-manager
Manage PostgreSQL schemas in Laravel applications
Fund package maintenance!
Aldoggutierrez
Installs: 38
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/aldoggutierrez/laravel-schema-manager
Requires
- php: ^8.2
- illuminate/contracts: ^10.0||^11.0||^12.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^2.9||^3.0
- laravel/pint: ^1.14
- nunomaduro/collision: ^7.0||^8.8
- orchestra/testbench: ^8.0||^9.0||^10.0
- pestphp/pest: ^2.0||^3.0||^4.0
- pestphp/pest-plugin-arch: ^2.0||^3.0||^4.0
- pestphp/pest-plugin-laravel: ^2.0||^3.0||^4.0
- phpstan/extension-installer: ^1.4
- phpstan/phpstan-deprecation-rules: ^1.1||^2.0
- phpstan/phpstan-phpunit: ^1.4||^2.0
README
Manage PostgreSQL schemas in Laravel applications with ease. Move tables between schemas while preserving foreign keys and relationships. Generate versioned schema migrations and dump the full database schema to a SQL file.
Installation
You can install the package via composer:
composer require aldoggutierrez/laravel-schema-manager
Configuration
Publish the configuration file:
php artisan vendor:publish --tag="schema-manager-config"
Available options in config/schema-manager.php:
return [ 'default_source_schema' => env('SCHEMA_MANAGER_SOURCE', 'external'), 'default_destination_schema' => env('SCHEMA_MANAGER_DESTINATION', 'public'), 'connection' => env('SCHEMA_MANAGER_CONNECTION', null), 'log_queries' => env('SCHEMA_MANAGER_LOG_QUERIES', false), ];
These are the contents of the published config file:
return [ /* * Default source schema when moving tables */ 'default_source_schema' => env('SCHEMA_MANAGER_SOURCE', 'external'), /* * Default destination schema when moving tables */ 'default_destination_schema' => env('SCHEMA_MANAGER_DESTINATION', 'public'), /* * Database connection to use (leave null to use default) */ 'connection' => env('SCHEMA_MANAGER_CONNECTION', null), /* * Enable query logging during operations */ 'log_queries' => env('SCHEMA_MANAGER_LOG_QUERIES', false), ];
Usage
Move a table between schemas
# Basic usage (uses config defaults) php artisan schema:move-table authorized_charges # Specify source and destination php artisan schema:move-table users --from=external --to=public # Preview changes without executing php artisan schema:move-table orders --dry-run # Skip confirmation prompt php artisan schema:move-table products --force
List tables in schemas
# List tables in default schema php artisan schema:list-tables # List tables in specific schema php artisan schema:list-tables external # List all schemas and their tables php artisan schema:list-tables --all
Generate a schema migration
Creates a versioned migration file that moves a table between schemas using
ALTER TABLE … SET SCHEMA. The generated migration also handles any sequences
attached to the table.
# Specify schemas explicitly php artisan make:schema-migration orders --from=external --to=public # Omit options to be prompted interactively php artisan make:schema-migration orders
The generated file is placed in database/migrations/ with a timestamped name
such as 2024_01_01_120000_move_orders_from_external_to_public.php. It contains
both up() and down() methods so the move is fully reversible.
Dump the database schema
Exports the PostgreSQL schema (DDL) for one or more schemas to a SQL file using
pg_dump. Useful for keeping a schema snapshot in version control and for
seeding fresh environments without running every historical migration.
Requires
pg_dumpto be installed and accessible on$PATH.
# Dump the default pgsql connection (uses search_path, falls back to public) php artisan schema:dump # Specify a custom connection php artisan schema:dump --database=tenant # Dump specific schemas php artisan schema:dump --schemas=billing,reports # Write to a custom path php artisan schema:dump --path=/tmp/schema.sql # Prune existing migration files after dumping php artisan schema:dump --prune
Options
| Option | Default | Description |
|---|---|---|
--database |
pgsql |
Laravel database connection to use |
--schemas |
connection search_path or public |
Comma-separated list of schemas to dump |
--path |
database/schema/{connection}-schema.sql |
Output file path |
--prune |
— | Delete all files in database/migrations/ after the dump |
The command writes two sections to the output file:
- Schema-only DDL (
pg_dump --schema-only) for all requested schemas. - Data rows from the
migrationstable so Laravel knows which migrations have already been run.
Features
✅ Move tables between PostgreSQL schemas ✅ Automatically handles foreign key constraints ✅ Preserves all relationships (ON UPDATE/DELETE rules) ✅ Cross-schema foreign key support ✅ Dry-run mode to preview changes ✅ Transaction-based for safety ✅ List tables and schemas ✅ Generate reversible schema migration files ✅ Dump the full schema (DDL + migrations data) to a SQL file
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.