Skip to content

Commit

Permalink
Replacing model_type in database (#100)
Browse files Browse the repository at this point in the history
* Replacing model_type in database

* Also update old Crater namespace
  • Loading branch information
mchev committed Jun 7, 2024
1 parent bb82580 commit a0bf6f7
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions database/migrations/2024_06_05_155321_update_stored_namespace.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
$this->replaceModelTypes('InvoiceShelf', 'App');
$this->replaceModelTypes('Crater', 'App');
}

/**
* Reverse the migrations.
*/
public function down(): void
{
$this->replaceModelTypes('App', 'InvoiceShelf');
}

/**
* Replace model types in the specified tables.
*/
private function replaceModelTypes(string $from, string $to): void
{
$mappings = [
'media' => 'model_type',
'email_logs' => 'mailable_type',
'notifications' => 'notifiable_type',
'personal_access_tokens' => 'tokenable_type',
'custom_field_values' => 'custom_field_valuable_type',
'abilities' => 'entity_type',
'assigned_roles' => 'entity_type',
];

foreach ($mappings as $table => $column) {
DB::table($table)->update([$column => DB::raw("REPLACE($column, '$from', '$to')")]);
}
}
};

0 comments on commit a0bf6f7

Please sign in to comment.