Changeset View
Changeset View
Standalone View
Standalone View
src/database/migrations/2020_05_05_095212_create_tenants_table.php
- This file was added.
<?php | |||||
use Illuminate\Database\Migrations\Migration; | |||||
use Illuminate\Database\Schema\Blueprint; | |||||
use Illuminate\Support\Facades\Schema; | |||||
// phpcs:ignore | |||||
class CreateTenantsTable extends Migration | |||||
{ | |||||
/** | |||||
* Run the migrations. | |||||
* | |||||
* @return void | |||||
*/ | |||||
public function up() | |||||
{ | |||||
Schema::create( | |||||
'tenants', | |||||
function (Blueprint $table) { | |||||
$table->bigIncrements('id'); | |||||
$table->string('title', 32); | |||||
$table->timestamps(); | |||||
} | |||||
); | |||||
\App\Tenant::create(['title' => 'Kolab Now']); | |||||
foreach (['users', 'discounts', 'domains', 'plans', 'packages', 'skus'] as $table_name) { | |||||
Schema::table( | |||||
$table_name, | |||||
function (Blueprint $table) { | |||||
$table->bigInteger('tenant_id')->unsigned()->nullable(); | |||||
$table->foreign('tenant_id')->references('id')->on('tenants')->onDelete('set null'); | |||||
} | |||||
); | |||||
if ($tenant_id = \config('app.tenant_id')) { | |||||
DB::statement("UPDATE `{$table_name}` SET `tenant_id` = {$tenant_id}"); | |||||
} | |||||
} | |||||
// Add fee column | |||||
foreach (['entitlements', 'skus'] as $table) { | |||||
Schema::table( | |||||
$table, | |||||
function (Blueprint $table) { | |||||
$table->integer('fee')->nullable(); | |||||
} | |||||
); | |||||
} | |||||
// FIXME: Should we also have package_skus.fee ? | |||||
// We have package_skus.cost, but I think it is not used anywhere. | |||||
} | |||||
/** | |||||
* Reverse the migrations. | |||||
* | |||||
* @return void | |||||
*/ | |||||
public function down() | |||||
{ | |||||
foreach (['users', 'discounts', 'domains', 'plans', 'packages', 'skus'] as $table_name) { | |||||
Schema::table( | |||||
$table_name, | |||||
function (Blueprint $table) { | |||||
$table->dropForeign(['tenant_id']); | |||||
$table->dropColumn('tenant_id'); | |||||
} | |||||
); | |||||
} | |||||
foreach (['entitlements', 'skus'] as $table) { | |||||
Schema::table( | |||||
$table, | |||||
function (Blueprint $table) { | |||||
$table->dropColumn('fee'); | |||||
} | |||||
); | |||||
} | |||||
Schema::dropIfExists('tenants'); | |||||
} | |||||
} |