Skip to content

Commit

Permalink
Prepare reactions table for v2
Browse files Browse the repository at this point in the history
  • Loading branch information
Lakshan-Madushanka committed Sep 23, 2024
1 parent 685984d commit e379060
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 9 deletions.
85 changes: 76 additions & 9 deletions src/Console/V2UpgradeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;
use LakM\Comments\ModelResolver;
use LakM\Comments\Models\Comment;
use LakM\Comments\Models\Reaction;

class V2UpgradeCommand extends Command
{
Expand Down Expand Up @@ -39,6 +42,7 @@ public function handle(): void
try {
$this->info('Migrating data from comments table...');
$this->migrateCommentsData();
$this->migrateReactionsData();

$this->info('Dropping redundant columns from comments table...');
$this->dropColumnsFromCommentsTable();
Expand All @@ -63,25 +67,23 @@ protected function migrateCommentsData(): void
{
$fileName = $this->getMigrationFileName('create_guests_table.php');

if (!($path = glob(database_path('migrations/' . '*guests*')))) {
if (!($path = glob(database_path('migrations/' . '*create_guests_table*')))) {
copy(__DIR__ . '/stubs/create_guests_table.php.stub', database_path('migrations/' . $fileName));
Artisan::call("migrate", ['--path' => 'database/migrations/' . $fileName]);
} else {
} else if (!Schema::hasTable('guests')){
Artisan::call("migrate", ['--path' => 'database/migrations/' . Str::after($path[0], 'migrations/')]);
}

// We need to move guest data to guests table

// First we pick distinct guest_email columns
$emails = Comment::query()
->whereNull('commenter_type')
->groupBy(['guest_email'])
->get();

$emails->each(function (Comment $comment) {
// Create a new guest record in guests table
if (!is_null($comment->commenter_type)) {
return;
}

$guest = $this->guestModel()::query()
->createOrFirst(
Expand All @@ -103,19 +105,84 @@ protected function migrateCommentsData(): void
});
}

public function dropColumnsFromCommentsTable(): void
protected function migrateReactionsData(): void
{
$fileName = $this->getMigrationFileName('add_owner_morph_columns_to_reactions_table.php');

if (!($path = glob(database_path('migrations/' . '*add_owner_morph_columns_to_reactions_table*')))) {
copy(__DIR__ . '/stubs/add_owner_morph_columns_to_reactions_table.php.stub', database_path('migrations/' . $fileName));
Artisan::call("migrate", ['--path' => 'database/migrations/' . $fileName]);
} else {
Artisan::call("migrate", ['--path' => 'database/migrations/' . Str::after($path[0], 'migrations/')]);
}
// dd($fileName);

$authUserReactions = Reaction::query()
->whereNotNull('user_id')
->groupBy(['user_id'])
->get();

$authUserReactions->each(function (Reaction $reaction) {
Reaction::query()
->where('user_id', $reaction->user_id)

Check failure on line 127 in src/Console/V2UpgradeCommand.php

View workflow job for this annotation

GitHub Actions / format

Access to an undefined property LakM\Comments\Models\Reaction::$user_id.

Check failure on line 127 in src/Console/V2UpgradeCommand.php

View workflow job for this annotation

GitHub Actions / format

Access to an undefined property LakM\Comments\Models\Reaction::$user_id.
->update([
'owner_type' => (ModelResolver::userModel())->getMorphClass(),
'owner_id' => $reaction->user_id,

Check failure on line 130 in src/Console/V2UpgradeCommand.php

View workflow job for this annotation

GitHub Actions / format

Access to an undefined property LakM\Comments\Models\Reaction::$user_id.

Check failure on line 130 in src/Console/V2UpgradeCommand.php

View workflow job for this annotation

GitHub Actions / format

Access to an undefined property LakM\Comments\Models\Reaction::$user_id.

]);
});

$guestReactions = Reaction::query()
->whereNull('user_id')
->groupBy(['ip_address'])
->get();

$guestReactions->each(function (Reaction $reaction) {
$guest = $this->guestModel()::query()
->createOrFirst(
['ip_address' => $reaction->ip_address],

Check failure on line 143 in src/Console/V2UpgradeCommand.php

View workflow job for this annotation

GitHub Actions / format

Access to an undefined property LakM\Comments\Models\Reaction::$ip_address.

Check failure on line 143 in src/Console/V2UpgradeCommand.php

View workflow job for this annotation

GitHub Actions / format

Access to an undefined property LakM\Comments\Models\Reaction::$ip_address.
['ip_address' => $reaction->ip_address],

Check failure on line 144 in src/Console/V2UpgradeCommand.php

View workflow job for this annotation

GitHub Actions / format

Access to an undefined property LakM\Comments\Models\Reaction::$ip_address.

Check failure on line 144 in src/Console/V2UpgradeCommand.php

View workflow job for this annotation

GitHub Actions / format

Access to an undefined property LakM\Comments\Models\Reaction::$ip_address.
);

Reaction::query()
->where('ip_address', $reaction->ip_address)

Check failure on line 148 in src/Console/V2UpgradeCommand.php

View workflow job for this annotation

GitHub Actions / format

Access to an undefined property LakM\Comments\Models\Reaction::$ip_address.

Check failure on line 148 in src/Console/V2UpgradeCommand.php

View workflow job for this annotation

GitHub Actions / format

Access to an undefined property LakM\Comments\Models\Reaction::$ip_address.
->update([
'owner_type' => 'LakM\Comments\Models\Guest',
'owner_id' => $guest->getKey(),
]);
});

$fileName = $this->getMigrationFileName('drop_user_id_from_reactions_table.php');

if (!Schema::hasColumn('reactions', 'user_id')) {
return;
}

if (!($path = glob(database_path('migrations/' . '*drop_user_id_from_reactions_table*')))) {
copy(__DIR__ . '/stubs/drop_user_id_from_reactions_table.php.stub', database_path('migrations/' . $fileName));
Artisan::call("migrate", ['--path' => 'database/migrations/' . $fileName]);
} else {
Artisan::call("migrate", ['--path' => 'database/migrations/' . Str::after($path[0], 'migrations/')]);
}
}

protected function dropColumnsFromCommentsTable(): void
{
if(!Schema::hasIndex('comments', 'comments_guest_name_index')) {
return;
}

$fileName = $this->getMigrationFileName('drop_guest_columns_from_comments_table.php');

if (!($path = glob(database_path('migrations/' . '*drop_guest*')))) {
if (!($path = glob(database_path('migrations/' . '*drop_guest_columns_from_comments_table*')))) {
copy(__DIR__ . '/stubs/drop_guest_columns_from_comments_table.php.stub', database_path('migrations/' . $fileName));
Artisan::call("migrate", ['--path' => 'database/migrations/' . $fileName]);
} else {
Artisan::call("migrate", ['--path' => 'database/migrations/' . Str::after($path[0], 'migrations/')]);
}
}

public function guestModel(): Model
protected function guestModel(): Model
{
return new class () extends Model {
protected $table = 'guests';
Expand All @@ -130,7 +197,7 @@ protected function getMigrationFileName(string $migrationFileName): string
$filesystem = app()->make(Filesystem::class);

return Collection::make([app()->databasePath() . DIRECTORY_SEPARATOR . 'migrations' . DIRECTORY_SEPARATOR])
->flatMap(fn ($path) => $filesystem->glob($path . '*_' . $migrationFileName))
->flatMap(fn($path) => $filesystem->glob($path . '*_' . $migrationFileName))
->push("{$timestamp}_{$migrationFileName}")
->first();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

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

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('reactions', function (Blueprint $table) {
$table->after('id', function (Blueprint $table) {
$table->nullableMorphs('owner');
});
});
}
};
19 changes: 19 additions & 0 deletions src/Console/stubs/drop_user_id_from_reactions_table.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

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

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('reactions', function (Blueprint $table) {
$table->dropIndex('reactions_user_id_index');
$table->dropColumn('user_id');
});
}
};

0 comments on commit e379060

Please sign in to comment.