Skip to content

Commit

Permalink
[Update] Migrations by adding Index to Optmize Query Performance
Browse files Browse the repository at this point in the history
  • Loading branch information
dipaksarkar committed Oct 21, 2024
1 parent 15c9036 commit c891b37
Show file tree
Hide file tree
Showing 52 changed files with 811 additions and 802 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ public function up()
$table->unsignedBigInteger('groupable_id');
$table->unsignedBigInteger('group_id');

$table->foreign('group_id')->references('id')->on('groups')->cascadeOnUpdate()->cascadeOnDelete();
$table->foreign('group_id')->references('id')->on('groups')->cascadeOnDelete();

$table->primary(['groupable_type', 'groupable_id', 'group_id']);

$table->index(['groupable_type', 'groupable_id']);
});
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@ public function up()
{
Schema::create('permissions', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('module_id')->nullable();
$table->string('action')->nullable();
$table->string('scope')->nullable()->unique();

$table->unsignedBigInteger('module_id')->index();

$table->string('action');
$table->string('scope')->unique();
$table->text('description')->nullable();

$table->timestamps();
$table->foreign('module_id')->references('id')->on('modules')->cascadeOnUpdate()->cascadeOnDelete();

$table->foreign('module_id')->references('id')->on('modules')->cascadeOnDelete();
});

$this->setAutoIncrement('permissions');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ public function up()
Schema::create('permissionables', function (Blueprint $table) {
$table->string('permissionable_type');
$table->unsignedBigInteger('permissionable_id');

$table->unsignedBigInteger('permission_id');
$table->boolean('access')->default(false)->nullable();

$table->foreign('permission_id')->references('id')->on('permissions')->cascadeOnUpdate()->cascadeOnDelete();
$table->boolean('access')->default(false);

$table->foreign('permission_id')->references('id')->on('permissions')->cascadeOnDelete();

$table->unique(['permissionable_type', 'permissionable_id', 'permission_id'], 'permissionable_unique');
});
}
};
9 changes: 6 additions & 3 deletions database/migrations/2021_05_26_051127_create_logs_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@ public function up()
$table->string('logable_type')->nullable();
$table->unsignedBigInteger('logable_id')->nullable();

$table->string('type')->default('default');
$table->string('status')->default(Log::STATUS_SUCCESS)->nullable();
$table->string('type')->default('default')->index();
$table->string('status')->default(Log::STATUS_SUCCESS)->index();
$table->text('message')->nullable();
$table->{$this->jsonable()}('options')->nullable();
$table->string('admin_id')->nullable();

$table->unsignedBigInteger('admin_id')->nullable()->index();

$table->timestamps();
$table->softDeletes();

$table->index(['logable_type', 'logable_id']);
});

$this->setAutoIncrement('logs');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,28 @@
*/
public function up()
{
if (Schema::hasTable('payment_methods')) return;

Schema::create('payment_methods', function (Blueprint $table) {
$table->id();

$table->string('name');
$table->string('provider')->default('manual');
$table->string('provider')->default('manual')->index();
$table->string('link')->nullable();
$table->string('logo')->nullable();
$table->text('description')->nullable();
$table->{$this->jsonable()}('credentials')->nullable();
$table->{$this->jsonable()}('methods')->nullable();
$table->boolean('active')->default(false);
$table->enum('capture', ['automatic', 'manual'])->nullable()->default('manual');
$table->boolean('active')->default(false)->index();
$table->string('capture')->default('manual')->index();
$table->string('additional_details')->nullable();
$table->string('payment_instructions')->nullable();
$table->boolean('test_mode')->default(false);
$table->string('transaction_fee')->default(0);
$table->string('webhook')->nullable();
$table->decimal('transaction_fee', 10, 2)->default(0.00);
$table->text('webhook')->nullable();

$table->timestamps();
$table->softDeletes();
});

$this->setAutoIncrement('payment_methods');
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public function up()

$table->string('label')->nullable();
$table->string('subject')->nullable();
$table->string('type')->nullable();
$table->string('type')->nullable()->index();
$table->text('content')->nullable();
$table->boolean('is_default')->nullable()->default(false);
$table->boolean('is_default')->default(false)->index();

$table->timestamps();
$table->softDeletes();
Expand Down
14 changes: 10 additions & 4 deletions database/migrations/2022_07_23_044529_create_addresses_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ public function up()
{
Schema::create('addresses', function (Blueprint $table) {
$table->id();

$table->nullableMorphs('addressable');

$table->string('name')->nullable();
$table->string('first_name')->nullable();
$table->string('last_name')->nullable();
$table->string('company')->nullable();
Expand All @@ -27,12 +30,15 @@ public function up()
$table->string('city')->nullable();
$table->string('state')->nullable();
$table->string('state_code')->nullable();
$table->string('postal_code')->nullable();

$table->string('postal_code')->nullable()->index();
$table->string('country')->nullable();
$table->string('country_code')->nullable();
$table->string('country_code')->nullable()->index();

$table->string('phone_number')->nullable();
$table->boolean('default')->default(false);
$table->string('ref')->nullable();
$table->boolean('default')->default(false)->index();

$table->string('ref')->nullable()->index();
$table->timestamps();
$table->softDeletes();
});
Expand Down
6 changes: 3 additions & 3 deletions database/migrations/2022_07_24_073057_create_files_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ public function up()
{
Schema::create('files', function (Blueprint $table) {
$table->id();
$table->string('disk')->nullable();
$table->string('disk')->nullable()->index();
$table->string('url')->nullable();
$table->string('path')->nullable();
$table->string('original_file_name')->nullable();
$table->string('hash')->nullable();
$table->string('mime_type')->nullable();
$table->string('mime_type')->nullable()->index();
$table->string('extension')->nullable();
$table->string('size')->nullable();
$table->string('ref')->default('default');
$table->string('ref')->default('default')->index();
$table->timestamps();
$table->softDeletes();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ public function up()
$table->unsignedBigInteger('fileable_id');
$table->unsignedBigInteger('file_id');
$table->string('order')->default(0)->nullable();
$table->string('type')->nullable();
$table->string('type')->nullable()->index();

$table->foreign('file_id')->references('id')->on('files')->cascadeOnUpdate()->cascadeOnDelete();
$table->foreign('file_id')->references('id')->on('files')->cascadeOnDelete();

$table->index(['fileable_type', 'fileable_id']);
});
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function up()
Schema::create('features', function (Blueprint $table) {
$table->id();
$table->string('label');
$table->string('slug')->unique();
$table->string('slug')->unique()->index();
$table->enum('type', ['integer', 'boolean'])->nullable()->default('integer');
$table->boolean('resetable')->nullable()->default(false);
$table->mediumText('description')->nullable();
Expand Down
8 changes: 5 additions & 3 deletions database/migrations/2022_07_24_092101_create_plans_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ public function up()
Schema::create('plans', function (Blueprint $table) {
$table->id();
$table->string('label')->nullable();
$table->string('slug')->unique();
$table->string('slug')->unique()->index();
$table->longText('description')->nullable();
$table->boolean('is_active')->default(true);
$table->boolean('is_active')->default(true)->index();
$table->string('default_interval')->default('month');
$table->string('interval')->default('month');
$table->string('interval')->default('month')->index();
$table->unsignedInteger('interval_count')->default(1);
$table->double('price', 12, 2)->default(0.00);
$table->unsignedInteger('trial_days')->nullable()->default(0);
Expand All @@ -39,6 +39,8 @@ public function up()
$table->foreignIdFor(Plan::class);
$table->foreignIdFor(Feature::class);
$table->integer('value')->default(0);

$table->index(['plan_id', 'feature_id']);
});
}
};
20 changes: 12 additions & 8 deletions database/migrations/2022_07_24_092102_create_coupons_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,16 @@ public function up(): void
Schema::create('coupons', function (Blueprint $table) {
$table->id();
$table->string('name')->nullable();
$table->string('stripe_id')->nullable();
$table->string('promotion_code')->unique();
$table->string('promotion_id')->nullable();
$table->string('promotion_code')->unique()->index();
$table->{$this->jsonable()}('applies_to')->nullable();
$table->string('duration');
$table->unsignedInteger('duration_in_months')->nullable();
$table->unsignedInteger('max_redemptions')->nullable();
$table->unsignedBigInteger('amount_off')->nullable();
$table->unsignedSmallInteger('percent_off')->nullable();
$table->boolean('fixed')->default(false);
$table->boolean('active')->default(true);
$table->dateTime('expires_at')->nullable();
$table->boolean('active')->default(true)->index();
$table->dateTime('expires_at')->nullable()->index();
$table->timestamps();
$table->softDeletes();
});
Expand All @@ -39,17 +37,23 @@ public function up(): void
$table->unsignedBigInteger('coupon_id');
$table->unsignedBigInteger('plan_id');

$table->foreign('plan_id')->references('id')->on('plans')->cascadeOnUpdate()->cascadeOnDelete();
$table->foreign('coupon_id')->references('id')->on('coupons')->cascadeOnUpdate()->cascadeOnDelete();
$table->foreign('plan_id')->references('id')->on('plans')->cascadeOnDelete();
$table->foreign('coupon_id')->references('id')->on('coupons')->cascadeOnDelete();

$table->index(['coupon_id', 'plan_id']);
});

Schema::create('redeems', function (Blueprint $table) {
$table->id();
$table->string('redeemable_type');
$table->unsignedBigInteger('redeemable_id');
$table->unsignedBigInteger('coupon_id');
$table->unsignedBigInteger('coupon_id')->index();
$table->double('amount', 20, 2)->default(0.00)->nullable();
$table->timestamps();

$table->foreign('coupon_id')->references('id')->on('coupons')->cascadeOnDelete();

$table->index(['redeemable_type', 'redeemable_id']);
});

$this->setAutoIncrement('redeems');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function up()
{
Schema::create('app_settings', function (Blueprint $table) {
$table->id();
$table->string('key')->unique();
$table->string('key')->unique()->index();
$table->{$this->jsonable()}('options')->nullable();
$table->timestamps();
$table->softDeletes();
Expand Down
27 changes: 16 additions & 11 deletions database/migrations/2022_10_14_064920_create_enquiries_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ public function up()
$table->id();

$table->string('name')->nullable();
$table->string('email')->nullable();
$table->string('email')->nullable()->index();
$table->string('phone')->nullable();
$table->string('subject')->nullable();
$table->text('message')->nullable();
$table->string('status')->nullable();
$table->boolean('seen')->nullable()->default(false);
$table->boolean('source')->nullable()->default(true);
$table->boolean('is_archived')->nullable()->default(false);
$table->boolean('user_archived')->nullable()->default(false);
$table->unsignedBigInteger('admin_id')->nullable();
$table->string('status')->nullable()->index();
$table->boolean('seen')->default(false)->index();
$table->boolean('source')->default(true)->index();
$table->boolean('is_archived')->default(false)->index();
$table->boolean('user_archived')->default(false)->index();
$table->unsignedBigInteger('admin_id')->nullable()->index();

$table->timestamps();
$table->softDeletes();
Expand All @@ -41,15 +41,20 @@ public function up()

Schema::create('replies', function (Blueprint $table) {
$table->id();

$table->string('user_type')->nullable();
$table->unsignedBigInteger('user_id')->nullable();
$table->unsignedBigInteger('enquiry_id');

$table->unsignedBigInteger('enquiry_id')->index();

$table->text('message')->nullable();
$table->boolean('seen')->nullable()->default(false);
$table->boolean('staff_only')->nullable()->default(false);
$table->boolean('seen')->default(false)->index();
$table->boolean('staff_only')->default(false)->index();
$table->timestamps();

$table->foreign('enquiry_id')->references('id')->on('enquiries')->cascadeOnUpdate()->cascadeOnDelete();
$table->foreign('enquiry_id')->references('id')->on('enquiries')->cascadeOnDelete();

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

$this->setAutoIncrement('replies');
Expand Down
20 changes: 13 additions & 7 deletions database/migrations/2023_01_30_164920_create_tasks_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function up()
Schema::create('tasks', function (Blueprint $table) {
$table->id();

$table->unsignedBigInteger('user_id')->nullable();
$table->unsignedBigInteger('user_id')->nullable()->index();
$table->string('subject')->nullable();
$table->text('message')->nullable();
$table->string('status')->nullable();
Expand All @@ -36,8 +36,10 @@ public function up()
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('task_id');

$table->foreign('task_id')->references('id')->on('tasks')->cascadeOnUpdate()->cascadeOnDelete();
$table->foreign('user_id')->references('id')->on('admins')->cascadeOnUpdate()->cascadeOnDelete();
$table->foreign('task_id')->references('id')->on('tasks')->cascadeOnDelete();
$table->foreign('user_id')->references('id')->on('admins')->cascadeOnDelete();

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

Schema::create('task_replies', function (Blueprint $table) {
Expand All @@ -48,8 +50,10 @@ public function up()
$table->text('message')->nullable();
$table->timestamps();

$table->foreign('task_id')->references('id')->on('tasks')->cascadeOnUpdate()->cascadeOnDelete();
$table->foreign('user_id')->references('id')->on('admins')->cascadeOnUpdate()->cascadeOnDelete();
$table->foreign('task_id')->references('id')->on('tasks')->cascadeOnDelete();
$table->foreign('user_id')->references('id')->on('admins')->cascadeOnDelete();

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

$this->setAutoIncrement('task_replies');
Expand All @@ -58,8 +62,10 @@ public function up()
$table->unsignedBigInteger('task_id');
$table->unsignedBigInteger('user_id');

$table->foreign('task_id')->references('id')->on('tasks')->cascadeOnUpdate()->cascadeOnDelete();
$table->foreign('user_id')->references('id')->on('admins')->cascadeOnUpdate()->cascadeOnDelete();
$table->foreign('task_id')->references('id')->on('tasks')->cascadeOnDelete();
$table->foreign('user_id')->references('id')->on('admins')->cascadeOnDelete();

$table->index(['task_id', 'user_id']);
});
}
};
Loading

0 comments on commit c891b37

Please sign in to comment.