adly-nady/php-my-migration
Laravel package to generate migrations (and optional Eloquent models) from an existing MySQL database. Detects column types, keys, indexes, foreign keys, timestamps/soft deletes; supports batch processing, custom paths, overwrite, and connections.
Installation
composer require adly-nady/php-my-migration
Add the service provider to config/app.php:
'providers' => [
// ...
AdlyNady\MyMigration\MyMigrationServiceProvider::class,
],
Publish Config
php artisan vendor:publish --provider="AdlyNady\MyMigration\MyMigrationServiceProvider" --tag="config"
Configure config/mymigration.php (adjust database and migration_path as needed).
First Run
php artisan my:migration:generate
This scans your database and generates migration files for existing tables in database/migrations/.
Verify Output
Check the generated migrations in database/migrations/ and inspect the schema definitions.
config/mymigration.php – Adjust database_connection, migration_path, and ignore_tables.php artisan my:migration:generate – Generate migrations for existing tables.php artisan my:migration:sync – Sync existing tables with generated migrations (e.g., after manual edits).use AdlyNady\MyMigration\Facades\MyMigration;
$migration = MyMigration::generateForTable('users'); // Generate a single table's migration.
Scenario: You inherited a project with an existing MySQL database but no Laravel migrations.
php artisan my:migration:generate to auto-generate migrations for all tables.up() and down() methods) for accuracy.php artisan migrate to create the tables (if they don’t exist) or sync them.php artisan my:migration:generate
Generates migrations for all tables in the configured database.MyMigration::generateForTable('products')->save();
Useful for selective backfilling.php artisan my:migration:sync
This updates the database schema to match the migration file.Configure ignore_tables in config/mymigration.php to exclude tables like:
'ignore_tables' => [
'migrations',
'failed_jobs',
'sessions',
],
Override the default migration template by publishing and modifying:
php artisan vendor:publish --provider="AdlyNady\MyMigration\MyMigrationServiceProvider" --tag="templates"
Edit resources/views/vendor/mymigration/migration.stub.
Use with Laravel Schema Builder:
Generated migrations can be extended with Laravel’s schema methods (e.g., $table->foreignId()).
Schema::table('orders', function (Blueprint $table) {
$table->foreignId('user_id')->constrained();
});
Seeders: After generating migrations, create seeders for existing data:
public function run()
{
DB::table('users')->insert([
['name' => 'Admin', 'email' => 'admin@example.com'],
]);
}
php artisan my:migration:generate in your CI/CD script to keep migrations in sync with the database schema.
Example (GitHub Actions):
- name: Generate migrations
run: php artisan my:migration:generate
- name: Commit changes
run: git config --global user.name "CI Bot" && git config --global user.email "ci@example.com" && git add database/migrations && git commit -m "chore: sync migrations" && git push
Schema::hasTable() and Schema::hasColumn() in tests to verify migrations:
public function test_users_table_exists()
{
$this->assertTrue(Schema::hasTable('users'));
$this->assertTrue(Schema::hasColumn('users', 'email'));
}
Data Type Mismatches:
INT → integer(), VARCHAR(255) → string()). Tip: Manually review generated migrations for edge cases like ENUM or SET fields, which may not map perfectly.php artisan my:migration:sync to update the database.Foreign Key Constraints:
php artisan my:migration:sync --with-foreign-keys (if supported in future versions).Transaction Conflicts:
php artisan migrate after generating migrations for a large database may fail due to transaction size limits.config/database.php or run migrations in batches.Case Sensitivity:
lower_case_table_names in my.cnf if needed.Overwriting Existing Migrations:
php artisan my:migration:generate --dry-run to preview changes before overwriting.Enable Verbose Output:
php artisan my:migration:generate --verbose
Shows detailed SQL queries and table analysis.
Check Logs:
Inspect storage/logs/laravel.log for errors during generation or syncing.
Compare with Database: Use a tool like TablePlus or Adminer to manually verify the generated schema matches the database.
Handle Custom Storage Engines: If a table uses a non-InnoDB engine (e.g., MyISAM), the package may not generate accurate constraints.
$table->engine = 'MyISAM';
Partial Generation: Generate migrations for specific schemas only:
MyMigration::generateForTables(['users', 'products']);
Exclude Views/Procedures: The package focuses on tables. For views or stored procedures, use raw SQL or tools like Doctrine DBAL.
Version Control:
Commit generated migrations to version control, but document manual changes in a README.md or comment block.
Performance: For large databases, optimize by:
php artisan my:migration:generate in a maintenance mode:
php artisan down --secret=your-secret
php artisan my:migration:generate
php artisan up --secret=your-secret
--chunk (if supported in future versions) to process tables in batches.Extending Functionality:
AdlyNady\MyMigration\Contracts\TypeMapper and binding your implementation in the service provider.registering and registered events in the service provider to add logic before/after migration generation.Collaboration:
# Manually added: $table->unique('slug');).How can I help you explore Laravel packages today?