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 CASCADE21) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
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; 4 5class CreateUsersTable extends Migration 6{ 7 /** 8 * Run the migrations. 9 *10 * @return void11 */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 }29 30 /**31 * Reverse the migrations.32 *33 * @return void34 */35 public function down()36 {37 Schema::dropIfExists('users');38 }39}
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 // ...4];
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.
0 comments:
Post a Comment
Thanks