Merge pull request #1656 from flarum/tz/fix-index-names

Fix index names in migrations
This commit is contained in:
Toby Zerner 2018-11-27 22:32:17 +10:30 committed by GitHub
commit 546b4f01ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 135 additions and 32 deletions

View File

@ -9,6 +9,7 @@
* file that was distributed with this source code.
*/
use Flarum\Database\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;
@ -23,14 +24,18 @@ return [
})
->delete();
$schema->table('access_tokens', function (Blueprint $table) {
$schema->table('access_tokens', function (Blueprint $table) use ($schema) {
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
Migration::fixIndexNames($schema, $table);
});
},
'down' => function (Builder $schema) {
$schema->table('access_tokens', function (Blueprint $table) {
$schema->table('access_tokens', function (Blueprint $table) use ($schema) {
$table->dropForeign(['user_id']);
Migration::fixIndexNames($schema, $table);
});
}
];

View File

@ -9,18 +9,21 @@
* file that was distributed with this source code.
*/
use Flarum\Database\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;
return [
'up' => function (Builder $schema) {
$schema->table('api_keys', function (Blueprint $table) {
$schema->table('api_keys', function (Blueprint $table) use ($schema) {
$table->dropPrimary(['id']);
$table->renameColumn('id', 'key');
$table->unique('key');
Migration::fixIndexNames($schema, $table);
});
$schema->table('api_keys', function (Blueprint $table) {
$schema->table('api_keys', function (Blueprint $table) use ($schema) {
$table->increments('id');
$table->string('allowed_ips')->nullable();
$table->string('scopes')->nullable();
@ -29,19 +32,25 @@ return [
$table->dateTime('last_activity_at')->nullable();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
Migration::fixIndexNames($schema, $table);
});
},
'down' => function (Builder $schema) {
$schema->table('api_keys', function (Blueprint $table) {
$schema->table('api_keys', function (Blueprint $table) use ($schema) {
$table->dropForeign(['user_id']);
$table->dropColumn('id', 'allowed_ips', 'user_id', 'scopes', 'created_at');
Migration::fixIndexNames($schema, $table);
});
$schema->table('api_keys', function (Blueprint $table) {
$schema->table('api_keys', function (Blueprint $table) use ($schema) {
$table->dropUnique(['key']);
$table->renameColumn('key', 'id');
$table->primary('id');
Migration::fixIndexNames($schema, $table);
});
}
];

View File

@ -9,6 +9,7 @@
* file that was distributed with this source code.
*/
use Flarum\Database\Migration;
use Illuminate\Database\Query\Expression;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;
@ -33,21 +34,25 @@ return [
'last_post_id' => $selectId('posts', 'last_post_id'),
]);
$schema->table('discussions', function (Blueprint $table) {
$schema->table('discussions', function (Blueprint $table) use ($schema) {
$table->foreign('user_id')->references('id')->on('users')->onDelete('set null');
$table->foreign('last_posted_user_id')->references('id')->on('users')->onDelete('set null');
$table->foreign('hidden_user_id')->references('id')->on('users')->onDelete('set null');
$table->foreign('first_post_id')->references('id')->on('posts')->onDelete('set null');
$table->foreign('last_post_id')->references('id')->on('posts')->onDelete('set null');
Migration::fixIndexNames($schema, $table);
});
},
'down' => function (Builder $schema) {
$schema->table('discussions', function (Blueprint $table) {
$schema->table('discussions', function (Blueprint $table) use ($schema) {
$table->dropForeign([
'user_id', 'last_posted_user_id', 'hidden_user_id',
'first_post_id', 'last_post_id'
]);
Migration::fixIndexNames($schema, $table);
});
}
];

View File

@ -9,6 +9,7 @@
* file that was distributed with this source code.
*/
use Flarum\Database\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;
@ -26,15 +27,19 @@ return [
})
->delete();
$schema->table('discussion_user', function (Blueprint $table) {
$schema->table('discussion_user', function (Blueprint $table) use ($schema) {
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('discussion_id')->references('id')->on('discussions')->onDelete('cascade');
Migration::fixIndexNames($schema, $table);
});
},
'down' => function (Builder $schema) {
$schema->table('discussion_user', function (Blueprint $table) {
$schema->table('discussion_user', function (Blueprint $table) use ($schema) {
$table->dropForeign(['user_id', 'discussion_id']);
Migration::fixIndexNames($schema, $table);
});
}
];

View File

@ -9,6 +9,7 @@
* file that was distributed with this source code.
*/
use Flarum\Database\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;
@ -23,14 +24,18 @@ return [
})
->delete();
$schema->table('email_tokens', function (Blueprint $table) {
$schema->table('email_tokens', function (Blueprint $table) use ($schema) {
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
Migration::fixIndexNames($schema, $table);
});
},
'down' => function (Builder $schema) {
$schema->table('email_tokens', function (Blueprint $table) {
$schema->table('email_tokens', function (Blueprint $table) use ($schema) {
$table->dropForeign(['user_id']);
Migration::fixIndexNames($schema, $table);
});
}
];

View File

@ -9,6 +9,7 @@
* file that was distributed with this source code.
*/
use Flarum\Database\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;
@ -23,14 +24,18 @@ return [
})
->delete();
$schema->table('group_permission', function (Blueprint $table) {
$schema->table('group_permission', function (Blueprint $table) use ($schema) {
$table->foreign('group_id')->references('id')->on('groups')->onDelete('cascade');
Migration::fixIndexNames($schema, $table);
});
},
'down' => function (Builder $schema) {
$schema->table('group_permission', function (Blueprint $table) {
$schema->table('group_permission', function (Blueprint $table) use ($schema) {
$table->dropForeign(['group_id']);
Migration::fixIndexNames($schema, $table);
});
}
];

View File

@ -9,6 +9,7 @@
* file that was distributed with this source code.
*/
use Flarum\Database\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;
@ -26,15 +27,19 @@ return [
})
->delete();
$schema->table('group_user', function (Blueprint $table) {
$schema->table('group_user', function (Blueprint $table) use ($schema) {
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('group_id')->references('id')->on('groups')->onDelete('cascade');
Migration::fixIndexNames($schema, $table);
});
},
'down' => function (Builder $schema) {
$schema->table('group_user', function (Blueprint $table) {
$schema->table('group_user', function (Blueprint $table) use ($schema) {
$table->dropForeign(['user_id', 'group_id']);
Migration::fixIndexNames($schema, $table);
});
}
];

View File

@ -9,6 +9,7 @@
* file that was distributed with this source code.
*/
use Flarum\Database\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;
@ -30,15 +31,19 @@ return [
})
->update(['from_user_id' => null]);
$schema->table('notifications', function (Blueprint $table) {
$schema->table('notifications', function (Blueprint $table) use ($schema) {
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('from_user_id')->references('id')->on('users')->onDelete('set null');
Migration::fixIndexNames($schema, $table);
});
},
'down' => function (Builder $schema) {
$schema->table('notifications', function (Blueprint $table) {
$schema->table('notifications', function (Blueprint $table) use ($schema) {
$table->dropForeign(['user_id', 'from_user_id']);
Migration::fixIndexNames($schema, $table);
});
}
];

View File

@ -9,6 +9,7 @@
* file that was distributed with this source code.
*/
use Flarum\Database\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;
@ -23,14 +24,18 @@ return [
})
->delete();
$schema->table('password_tokens', function (Blueprint $table) {
$schema->table('password_tokens', function (Blueprint $table) use ($schema) {
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
Migration::fixIndexNames($schema, $table);
});
},
'down' => function (Builder $schema) {
$schema->table('password_tokens', function (Blueprint $table) {
$schema->table('password_tokens', function (Blueprint $table) use ($schema) {
$table->dropForeign(['user_id']);
Migration::fixIndexNames($schema, $table);
});
}
];

View File

@ -9,6 +9,7 @@
* file that was distributed with this source code.
*/
use Flarum\Database\Migration;
use Illuminate\Database\Query\Expression;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;
@ -31,19 +32,23 @@ return [
'hidden_user_id' => $selectId('users', 'hidden_user_id'),
]);
$schema->table('posts', function (Blueprint $table) {
$schema->table('posts', function (Blueprint $table) use ($schema) {
$table->foreign('user_id')->references('id')->on('users')->onDelete('set null');
$table->foreign('edited_user_id')->references('id')->on('users')->onDelete('set null');
$table->foreign('hidden_user_id')->references('id')->on('users')->onDelete('set null');
Migration::fixIndexNames($schema, $table);
});
},
'down' => function (Builder $schema) {
$schema->table('posts', function (Blueprint $table) {
$schema->table('posts', function (Blueprint $table) use ($schema) {
$table->dropForeign([
'user_id', 'discussion_id',
'edited_user_id', 'hidden_user_id'
]);
Migration::fixIndexNames($schema, $table);
});
}
];

View File

@ -9,25 +9,30 @@
* file that was distributed with this source code.
*/
use Flarum\Database\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;
return [
'up' => function (Builder $schema) {
$schema->table('users', function (Blueprint $table) {
$schema->table('users', function (Blueprint $table) use ($schema) {
$table->index('joined_at');
$table->index('last_seen_at');
$table->index('discussion_count');
$table->index('comment_count');
Migration::fixIndexNames($schema, $table);
});
},
'down' => function (Builder $schema) {
$schema->table('users', function (Blueprint $table) {
$schema->table('users', function (Blueprint $table) use ($schema) {
$table->dropIndex(['joined_at']);
$table->dropIndex(['last_seen_at']);
$table->dropIndex(['discussion_count']);
$table->dropIndex(['comment_count']);
Migration::fixIndexNames($schema, $table);
});
}
];

View File

@ -9,12 +9,13 @@
* file that was distributed with this source code.
*/
use Flarum\Database\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;
return [
'up' => function (Builder $schema) {
$schema->table('discussions', function (Blueprint $table) {
$schema->table('discussions', function (Blueprint $table) use ($schema) {
$table->index('last_posted_at');
$table->index('last_posted_user_id');
$table->index('created_at');
@ -22,11 +23,13 @@ return [
$table->index('comment_count');
$table->index('participant_count');
$table->index('hidden_at');
Migration::fixIndexNames($schema, $table);
});
},
'down' => function (Builder $schema) {
$schema->table('discussions', function (Blueprint $table) {
$schema->table('discussions', function (Blueprint $table) use ($schema) {
$table->dropIndex(['last_posted_at']);
$table->dropIndex(['last_posted_user_id']);
$table->dropIndex(['created_at']);
@ -34,6 +37,8 @@ return [
$table->dropIndex(['comment_count']);
$table->dropIndex(['participant_count']);
$table->dropIndex(['hidden_at']);
Migration::fixIndexNames($schema, $table);
});
}
];

View File

@ -9,19 +9,24 @@
* file that was distributed with this source code.
*/
use Flarum\Database\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;
return [
'up' => function (Builder $schema) {
$schema->table('notifications', function (Blueprint $table) {
$schema->table('notifications', function (Blueprint $table) use ($schema) {
$table->index('user_id');
Migration::fixIndexNames($schema, $table);
});
},
'down' => function (Builder $schema) {
$schema->table('notifications', function (Blueprint $table) {
$schema->table('notifications', function (Blueprint $table) use ($schema) {
$table->dropIndex(['user_id']);
Migration::fixIndexNames($schema, $table);
});
}
];

View File

@ -9,23 +9,28 @@
* file that was distributed with this source code.
*/
use Flarum\Database\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;
return [
'up' => function (Builder $schema) {
$schema->table('posts', function (Blueprint $table) {
$schema->table('posts', function (Blueprint $table) use ($schema) {
$table->index(['discussion_id', 'number']);
$table->index(['discussion_id', 'created_at']);
$table->index(['user_id', 'created_at']);
Migration::fixIndexNames($schema, $table);
});
},
'down' => function (Builder $schema) {
$schema->table('posts', function (Blueprint $table) {
$schema->table('posts', function (Blueprint $table) use ($schema) {
$table->dropIndex(['discussion_id', 'number']);
$table->dropIndex(['discussion_id', 'created_at']);
$table->dropIndex(['user_id', 'created_at']);
Migration::fixIndexNames($schema, $table);
});
}
];

View File

@ -29,7 +29,11 @@ abstract class Migration
{
return [
'up' => function (Builder $schema) use ($name, $definition) {
$schema->create($name, $definition);
$schema->create($name, function (Blueprint $table) use ($schema, $definition) {
$definition($table);
static::fixIndexNames($schema, $table);
});
},
'down' => function (Builder $schema) use ($name) {
$schema->drop($name);
@ -59,11 +63,13 @@ abstract class Migration
{
return [
'up' => function (Builder $schema) use ($tableName, $columnDefinitions) {
$schema->table($tableName, function (Blueprint $table) use ($columnDefinitions) {
$schema->table($tableName, function (Blueprint $table) use ($schema, $columnDefinitions) {
foreach ($columnDefinitions as $columnName => $options) {
$type = array_shift($options);
$table->addColumn($type, $columnName, $options);
}
Migration::fixIndexNames($schema, $table);
});
},
'down' => function (Builder $schema) use ($tableName, $columnDefinitions) {
@ -187,4 +193,27 @@ abstract class Migration
}
];
}
/**
* Add a prefix to index names on the given table blueprint.
*
* Laravel 5.5 doesn't automatically add the table prefix to index
* names, but this has been fixed in 5.7. We will manually fix the
* names for now, and we can remove this when we upgrade to 5.7.
*/
public static function fixIndexNames(Builder $schema, Blueprint $table)
{
$indexCommands = [
'unique', 'index', 'spatialIndex', 'foreign',
'dropUnique', 'dropIndex', 'dropSpatialIndex', 'dropForeign'
];
$prefix = $schema->getConnection()->getTablePrefix();
foreach ($table->getCommands() as $command) {
if (in_array($command->name, $indexCommands)) {
$command->index = $prefix.$command->index;
}
}
}
}