PHPackages                             maheralyamany/laravel-advanced-migration - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. maheralyamany/laravel-advanced-migration

ActiveLibrary[Framework](/categories/framework)

maheralyamany/laravel-advanced-migration
========================================

Generate migrations from existing database structures

v1.1.3(4mo ago)12MITPHPPHP ^7.4|^8.0|^8.1|^8.2|^8.3|^8.4

Since Nov 24Pushed 4mo agoCompare

[ Source](https://github.com/maheralyamany/laravel-advanced-migration)[ Packagist](https://packagist.org/packages/maheralyamany/laravel-advanced-migration)[ RSS](/packages/maheralyamany-laravel-advanced-migration/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (7)Versions (5)Used By (0)

Laravel Advanced Migration
==========================

[](#laravel-advanced-migration)

 [![Latest Version](https://camo.githubusercontent.com/88ab8e29854168aba50d39bf07b50ce14658eb141ca49894f4e3707d5ee74807/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d61686572616c79616d616e792f6c61726176656c2d616476616e6365642d6d6967726174696f6e)](https://camo.githubusercontent.com/88ab8e29854168aba50d39bf07b50ce14658eb141ca49894f4e3707d5ee74807/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d61686572616c79616d616e792f6c61726176656c2d616476616e6365642d6d6967726174696f6e) [![Total Downloads](https://camo.githubusercontent.com/28659e0f287735e7f8d0342e19b31141c006fa615a8dc104559d0d4e9dfd5967/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d61686572616c79616d616e792f6c61726176656c2d616476616e6365642d6d6967726174696f6e)](https://camo.githubusercontent.com/28659e0f287735e7f8d0342e19b31141c006fa615a8dc104559d0d4e9dfd5967/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d61686572616c79616d616e792f6c61726176656c2d616476616e6365642d6d6967726174696f6e) [![License](https://camo.githubusercontent.com/10f29c3c633381d572b49125c54baa6c01b04e5e9c6f62a2768e51becf2c3b9a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6d61686572616c79616d616e792f6c61726176656c2d616476616e6365642d6d6967726174696f6e)](https://camo.githubusercontent.com/10f29c3c633381d572b49125c54baa6c01b04e5e9c6f62a2768e51becf2c3b9a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6d61686572616c79616d616e792f6c61726176656c2d616476616e6365642d6d6967726174696f6e) [![Stars](https://camo.githubusercontent.com/300a104696ac9e7731d1939ab0bc0ba4a954fda19688537cbf1d65eb017573c4/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73746172732f6d61686572616c79616d616e792f6c61726176656c2d616476616e6365642d6d6967726174696f6e)](https://camo.githubusercontent.com/300a104696ac9e7731d1939ab0bc0ba4a954fda19688537cbf1d65eb017573c4/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73746172732f6d61686572616c79616d616e792f6c61726176656c2d616476616e6365642d6d6967726174696f6e)

Generate migrations from existing database structures, an alternative to the schema dump provided by Laravel. A primary use case for this package would be a project that has many migrations that alter tables using `-&gt;change()` from doctrine/dbal that SQLite doesn't support and need a way to get table structures updated for SQLite to use in tests. Another use case would be taking a project with a database and no migrations and turning that database into base migrations.

📋 Requirements
--------------

[](#-requirements)

- PHP ^7.4 or higher
- Laravel 9.x or higher
- Composer

🚀 Installation
--------------

[](#-installation)

```
composer require maheralyamany/laravel-advanced-migration --dev
```

### Publish Configuration

[](#publish-configuration)

```
php artisan vendor:publish --provider="AdvancedMigration\MigrationGeneratorProvider"
```

Copy config file from `vendor/maheralyamany/laravel-advanced-migration/config` to your Lumen config folder

Register service provider in bootstrap/app.php

```
$app->register(\AdvancedMigration\MigrationGeneratorProvider::class);
```

📖 Usage
-------

[](#-usage)

Whenever you have database changes or are ready to squash your database structure down to migrations, run:

```
php artisan generate:advanced-migrations
```

By default, the migrations will be created in `tests/database/migrations`. You can specify a different path with the `--path` option:

```
php artisan generate:advanced-migrations --path=database/migrations
```

You can specify the connection to use as the database with the `--connection` option:

```
php artisan generate:advanced-migrations --connection=mysql2
```

You can also clear the directory with the `--empty-path` option:

```
php artisan generate:advanced-migrations --empty-path
```

This command can also be run by setting the `LMG_RUN_AFTER_MIGRATIONS` environment variable to `true` and running your migrations as normal. This will latch into the `MigrationsEnded` event and run this command using the default options specified via your environment variables. Note: it will only run when your app environment is set to `local`.

Configuration
=============

[](#configuration)

Want to customize the migration stubs? Make sure you've published the vendor assets with the artisan command to publish vendor files above.

Environment Variables
---------------------

[](#environment-variables)

KeyDefault ValueAllowed ValuesDescriptionLMG\_RUN\_AFTER\_MIGRATIONSfalsebooleanWhether or not the migration generator should run after migrations have completed.LMG\_CLEAR\_OUTPUT\_PATHfalsebooleanWhether or not to clear out the output path before creating new files. Same as specifying `--empty-path` on the commandLMG\_TABLE\_NAMING\_SCHEME`[Timestamp]_create_[TableName]_table.php`stringThe string to be used to name table migration filesLMG\_VIEW\_NAMING\_SCHEME`[Timestamp]_create_[ViewName]_view.php`stringThe string to be used to name view migration filesLMG\_OUTPUT\_PATHtests/database/migrationsstringThe path (relative to the root of your project) to where the files will be output to. Same as specifying `--path=` on the commandLMG\_SKIPPABLE\_TABLESmigrationscomma delimited stringThe tables to be skippedLMG\_SKIP\_VIEWSfalsebooleanWhen true, skip all viewsLMG\_SKIPPABLE\_VIEWS''comma delimited stringThe views to be skippedLMG\_SORT\_MODE'foreign\_key'stringThe sorting mode to be used. Options: `foreign_key`LMG\_PREFER\_UNSIGNED\_PREFIXtruebooleanWhen true, uses `unsigned` variant methods instead of the `->unsigned()` modifier.LMG\_USE\_DEFINED\_INDEX\_NAMEStruebooleanWhen true, uses index names defined by the database as the name parameter for index methodsLMG\_USE\_DEFINED\_FOREIGN\_KEY\_INDEX\_NAMEStruebooleanWhen true, uses foreign key index names defined by the database as the name parameter for foreign key methodsLMG\_USE\_DEFINED\_UNIQUE\_KEY\_INDEX\_NAMEStruebooleanWhen true, uses unique key index names defined by the database as the name parameter for the `unique` methodsLMG\_USE\_DEFINED\_PRIMARY\_KEY\_INDEX\_NAMEStruebooleanWhen true, uses primary key index name defined by the database as the name parameter for the `primary` methodLMG\_WITH\_COMMENTStruebooleanWhen true, export comment using `->comment()` method.LMG\_USE\_DEFINED\_DATATYPE\_ON\_TIMESTAMPfalsebooleanWhen false, uses `->timestamps()` by mashing up `created_at` and `updated_at` regardless of datatype defined by the databaseLMG\_MYSQL\_TABLE\_NAMING\_SCHEMEnull?booleanWhen not null, this setting will override LMG\_TABLE\_NAMING\_SCHEME when the database driver is `mysql`.LMG\_MYSQL\_VIEW\_NAMING\_SCHEMEnull?booleanWhen not null, this setting will override LMG\_VIEW\_NAMING\_SCHEME when the database driver is `mysql`.LMG\_MYSQL\_OUTPUT\_PATHnull?booleanWhen not null, this setting will override LMG\_OUTPUT\_PATH when the database driver is `mysql`.LMG\_MYSQL\_SKIPPABLE\_TABLESnull?booleanWhen not null, this setting will override LMG\_SKIPPABLE\_TABLES when the database driver is `mysql`.LMG\_MYSQL\_SKIPPABLE\_VIEWSnullcomma delimited stringThe views to be skipped when driver is `mysql`LMG\_SQLITE\_TABLE\_NAMING\_SCHEMEnull?booleanWhen not null, this setting will override LMG\_TABLE\_NAMING\_SCHEME when the database driver is `sqlite`.LMG\_SQLITE\_VIEW\_NAMING\_SCHEMEnull?booleanWhen not null, this setting will override LMG\_VIEW\_NAMING\_SCHEME when the database driver is `sqlite`.LMG\_SQLITE\_OUTPUT\_PATHnull?booleanWhen not null, this setting will override LMG\_OUTPUT\_PATH when the database driver is `sqlite`.LMG\_SQLITE\_SKIPPABLE\_TABLESnull?booleanWhen not null, this setting will override LMG\_SKIPPABLE\_TABLES when the database driver is `sqlite`.LMG\_SQLITE\_SKIPPABLE\_VIEWSnullcomma delimited stringThe views to be skipped when driver is `sqlite`LMG\_PGSQL\_TABLE\_NAMING\_SCHEMEnull?booleanWhen not null, this setting will override LMG\_TABLE\_NAMING\_SCHEME when the database driver is `pgsql`.LMG\_PGSQL\_VIEW\_NAMING\_SCHEMEnull?booleanWhen not null, this setting will override LMG\_VIEW\_NAMING\_SCHEME when the database driver is `pgsql`.LMG\_PGSQL\_OUTPUT\_PATHnull?booleanWhen not null, this setting will override LMG\_OUTPUT\_PATH when the database driver is `pgsql`.LMG\_PGSQL\_SKIPPABLE\_TABLESnull?booleanWhen not null, this setting will override LMG\_SKIPPABLE\_TABLES when the database driver is `pgsql`.LMG\_PGSQL\_SKIPPABLE\_VIEWSnullcomma delimited stringThe views to be skipped when driver is `pgsql`LMG\_SQLSRV\_TABLE\_NAMING\_SCHEMEnull?booleanWhen not null, this setting will override LMG\_TABLE\_NAMING\_SCHEME when the database driver is `sqlsrc`.LMG\_SQLSRV\_VIEW\_NAMING\_SCHEMEnull?booleanWhen not null, this setting will override LMG\_VIEW\_NAMING\_SCHEME when the database driver is `sqlsrv`.LMG\_SQLSRV\_OUTPUT\_PATHnull?booleanWhen not null, this setting will override LMG\_OUTPUT\_PATH when the database driver is `sqlsrv`.LMG\_SQLSRV\_SKIPPABLE\_TABLESnull?booleanWhen not null, this setting will override LMG\_SKIPPABLE\_TABLES when the database driver is `sqlsrv`.LMG\_SQLSRV\_SKIPPABLE\_VIEWSnullcomma delimited stringThe views to be skipped when driver is `sqlsrv`Stubs
-----

[](#stubs)

There is a default stub for tables and views, found in `resources/stubs/vendor/advanced-migration-generator/`. Each database driver can be assigned a specific migration stub by creating a new stub file in `resources/stubs/vendor/advanced-migration-generator/` with a `driver`-prefix, e.g. `mysql-table.stub` for a MySQL specific table stub.

Stub Naming
-----------

[](#stub-naming)

Table and view stubs can be named using the `LMG_(TABLE|VIEW)_NAMING_SCHEME` environment variables. Optionally, driver-specific naming schemes can be used as well by specifying `LMG_{driver}_TABLE_NAMING_SCHEME` environment vars using the same tokens. See below for available tokens that can be replaced.

### Table Name Stub Tokens

[](#table-name-stub-tokens)

Table stubs have the following tokens available for the naming scheme:

TokenExampleDescription`[TableName]`usersTable's name, same as what is defined in the database`[TableName:Studly]`UsersTable's name with `Str::studly()` applied to it (useful for standardizing table names if they are inconsistent)`[TableName:Lowercase]`usersTable's name with `strtolower` applied to it (useful for standardizing table names if they are inconsistent)`[Timestamp]`2021\_04\_25\_110000The standard migration timestamp format, at the time of calling the command: `Y_m_d_His``[Index]`0The key of the migration in the sorted order, for use with enforcing a sort order`[IndexedEmptyTimestamp]`0000\_00\_00\_000041The standard migration timestamp format, but filled with 0s and incremented by `[Index]` seconds`[IndexedTimestamp]`2021\_04\_25\_110003The standard migration timestamp format, at the time of calling the command: `Y_m_d_His` incremented by `[Index]` seconds### Table Schema Stub Tokens

[](#table-schema-stub-tokens)

Table schema stubs have the following tokens available:

TokenDescription`[TableName]`Table's name, same as what is defined in the database`[TableName:Studly]`Table's name with `Str::studly()` applied to it, for use with the class name`[TableUp]`Table's `up()` function`[TableDown]`Table's `down()` function`[Schema]`The table's generated schema### View Name Stub Tokens

[](#view-name-stub-tokens)

View stubs have the following tokens available for the naming scheme:

TokenExampleDescription`[ViewName]`user\_earningsView's name, same as what is defined in the database`[ViewName:Studly]`UserEarningsView's name with `Str::studly()` applied to it (useful for standardizing view names if they are inconsistent)`[ViewName:Lowercase]`user\_earningsView's name with `strtolower` applied to it (useful for standardizing view names if they are inconsistent)`[Timestamp]`2021\_04\_25\_110000The standard migration timestamp format, at the time of calling the command: `Y_m_d_His``[Index]`0The key of the migration in the sorted order, for use with enforcing a sort order`[IndexedEmptyTimestamp]`0000\_00\_00\_000041The standard migration timestamp format, but filled with 0s and incremented by `[Index]` seconds`[IndexedTimestamp]`2021\_04\_25\_110003The standard migration timestamp format, at the time of calling the command: `Y_m_d_His` incremented by `[Index]` seconds### View Schema Stub Tokens

[](#view-schema-stub-tokens)

View schema stubs have the following tokens available:

TokenDescription`[ViewName]`View's name, same as what is defined in the database`[ViewName:Studly]`View's name with `Str::studly()` applied to it, for use with the class name`[Schema]`The view's schemaExample Usage
=============

[](#example-usage)

Given a database structure for a `users` table of:

```
CREATE TABLE `users` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `username` varchar(128) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `password` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `first_name` varchar(45) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `last_name` varchar(45) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `timezone` varchar(45) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'America/New_York',
  `location_id` int(10) unsigned NOT NULL,
  `deleted_at` timestamp NULL DEFAULT NULL,
  `remember_token` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `users_username_index` (`username`),
  KEY `users_first_name_index` (`first_name`),
  KEY `users_last_name_index` (`last_name`),
  KEY `users_email_index` (`email`),
  KEY `fk_users_location_id_index` (`location_id`)
  CONSTRAINT `users_location_id_foreign` FOREIGN KEY (`location_id`) REFERENCES `locations` (`id`) ON UPDATE CASCADE ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
```

A `tests/database/migrations/[TIMESTAMP]_create_users_table.php` with the following Blueprint would be created:

```
