Skip to content

Commit

Permalink
Use BigIncrements for Laravel 5.8 (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
musonza authored Jul 22, 2019
1 parent 1247bd0 commit 0104a62
Showing 1 changed file with 142 additions and 68 deletions.
210 changes: 142 additions & 68 deletions database/migrations/create_chat_tables.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ class CreateChatTables extends Migration
protected $userModelPrimaryKey = 'id';
protected $userModelTable = 'users';

protected $useBigIncrements;

public function __construct()
{
$config = config('musonza_chat');
$userModel = app($config['user_model']);

$this->userModelPrimaryKey = $userModel->getKeyName();
$this->userModelTable = $userModel->getTable();
$this->useBigIncrements = app()::VERSION >= 5.8;
}

/**
Expand All @@ -25,74 +28,145 @@ public function __construct()
*/
public function up()
{
Schema::create('mc_conversations', function (Blueprint $table) {
$table->increments('id');
$table->boolean('private')->default(true);
$table->text('data')->nullable();
$table->timestamps();
});

Schema::create('mc_messages', function (Blueprint $table) {
$table->increments('id');
$table->text('body');
$table->integer('conversation_id')->unsigned();
$table->integer('user_id')->unsigned();
$table->string('type')->default('text');
$table->timestamps();

$table->foreign('user_id')
->references($this->userModelPrimaryKey)
->on($this->userModelTable)
->onDelete('cascade');

$table->foreign('conversation_id')
->references('id')
->on('mc_conversations')
->onDelete('cascade');
});

Schema::create('mc_conversation_user', function (Blueprint $table) {
$table->integer('user_id')->unsigned();
$table->integer('conversation_id')->unsigned();
$table->primary(['user_id', 'conversation_id']);
$table->timestamps();

$table->foreign('conversation_id')
->references('id')->on('mc_conversations')
->onDelete('cascade');

$table->foreign('user_id')
->references($this->userModelPrimaryKey)
->on($this->userModelTable)
->onDelete('cascade');
});

Schema::create('mc_message_notification', function (Blueprint $table) {
$table->increments('id');
$table->integer('message_id')->unsigned();
$table->integer('conversation_id')->unsigned();
$table->integer('user_id')->unsigned();
$table->boolean('is_seen')->default(false);
$table->boolean('is_sender')->default(false);
$table->boolean('flagged')->default(false);
$table->timestamps();
$table->softDeletes();

$table->index(['user_id', 'message_id']);

$table->foreign('message_id')
->references('id')->on('mc_messages')
->onDelete('cascade');

$table->foreign('conversation_id')
->references('id')->on('mc_conversations')
->onDelete('cascade');

$table->foreign('user_id')
->references($this->userModelPrimaryKey)
->on($this->userModelTable)
->onDelete('cascade');
});
if ($this->useBigIncrements) {
Schema::create('mc_conversations', function (Blueprint $table) {
$table->bigIncrements('id');
$table->boolean('private')->default(true);
$table->text('data')->nullable();
$table->timestamps();
});

Schema::create('mc_messages', function (Blueprint $table) {
$table->bigIncrements('id');
$table->text('body');
$table->integer('conversation_id')->unsignedBigIntegers();
$table->integer('user_id')->unsignedBigIntegers();
$table->string('type')->default('text');
$table->timestamps();

$table->foreign('user_id')
->references($this->userModelPrimaryKey)
->on($this->userModelTable)
->onDelete('cascade');

$table->foreign('conversation_id')
->references('id')
->on('mc_conversations')
->onDelete('cascade');
});

Schema::create('mc_conversation_user', function (Blueprint $table) {
$table->bigIncrements('user_id')->unsignedBigIntegers();
$table->integer('conversation_id')->unsignedBigIntegers();
$table->primary(['user_id', 'conversation_id']);
$table->timestamps();

$table->foreign('conversation_id')
->references('id')->on('mc_conversations')
->onDelete('cascade');

$table->foreign('user_id')
->references($this->userModelPrimaryKey)
->on($this->userModelTable)
->onDelete('cascade');
});

Schema::create('mc_message_notification', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('message_id')->unsignedBigIntegers();
$table->integer('conversation_id')->unsignedBigIntegers();
$table->integer('user_id')->unsignedBigIntegers();
$table->boolean('is_seen')->default(false);
$table->boolean('is_sender')->default(false);
$table->boolean('flagged')->default(false);
$table->timestamps();
$table->softDeletes();

$table->index(['user_id', 'message_id']);

$table->foreign('message_id')
->references('id')->on('mc_messages')
->onDelete('cascade');

$table->foreign('conversation_id')
->references('id')->on('mc_conversations')
->onDelete('cascade');

$table->foreign('user_id')
->references($this->userModelPrimaryKey)
->on($this->userModelTable)
->onDelete('cascade');
});
} else {
Schema::create('mc_conversations', function (Blueprint $table) {
$table->increments('id');
$table->boolean('private')->default(true);
$table->text('data')->nullable();
$table->timestamps();
});

Schema::create('mc_messages', function (Blueprint $table) {
$table->increments('id');
$table->text('body');
$table->integer('conversation_id')->unsigned();
$table->integer('user_id')->unsigned();
$table->string('type')->default('text');
$table->timestamps();

$table->foreign('user_id')
->references($this->userModelPrimaryKey)
->on($this->userModelTable)
->onDelete('cascade');

$table->foreign('conversation_id')
->references('id')
->on('mc_conversations')
->onDelete('cascade');
});

Schema::create('mc_conversation_user', function (Blueprint $table) {
$table->integer('user_id')->unsigned();
$table->integer('conversation_id')->unsigned();
$table->primary(['user_id', 'conversation_id']);
$table->timestamps();

$table->foreign('conversation_id')
->references('id')->on('mc_conversations')
->onDelete('cascade');

$table->foreign('user_id')
->references($this->userModelPrimaryKey)
->on($this->userModelTable)
->onDelete('cascade');
});

Schema::create('mc_message_notification', function (Blueprint $table) {
$table->increments('id');
$table->integer('message_id')->unsigned();
$table->integer('conversation_id')->unsigned();
$table->integer('user_id')->unsigned();
$table->boolean('is_seen')->default(false);
$table->boolean('is_sender')->default(false);
$table->boolean('flagged')->default(false);
$table->timestamps();
$table->softDeletes();

$table->index(['user_id', 'message_id']);

$table->foreign('message_id')
->references('id')->on('mc_messages')
->onDelete('cascade');

$table->foreign('conversation_id')
->references('id')->on('mc_conversations')
->onDelete('cascade');

$table->foreign('user_id')
->references($this->userModelPrimaryKey)
->on($this->userModelTable)
->onDelete('cascade');
});
}
}

/**
Expand Down

0 comments on commit 0104a62

Please sign in to comment.