MOON
Server: Apache
System: Linux res.emeff.ca 3.10.0-962.3.2.lve1.5.24.10.el7.x86_64 #1 SMP Wed Mar 20 07:36:02 EDT 2019 x86_64
User: accemeff (1004)
PHP: 7.0.33
Disabled: NONE
Upload Files
File: /home/accemeff/vendor/craftcms/cms/src/migrations/m160829_000000_pending_user_content_cleanup.php
<?php

namespace craft\migrations;

use craft\db\Migration;
use craft\db\Query;
use craft\db\Table;
use craft\elements\Entry;

/**
 * The class name is the UTC timestamp in the format of mYYMMDD_HHMMSS_migrationName
 */
class m160829_000000_pending_user_content_cleanup extends Migration
{
    /**
     * Any migration code in here is wrapped inside of a transaction.
     *
     * @return bool
     */
    public function safeUp(): bool
    {
        // Find any orphaned entries.
        $ids = (new Query())
            ->select(['el.id'])
            ->from(['{{%elements}} el'])
            ->leftJoin('{{%entries}} en', '[[en.id]] = [[el.id]]')
            ->where([
                'el.type' => Entry::class,
                'en.id' => null
            ])
            ->column($this->db);

        if (!empty($ids)) {
            echo '    > Found ' . count($ids) . ' orphaned element IDs in the elements table: ' . implode(', ', $ids) . "\n";

            // Delete 'em
            $this->delete(Table::ELEMENTS, ['id' => $ids]);
        }

        return true;
    }

    /**
     * @inheritdoc
     */
    public function safeDown()
    {
        echo "m160829_000000_pending_user_content_cleanup cannot be reverted.\n";

        return false;
    }
}