@@ -17,24 +17,10 @@ public function up()
1717 Schema::create ('two_factor_auths ' , function (Blueprint $ table ) {
1818 $ table ->string ('id ' )->nullable ();
1919
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 ' );
20+ if ($ this ->isUnsignedInteger ()) {
21+ $ table ->unsignedInteger ('user_id ' );
3622 } else {
37- $ table ->bigIncrements ('user_id ' );
23+ $ table ->unsignedBigInteger ('user_id ' );
3824 }
3925
4026 $ table ->foreign ('user_id ' )->references ('id ' )->on ('users ' )->onDelete ('cascade ' );
@@ -51,4 +37,35 @@ public function down()
5137 {
5238 Schema::dropIfExists ('two_factor_auths ' );
5339 }
40+
41+ /**
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
61+ *
62+ * @return bool
63+ */
64+ private function isUnsignedInteger ()
65+ {
66+ return DB ::getDoctrineSchemaManager ()
67+ ->listTableDetails ('users ' )
68+ ->getColumn ('id ' )
69+ ->getType () instanceof \Doctrine \DBAL \Types \IntegerType;
70+ }
5471}
0 commit comments