cybercog / laravel-love

Add Social Reactions to Laravel Eloquent Models. It lets people express how they feel about the content. Fully customizable Weighted Reaction System & Reaction Type System with Like, Dislike and any other custom emotion types. Do you react?
https://komarev.com/sources/laravel-love
MIT License
1.16k stars 72 forks source link

Add console command to add columns to application tables #53

Closed antonkomarev closed 5 years ago

antonkomarev commented 5 years ago

Reacterable

Command:

$ php artisan love:setup-reacterable --model=App\User

Should create database migration file:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddLoveReacterIdToUsersTable extends Migration
{
    public function up(): void
    {
         Schema::table('users', function (Blueprint $table) {
             $table->unsignedBigInteger('love_reacter_id');
         });

        $table
            ->foreign('love_reacter_id')
            ->references('id')
            ->on('love_reacters');
    }
}

Reactant

Command:

$ php artisan love:setup-reactable --model=App\Comment

Should create database migration file:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddLoveReactantIdToCommentsTable extends Migration
{
    public function up(): void
    {
         Schema::table('comments', function (Blueprint $table) {
             $table->unsignedBigInteger('love_reactant_id');
         });

        $table
            ->foreign('love_reactant_id')
            ->references('id')
            ->on('love_reactants');
    }
}
antonkomarev commented 5 years ago

Questions to solve

What will be a name of the console command?

Console command name should be intuitive. Installation is completed, but some more preparations should be done.

  1. config word might be confusing because configuration is more about changing configuration files, and not preparing database structure.

  2. Name it in default Artisan make namespace:

    • artisan make:love-reacterable-migration
    • artisan make:love-reactable-migration
  3. Use migration word in love namespace:

    • artisan love:make-reacterable-migration
    • artisan love:make-reactable-migration
  4. Use setup word in love namespace:

    • artisan love:setup-reacterable
    • artisan love:setup-reactable

Will it check that target model implements Reactable or Reactant contracts?

Yes. Migration will be created only for models which implements any of these interfaces. By this way in future we will be able to run command without model argument and see a list of all models which implements this interface (but not set-upped yet).

How to deal with nullable columns?

To allow nullable columns we should add optional --nullable boolean argument.

Should this command run migration automatically after adding relation column?

It might be good to run migrate command automatically on development, and on production when migration already exists and not yet migrated, but it might be tricky. I'd prefer to hold off this feature for now.