<?php

use App\DataMigration;
use Illuminate\Support\Facades\DB;

class DataMigrationV2_2_0_alpha_1 extends DataMigration
{
    public function getVersion()
    {
        return '2.2.0-alpha.1';
    }

    public function run($currentVersion)
    {
        // Insert photo.uploaded events
        DB::insert('INSERT INTO user_activity (user_id, activity_at, type, photo_id, created_at, updated_at)
SELECT photos.user_id, photos.created_at, \'photo.uploaded\', photos.id, NOW(), NOW()
FROM photos
LEFT OUTER JOIN user_activity ON (user_activity.user_id = photos.user_id AND user_activity.activity_at = photos.created_at AND user_activity.type = \'photo.uploaded\' AND user_activity.photo_id = photos.id)
WHERE user_activity.id IS NULL');

        // Insert photo.taken events
        DB::insert('INSERT INTO user_activity (user_id, activity_at, type, photo_id, created_at, updated_at)
SELECT photos.user_id, photos.taken_at, \'photo.taken\', photos.id, NOW(), NOW()
FROM photos
LEFT OUTER JOIN user_activity ON (user_activity.user_id = photos.user_id AND user_activity.activity_at = photos.taken_at AND user_activity.type = \'photo.taken\' AND user_activity.photo_id = photos.id)
WHERE user_activity.id IS NULL AND photos.taken_at IS NOT NULL');

        // Insert album.created events
        DB::insert('INSERT INTO user_activity (user_id, activity_at, type, album_id, created_at, updated_at)
SELECT albums.user_id, albums.created_at, \'album.created\', albums.id, NOW(), NOW()
FROM albums
LEFT OUTER JOIN user_activity ON (user_activity.user_id = albums.user_id AND user_activity.activity_at = albums.created_at AND user_activity.type = \'album.created\' AND user_activity.album_id = albums.id)
WHERE user_activity.id IS NULL');

        // Insert user.created events
        DB::insert('INSERT INTO user_activity (user_id, activity_at, type, created_at, updated_at)
SELECT users.id, users.created_at, \'user.created\', NOW(), NOW()
FROM users
LEFT OUTER JOIN user_activity ON (user_activity.user_id = users.id AND user_activity.activity_at = users.created_at AND user_activity.type = \'user.created\')
WHERE user_activity.id IS NULL');
    }
}