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

Commit 60e56bb

Browse files
committed
Let user decide which type to use for 'user_id' col
1 parent 53f65e4 commit 60e56bb

File tree

2 files changed

+27
-27
lines changed

2 files changed

+27
-27
lines changed

src/config/twofactor-auth.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,24 @@
6464
'post' => '/auth/token',
6565
],
6666

67+
/*
68+
|--------------------------------------------------------------------------
69+
| Database Settings
70+
|--------------------------------------------------------------------------
71+
|
72+
| Starting from Laravel 5.8, the default is to use "bigIncrements" instead
73+
| of "increments" for the "id" column on the "users" table. This setting
74+
| allows you to control what type to use for the "user_id" column on the
75+
| "two_factor_auths" table. The default is to use "unsignedBigInteger" in
76+
| order to stay in line with the changes in Laravel 5.8.
77+
|
78+
| NOTE: Modifying this setting only has an effect before you run any
79+
| migrations for this package. If you need to change the signature of
80+
| "user_id" afterwards, you will have to write your own migration for this
81+
| (see install instructions for more details).
82+
|
83+
*/
84+
85+
'big_int' => true,
86+
6787
];

src/database/migrations/2017_05_26_102832_create_two_factor_auths_table.php

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ public function up()
1717
Schema::create('two_factor_auths', function (Blueprint $table) {
1818
$table->string('id')->nullable();
1919

20-
if ($this->isUnsignedInteger()) {
21-
$table->unsignedInteger('user_id');
22-
} else {
20+
if ($this->useBigInt()) {
2321
$table->unsignedBigInteger('user_id');
22+
} else {
23+
$table->unsignedInteger('user_id');
2424
}
2525

2626
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
@@ -39,33 +39,13 @@ public function down()
3939
}
4040

4141
/**
42-
* Determine if the column type for "user_id" should be of type "unsignedInteger"
43-
* using the type of "id" on "users" as a reference.
44-
*
45-
* Why do we do this?
46-
*
47-
* Laravel 5.8 changed the type of "id" on "users" from "increments"
48-
* to "bigIncrements". Hence, we potentially could run into trouble if we
49-
* don't support the following two potential scenarios
50-
*
51-
* 1. We update from 5.7 to 5.8, but decide to keep using "increments"
52-
* 2. We start a fresh Laravel project which uses "bigIncrements" by default
53-
*
54-
* The only way to account for both scenarios in a reliable way is to check
55-
* the type of "id" on "users" before we create our foreign key.
56-
*
57-
* Why is this not ideal?
58-
*
59-
* 1. There now is a dependency on doctrine/dbal
60-
* 2. We now have to use conditional logic in our migration file
42+
* Determine if the type of the "user_id" column should be of type
43+
* "unsignedBigInteger".
6144
*
6245
* @return bool
6346
*/
64-
private function isUnsignedInteger()
47+
private function useBigInt()
6548
{
66-
return DB::getDoctrineSchemaManager()
67-
->listTableDetails('users')
68-
->getColumn('id')
69-
->getType() instanceof \Doctrine\DBAL\Types\IntegerType;
49+
return config('twofactor-auth.big_int');
7050
}
7151
}

0 commit comments

Comments
 (0)