feat(migration): move integrations from tenants to clients and update schema
This commit is contained in:
parent
a2298c9de2
commit
817d0de6d2
|
|
@ -9,55 +9,63 @@ return new class extends Migration
|
||||||
{
|
{
|
||||||
public function up(): void
|
public function up(): void
|
||||||
{
|
{
|
||||||
Schema::create('client_integrations', function (Blueprint $table): void {
|
if (! Schema::hasTable('client_integrations')) {
|
||||||
$table->id();
|
Schema::create('client_integrations', function (Blueprint $table): void {
|
||||||
$table->foreignId('client_id')->constrained('clients')->cascadeOnDelete();
|
$table->id();
|
||||||
$table->string('integration_code');
|
$table->foreignId('client_id')->constrained('clients')->cascadeOnDelete();
|
||||||
$table->longText('integration_data')->nullable();
|
$table->string('integration_code');
|
||||||
$table->timestamps();
|
$table->longText('integration_data')->nullable();
|
||||||
|
$table->timestamps();
|
||||||
|
|
||||||
$table->foreign('integration_code')
|
$table->foreign('integration_code')
|
||||||
->references('integration_code')
|
->references('integration_code')
|
||||||
->on('integrations')
|
->on('integrations')
|
||||||
->cascadeOnDelete();
|
->cascadeOnDelete();
|
||||||
$table->unique(['client_id', 'integration_code']);
|
$table->unique(['client_id', 'integration_code']);
|
||||||
});
|
|
||||||
|
|
||||||
DB::table('tenant_integration')
|
|
||||||
->join('tenants', 'tenants.codigo', '=', 'tenant_integration.tenant_code')
|
|
||||||
->select([
|
|
||||||
'tenants.client_id',
|
|
||||||
'tenant_integration.integration_code',
|
|
||||||
'tenant_integration.integration_data',
|
|
||||||
'tenant_integration.created_at',
|
|
||||||
'tenant_integration.updated_at',
|
|
||||||
])
|
|
||||||
->orderBy('tenant_integration.id')
|
|
||||||
->each(function (object $configuration): void {
|
|
||||||
DB::table('client_integrations')->updateOrInsert(
|
|
||||||
[
|
|
||||||
'client_id' => $configuration->client_id,
|
|
||||||
'integration_code' => $configuration->integration_code,
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'integration_data' => $configuration->integration_data,
|
|
||||||
'created_at' => $configuration->created_at,
|
|
||||||
'updated_at' => $configuration->updated_at,
|
|
||||||
],
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
Schema::table('integrations', function (Blueprint $table): void {
|
if (Schema::hasTable('tenant_integration')) {
|
||||||
$table->boolean('requires_client_configuration')->default(true);
|
DB::table('tenant_integration')
|
||||||
});
|
->join('tenants', 'tenants.codigo', '=', 'tenant_integration.tenant_code')
|
||||||
|
->select([
|
||||||
|
'tenants.client_id',
|
||||||
|
'tenant_integration.integration_code',
|
||||||
|
'tenant_integration.integration_data',
|
||||||
|
'tenant_integration.created_at',
|
||||||
|
'tenant_integration.updated_at',
|
||||||
|
])
|
||||||
|
->orderBy('tenant_integration.id')
|
||||||
|
->each(function (object $configuration): void {
|
||||||
|
DB::table('client_integrations')->updateOrInsert(
|
||||||
|
[
|
||||||
|
'client_id' => $configuration->client_id,
|
||||||
|
'integration_code' => $configuration->integration_code,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'integration_data' => $configuration->integration_data,
|
||||||
|
'created_at' => $configuration->created_at,
|
||||||
|
'updated_at' => $configuration->updated_at,
|
||||||
|
],
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
DB::table('integrations')->update([
|
if (! Schema::hasColumn('integrations', 'requires_client_configuration')) {
|
||||||
'requires_client_configuration' => DB::raw('requires_tenant_configuration'),
|
Schema::table('integrations', function (Blueprint $table): void {
|
||||||
]);
|
$table->boolean('requires_client_configuration')->default(true);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
Schema::table('integrations', function (Blueprint $table): void {
|
if (Schema::hasColumn('integrations', 'requires_tenant_configuration')) {
|
||||||
$table->dropColumn('requires_tenant_configuration');
|
DB::table('integrations')->update([
|
||||||
});
|
'requires_client_configuration' => DB::raw('requires_tenant_configuration'),
|
||||||
|
]);
|
||||||
|
|
||||||
|
Schema::table('integrations', function (Blueprint $table): void {
|
||||||
|
$table->dropColumn('requires_tenant_configuration');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
Schema::dropIfExists('tenant_integration');
|
Schema::dropIfExists('tenant_integration');
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue