Skip to content

Commit

Permalink
Support SQL Server unique indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
stevebauman committed Nov 2, 2022
1 parent e2e0439 commit 64f7215
Showing 1 changed file with 37 additions and 2 deletions.
39 changes: 37 additions & 2 deletions database/migrations/add_ldap_columns_to_users_table.php.stub
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
Expand All @@ -8,22 +9,56 @@ class AddLdapColumnsToUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->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()
{
Schema::table('users', function (Blueprint $table) {
$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
);
}
}

0 comments on commit 64f7215

Please sign in to comment.