Skip to content

Commit

Permalink
fix: make migrations compatible with sqlite & postgres
Browse files Browse the repository at this point in the history
Signed-off-by: Sam Poyigi <[email protected]>
  • Loading branch information
sampoyigi committed Dec 23, 2024
1 parent 8f8dd9d commit 9bbf4ba
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,13 @@ private function updateLocations()
]);
});

Schema::table('locations', function(Blueprint $table) {
$table->json('options')->change();
});
if (Schema::getConnection()->getDriverName() === 'pgsql') {
DB::statement('ALTER TABLE locations ALTER COLUMN options TYPE json USING options::json');
} else {
Schema::table('locations', function(Blueprint $table) {
$table->json('options')->change();
});
}
}

private function updateLocationAreas()
Expand All @@ -48,10 +52,16 @@ private function updateLocationAreas()
]);
});

Schema::table('location_areas', function(Blueprint $table) {
$table->json('boundaries')->change();
$table->json('conditions')->change();
});

if (Schema::getConnection()->getDriverName() === 'pgsql') {
DB::statement('ALTER TABLE location_areas ALTER COLUMN boundaries TYPE json USING boundaries::json');
DB::statement('ALTER TABLE location_areas ALTER COLUMN conditions TYPE json USING conditions::json');
} else {
Schema::table('location_areas', function(Blueprint $table) {
$table->json('boundaries')->change();
$table->json('conditions')->change();
});
}
}

private function updatePayments()
Expand All @@ -66,9 +76,13 @@ private function updatePayments()
]);
});

Schema::table('payments', function(Blueprint $table) {
$table->json('data')->change();
});
if (Schema::getConnection()->getDriverName() === 'pgsql') {
DB::statement('ALTER TABLE payments ALTER COLUMN data TYPE json USING data::json');
} else {
Schema::table('payments', function(Blueprint $table) {
$table->json('data')->change();
});
}
}

protected function updateExtensionSettings()
Expand All @@ -83,9 +97,13 @@ protected function updateExtensionSettings()
]);
});

Schema::table('extension_settings', function(Blueprint $table) {
$table->json('data')->change();
});
if (Schema::getConnection()->getDriverName() === 'pgsql') {
DB::statement('ALTER TABLE extension_settings ALTER COLUMN data TYPE json USING data::json');
} else {
Schema::table('extension_settings', function(Blueprint $table) {
$table->json('data')->change();
});
}
}

protected function updateThemes()
Expand All @@ -100,8 +118,12 @@ protected function updateThemes()
]);
});

Schema::table('themes', function(Blueprint $table) {
$table->json('data')->change();
});
if (Schema::getConnection()->getDriverName() === 'pgsql') {
DB::statement('ALTER TABLE themes ALTER COLUMN data TYPE json USING data::json');
} else {
Schema::table('themes', function(Blueprint $table) {
$table->json('data')->change();
});
}
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -33,48 +33,69 @@ public function up()
}

Schema::table('customers', function(Blueprint $table) {
$table->timestamp('date_added')->change();
$table->renameColumn('date_added', 'created_at');
});

Schema::table('customers', function(Blueprint $table) {
$table->timestamp('created_at')->change();
$table->timestamp('updated_at');
});

Schema::table('orders', function(Blueprint $table) {
$table->timestamp('date_added')->change();
$table->timestamp('date_modified')->change();
});

Schema::table('orders', function(Blueprint $table) {
$table->renameColumn('date_added', 'created_at');
$table->renameColumn('date_modified', 'updated_at');
});

Schema::table('payments', function(Blueprint $table) {
$table->timestamp('date_added')->change();
$table->timestamp('date_updated')->change();
});

Schema::table('payments', function(Blueprint $table) {
$table->renameColumn('date_added', 'created_at');
$table->renameColumn('date_updated', 'updated_at');
});

Schema::table('payment_logs', function(Blueprint $table) {
$table->timestamp('date_added')->change();
$table->timestamp('date_updated')->change();
});

Schema::table('payment_logs', function(Blueprint $table) {
$table->renameColumn('date_added', 'created_at');
$table->renameColumn('date_updated', 'updated_at');
});

Schema::table('reservations', function(Blueprint $table) {
$table->timestamp('date_added')->change();
$table->timestamp('date_modified')->change();
});

Schema::table('reservations', function(Blueprint $table) {
$table->renameColumn('date_added', 'created_at');
$table->renameColumn('date_modified', 'updated_at');
});

Schema::table('staffs', function(Blueprint $table) {
$table->timestamp('date_added')->change();
$table->renameColumn('date_added', 'created_at');
});

Schema::table('staffs', function(Blueprint $table) {
$table->timestamp('created_at')->change();
$table->timestamp('updated_at');
});

Schema::table('status_history', function(Blueprint $table) {
$table->timestamp('date_added')->change();
$table->renameColumn('date_added', 'created_at');
});

Schema::table('status_history', function(Blueprint $table) {
$table->timestamp('created_at')->change();
$table->timestamp('updated_at');
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,35 @@

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

return new class extends Migration
{
public function up()
{
Schema::table('working_hours', function(Blueprint $table) {
// Create copy of working_hours table
Schema::create('working_hours_new', function(Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('location_id');
$table->string('type', 32);
$table->integer('weekday');
$table->time('start_time');
$table->time('end_time');
$table->boolean('status')->default(0);
$table->timestamps();
});

// Copy data from old table to new table
DB::table('working_hours_new')->insert(
DB::table('working_hours')->get()->toArray(),
);

// Drop the old table
Schema::dropIfExists('working_hours');

// Rename the new table to the original table's name
Schema::rename('working_hours_new', 'working_hours');
}

public function down() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Igniter\Main\Classes\MediaLibrary;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Schema;

Expand Down Expand Up @@ -44,39 +45,40 @@ public function down()

protected function seedAttachmentsFromExistingModels()
{
Menu::select('menu_photo', 'menu_id')->get()->each(function($model) {
DB::table('menus')->select('menu_photo', 'menu_id')->get()->each(function($model) {
if (!empty($model->menu_photo)) {
$this->createMediaAttachment($model->menu_photo, $model, 'thumb');
$this->createMediaAttachment($model->menu_photo, 'thumb', Menu::class);
}
});

Category::pluck('image', 'category_id')->each(function($model) {
DB::table('categories')->select('image', 'category_id')->get()->each(function($model) {
if (!empty($model->image)) {
$this->createMediaAttachment($model->image, $model, 'thumb');
$this->createMediaAttachment($model->image, 'thumb', Category::class);
}
});

Location::select('location_image', 'options', 'location_id')->get()->each(function($model) {
DB::table('locations')->select('location_image', 'options', 'location_id')->get()->each(function($model) {
if (!empty($model->location_image)) {
$this->createMediaAttachment($model->location_image, $model, 'thumb');
$this->createMediaAttachment($model->location_image, 'thumb', Location::class);
}

if (!empty($images = array_get($model->options, 'gallery.images'))) {
foreach ($images as $image) {
$this->createMediaAttachment($image, $model, 'gallery');
$this->createMediaAttachment($image, 'gallery', Location::class);
}
}
});
}

protected function createMediaAttachment($path, $model, $tagName)
protected function createMediaAttachment($path, $tagName, $modelClass)
{
try {
$mediaLibrary = resolve(MediaLibrary::class);
$path = $mediaLibrary->getMediaRelativePath($path);

$model = new $modelClass;
$media = $model->newMediaInstance();
$media->addFromFile(assets_path($mediaLibrary->getMediaPath($path)), $tagName);
$media->addFromFile(url($mediaLibrary->getMediaPath($path)), $tagName);

$media->save();
$model->media()->save($media);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ public function up()

Schema::table('currencies', function(Blueprint $table) {
$table->timestamp('date_modified')->change();
});

Schema::table('currencies', function(Blueprint $table) {
$table->renameColumn('date_modified', 'updated_at');
});

Schema::table('currencies', function(Blueprint $table) {
$table->timestamp('created_at');
});

Expand All @@ -25,13 +31,19 @@ public function up()
Schema::table('mail_layouts', function(Blueprint $table) {
$table->timestamp('date_added')->change();
$table->timestamp('date_updated')->change();
});

Schema::table('mail_layouts', function(Blueprint $table) {
$table->renameColumn('date_added', 'created_at');
$table->renameColumn('date_updated', 'updated_at');
});

Schema::table('mail_templates', function(Blueprint $table) {
$table->timestamp('date_added')->change();
$table->timestamp('date_updated')->change();
});

Schema::table('mail_templates', function(Blueprint $table) {
$table->renameColumn('date_added', 'created_at');
$table->renameColumn('date_updated', 'updated_at');
});
Expand Down

0 comments on commit 9bbf4ba

Please sign in to comment.