Skip to content
This repository was archived by the owner on Apr 19, 2025. It is now read-only.

Commit 26b1ded

Browse files
committed
Use conditional to determine the correct type for 'user_id' col to keep the project backwards compatible and make it compatible with Laravel 5.8 (not ideal)
1 parent c425e33 commit 26b1ded

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

src/database/migrations/2017_05_26_102832_create_two_factor_auths_table.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use Illuminate\Support\Facades\DB;
34
use Illuminate\Support\Facades\Schema;
45
use Illuminate\Database\Schema\Blueprint;
56
use Illuminate\Database\Migrations\Migration;
@@ -15,7 +16,27 @@ public function up()
1516
{
1617
Schema::create('two_factor_auths', function (Blueprint $table) {
1718
$table->string('id')->nullable();
18-
$table->bigIncrements('user_id');
19+
20+
// Laravel 5.8 changed the type of "id" on "users" from "increments"
21+
// to "bigIncrements". Hence, we potentially could run into trouble:
22+
//
23+
// 1. We update from 5.7 to 5.8 and keep using "increments"
24+
// 2. We start a fresh Laravel project which uses "bigIncrements"
25+
//
26+
// Both scenarios are possible and we need to be able to account for
27+
// it. The only way to do that in a reliable way is to check the type
28+
// of "id" on "users" before we create our foreign key.
29+
//
30+
// Why is this not ideal?
31+
//
32+
// 1. There now is a dependency on doctrine/dbal
33+
// 2. This sort of conditional logic has no place in a migration file
34+
if (DB::getDoctrineSchemaManager()->listTableDetails('users')->getColumn('id')->getType() instanceof \Doctrine\DBAL\Types\IntegerType) {
35+
$table->increments('user_id');
36+
} else {
37+
$table->bigIncrements('user_id');
38+
}
39+
1940
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
2041
$table->timestamps();
2142
});

0 commit comments

Comments
 (0)