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() public function up()
{ {
Schema::table('activities', function(Blueprint $table) { if (!Schema::hasColumn('activities', 'deleted_at')) {
$table->softDeletes()->index(); Schema::table('activities', function (Blueprint $table) {
}); $table->softDeletes()->index();
});
// Retroactively fix activities that should be marked as deleted. // Retroactively fix activities that should be marked as deleted.
// Tracks // Tracks
DB::table('activities') DB::table('activities')
->where('resource_type', 2) ->where('resource_type', 2)
->join('tracks', 'activities.resource_id', '=', 'tracks.id') ->join('tracks', 'activities.resource_id', '=', 'tracks.id')
->whereNotNull('tracks.deleted_at') ->whereNotNull('tracks.deleted_at')
->update(['deleted_at' => DB::raw('tracks.deleted_at')]); ->update(['deleted_at' => DB::raw('tracks.deleted_at')]);
// Albums // Albums
DB::table('activities') DB::table('activities')
->where('resource_type', 3) ->where('resource_type', 3)
->join('albums', 'activities.resource_id', '=', 'albums.id') ->join('albums', 'activities.resource_id', '=', 'albums.id')
->whereNotNull('albums.deleted_at') ->whereNotNull('albums.deleted_at')
->update(['deleted_at' => DB::raw('albums.deleted_at')]); ->update(['deleted_at' => DB::raw('albums.deleted_at')]);
// Playlists // Playlists
DB::table('activities') DB::table('activities')
->where('resource_type', 4) ->where('resource_type', 4)
->join('playlists', 'activities.resource_id', '=', 'playlists.id') ->join('playlists', 'activities.resource_id', '=', 'playlists.id')
->whereNotNull('playlists.deleted_at') ->whereNotNull('playlists.deleted_at')
->update(['deleted_at' => DB::raw('playlists.deleted_at')]); ->update(['deleted_at' => DB::raw('playlists.deleted_at')]);
// Comments // Comments
DB::table('activities') DB::table('activities')
->where('resource_type', 5) ->where('resource_type', 5)
->join('comments', 'activities.resource_id', '=', 'comments.id') ->join('comments', 'activities.resource_id', '=', 'comments.id')
->whereNotNull('comments.deleted_at') ->whereNotNull('comments.deleted_at')
->update(['deleted_at' => DB::raw('comments.deleted_at')]); ->update(['deleted_at' => DB::raw('comments.deleted_at')]);
}
} }
/** /**