Ensure column does not exist before creation

This commit is contained in:
Josef Citrine 2016-09-22 23:28:24 +01:00
parent abdfe2f698
commit 66ffd7714d

View file

@ -30,38 +30,40 @@ class AddDeletedAtColumnToActivities extends Migration
*/
public function up()
{
Schema::table('activities', function(Blueprint $table) {
$table->softDeletes()->index();
});
if (!Schema::hasColumn('activities', 'deleted_at')) {
Schema::table('activities', function (Blueprint $table) {
$table->softDeletes()->index();
});
// Retroactively fix activities that should be marked as deleted.
// Tracks
DB::table('activities')
->where('resource_type', 2)
->join('tracks', 'activities.resource_id', '=', 'tracks.id')
->whereNotNull('tracks.deleted_at')
->update(['deleted_at' => DB::raw('tracks.deleted_at')]);
// Retroactively fix activities that should be marked as deleted.
// Tracks
DB::table('activities')
->where('resource_type', 2)
->join('tracks', 'activities.resource_id', '=', 'tracks.id')
->whereNotNull('tracks.deleted_at')
->update(['deleted_at' => DB::raw('tracks.deleted_at')]);
// Albums
DB::table('activities')
->where('resource_type', 3)
->join('albums', 'activities.resource_id', '=', 'albums.id')
->whereNotNull('albums.deleted_at')
->update(['deleted_at' => DB::raw('albums.deleted_at')]);
// Albums
DB::table('activities')
->where('resource_type', 3)
->join('albums', 'activities.resource_id', '=', 'albums.id')
->whereNotNull('albums.deleted_at')
->update(['deleted_at' => DB::raw('albums.deleted_at')]);
// Playlists
DB::table('activities')
->where('resource_type', 4)
->join('playlists', 'activities.resource_id', '=', 'playlists.id')
->whereNotNull('playlists.deleted_at')
->update(['deleted_at' => DB::raw('playlists.deleted_at')]);
// Playlists
DB::table('activities')
->where('resource_type', 4)
->join('playlists', 'activities.resource_id', '=', 'playlists.id')
->whereNotNull('playlists.deleted_at')
->update(['deleted_at' => DB::raw('playlists.deleted_at')]);
// Comments
DB::table('activities')
->where('resource_type', 5)
->join('comments', 'activities.resource_id', '=', 'comments.id')
->whereNotNull('comments.deleted_at')
->update(['deleted_at' => DB::raw('comments.deleted_at')]);
// Comments
DB::table('activities')
->where('resource_type', 5)
->join('comments', 'activities.resource_id', '=', 'comments.id')
->whereNotNull('comments.deleted_at')
->update(['deleted_at' => DB::raw('comments.deleted_at')]);
}
}
/**