Generate Migrations from an Existing Database With the Migration Generator Package


April 23rd, 2021

Generate Migrations from an Existing Database With the Migration Generator Package

Laravel Migration Generator

Migration Generator for Laravel is a package by Bennett Treptow to generate migrations from existing database structures:

A primary use case for this package would be a project that has many migrations that alter tables using ->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.

This package could be helpful if you are porting an existing application into Laravel and want to re-create the database migrations for the application to help with development and testing.

To visualize how this process works, the readme has example usage which defines the following users table:

1CREATE TABLE `users` (
2 `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
3 `username` varchar(128) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
4 `email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
5 `password` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
6 `first_name` varchar(45) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
7 `last_name` varchar(45) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
8 `timezone` varchar(45) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'America/New_York',
9 `location_id` int(10) unsigned NOT NULL,
10 `deleted_at` timestamp NULL DEFAULT NULL,
11 `remember_token` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
12 `created_at` timestamp NULL DEFAULT NULL,
13 `updated_at` timestamp NULL DEFAULT NULL,
14 PRIMARY KEY (`id`),
15 KEY `users_username_index` (`username`),
16 KEY `users_first_name_index` (`first_name`),
17 KEY `users_last_name_index` (`last_name`),
18 KEY `users_email_index` (`email`),
19 KEY `fk_users_location_id_index` (`location_id`)
20 CONSTRAINT `users_location_id_foreign` FOREIGN KEY (`location_id`) REFERENCES `locations` (`id`) ON UPDATE CASCADE ON DELETE CASCADE

Using this package, you can run the following command to generate a blueprint class based on the table definition:

1php artisan generate:migrations

And the derived blueprint from the users table would look as follows according to the example:

1use Illuminate\Support\Facades\Schema;
2use Illuminate\Database\Schema\Blueprint;
3use Illuminate\Database\Migrations\Migration;
5class CreateUsersTable extends Migration
7 /**
8 * Run the migrations.
9 *
10 * @return void
11 */
12 public function up()
13 {
14 Schema::create('users', function (Blueprint $table) {
15 $table->increments('id');
16 $table->string('username', 128)->nullable()->index();
17 $table->string('email', 255)->index();
18 $table->string('password', 255);
19 $table->string('first_name', 45)->nullable()->index();
20 $table->string('last_name', 45)->index();
21 $table->string('timezone', 45)->default('America/New_York');
22 $table->unsignedInteger('location_id');
23 $table->softDeletes();
24 $table->string('remember_token', 255)->nullable();
25 $table->timestamps();
26 $table->foreign('location_id', 'users_location_id_foreign')->references('id')->on('locations')->onUpdate('cascade')->onDelete('cascade');
27 });
28 }
30 /**
31 * Reverse the migrations.
32 *
33 * @return void
34 */
35 public function down()
36 {
37 Schema::dropIfExists('users');
38 }

The package also comes with various table and view migration stubs and configuration settings. For example, a configuration defining the filename patterns used to generate table schema:

1return [
2 'table_naming_scheme' => '[Timestamp]_create_[TableName]_table.php',
3 // ...

At the time of writing the package supports MySQL, but could also support Postgres, SQLite, and SQL Server according the the readme.

You can learn more about this package, get full installation instructions, and view the source code on GitHub.

This package was submitted to our Laravel News Links section. Links is a place the community can post packages and tutorials around the Laravel ecosystem. Follow along on Twitter @LaravelLinks

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.