diff --git a/database/migrations/add_ldap_columns_to_users_table.php.stub b/database/migrations/add_ldap_columns_to_users_table.php.stub index 58bc8880..66afc94e 100644 --- a/database/migrations/add_ldap_columns_to_users_table.php.stub +++ b/database/migrations/add_ldap_columns_to_users_table.php.stub @@ -1,5 +1,6 @@ string('guid')->unique()->nullable(); + $driver = Schema::getConnection()->getDriverName(); + + Schema::table('users', function (Blueprint $table) use ($driver) { + $table->string('guid')->nullable(); $table->string('domain')->nullable(); + + if ($driver !== 'sqlsrv') { + $table->unique('guid'); + } }); + + if ($driver === 'sqlsrv') { + DB::statement( + $this->compileUniqueSqlServerIndexStatement('users', 'guid') + ); + } } /** * Reverse the migrations. + * + * @return void */ public function down() { @@ -26,4 +43,22 @@ class AddLdapColumnsToUsersTable extends Migration $table->dropColumn(['guid', 'domain']); }); } + + /** + * Compile a compatible "unique" SQL Server index constraint. + * + * @param string $table + * @param string $column + * + * @return string + */ + protected function compileUniqueSqlServerIndexStatement($table, $column) + { + return sprintf('create unique index %s on %s (%s) where %s is not null', + implode('_', [$table, $column, 'unique']), + $table, + $column, + $column + ); + } }