studioespresso/craft-exporter

View on GitHub
src/migrations/Install.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php

namespace studioespresso\exporter\migrations;

use Craft;
use craft\db\Migration;
use craft\db\Table;
use studioespresso\exporter\records\ExportRecord;

class Install extends Migration
{
    /**
     * @inheritdoc
     */
    public function safeUp(): bool
    {
        if ($this->createTables()) {
            $this->addForeignKeys();

            // Refresh the db schema caches
            Craft::$app->getDb()->schema->refresh();
        }

        return true;
    }

    protected function createTables(): bool
    {
        if (!$this->db->tableExists(ExportRecord::tableName())) {
            $this->createTable(ExportRecord::tableName(), [
                'id' => $this->integer()->notNull(),
                'name' => $this->string()->notNull(),
                'elementType' => $this->string()->notNull(),
                'settings' => $this->longText(),
                'attributes' => $this->longText(),
                'fields' => $this->longText(),
                'runSettings' => $this->longText(),
                'dateCreated' => $this->dateTime()->notNull(),
                'dateUpdated' => $this->dateTime()->notNull(),
                'uid' => $this->uid(),
                'PRIMARY KEY([[id]])',
            ]);
        }
        return true;
    }

    protected function addForeignKeys(): void
    {
        $this->addForeignKey(null, ExportRecord::tableName(), 'id', Table::ELEMENTS, 'id', 'CASCADE');
    }

    public function safeDown()
    {
        $this->dropTableIfExists(ExportRecord::tableName());
        parent::safeDown(); // TODO: Change the autogenerated stub
    }
}