Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migration Class Refactor and Column Existence Check #358

Merged
merged 4 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions migrations/2016_06_27_000000_create_mediable_tables.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
use Illuminate\Support\Facades\Schema;
use Plank\Mediable\Media;

class CreateMediableTables extends Migration
{
return new class extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
public function up(): void
{
if (!Schema::hasTable('media')) {
Schema::create(
Expand Down Expand Up @@ -52,7 +51,7 @@ function (Blueprint $table) {
*
* @return void
*/
public function down()
public function down(): void
{
Schema::dropIfExists('mediables');
Schema::dropIfExists('media');
Expand All @@ -65,4 +64,4 @@ public function getConnection()
{
return config('mediable.connection_name', parent::getConnection());
}
}
};
24 changes: 16 additions & 8 deletions migrations/2020_10_12_000000_add_variants_to_media.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,27 @@
use Illuminate\Support\Facades\Schema;
use Plank\Mediable\Media;

class AddVariantsToMedia extends Migration
{
return new class extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
public function up(): void
{
Schema::table(
Schema::whenTableDoesntHaveColumn(
'media',
'variant_name',
function (Blueprint $table) {
$table->string('variant_name', 255)
->after('size')
->nullable();
}
);
Schema::whenTableDoesntHaveColumn(
'media',
'original_media_id',
function (Blueprint $table) {
$table->foreignIdFor(Media::class, 'original_media_id')
->nullable()
->after('variant_name')
Expand All @@ -35,19 +41,21 @@ function (Blueprint $table) {
*
* @return void
*/
public function down()
public function down(): void
{
Schema::table(
Schema::whenTableHasColumn(
'media',
'original_media_id',
function (Blueprint $table) {
if (DB::getDriverName() !== 'sqlite') {
$table->dropForeign('original_media_id');
}
$table->dropColumn('original_media_id');
}
);
Schema::table(
Schema::whenTableHasColumn(
'media',
'variant_name',
function (Blueprint $table) {
$table->dropColumn('variant_name');
}
Expand All @@ -61,4 +69,4 @@ public function getConnection()
{
return config('mediable.connection_name', parent::getConnection());
}
}
};
17 changes: 9 additions & 8 deletions migrations/2024_03_30_000000_add_alt_to_media.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@
use Illuminate\Support\Facades\Schema;
use Plank\Mediable\Media;

class AddAltToMedia extends Migration
{
return new class extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
public function up():void
{
Schema::table(
Schema::whenTableDoesntHaveColumn(
'media',
'alt',
function (Blueprint $table) {
$table->text('alt');
$table->text('alt')->nullable();
}
);
}
Expand All @@ -28,10 +28,11 @@ function (Blueprint $table) {
*
* @return void
*/
public function down()
public function down():void
{
Schema::table(
Schema::whenTableHasColumn(
'media',
'alt',
function (Blueprint $table) {
$table->dropColumn('alt');
}
Expand All @@ -45,4 +46,4 @@ public function getConnection()
{
return config('mediable.connection_name', parent::getConnection());
}
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@
/**
* Create table for mock mediable model
*/
class CreateMediableTestTables extends Migration
{
return new class extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
public function up(): void
{
Schema::create('sample_mediables', function (Blueprint $table) {
$table->increments('id');
Expand Down Expand Up @@ -47,7 +46,7 @@ public function up()
*
* @return void
*/
public function down()
public function down(): void
{
Schema::connection($this->getConnectionName())->table('media', function (Blueprint $table) {
$table->dropColumn('deleted_at');
Expand All @@ -65,4 +64,4 @@ public function getConnectionName(): ?string
{
return config('mediable.connection_name', $this->getConnection());
}
}
};