Skip to content

Commit

Permalink
Fix migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
internetztube committed Jun 18, 2024
1 parent 62f53d5 commit b019f39
Showing 1 changed file with 18 additions and 26 deletions.
44 changes: 18 additions & 26 deletions src/migrations/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,35 @@

use Craft;
use craft\db\Migration;
use craft\db\Table;
use internetztube\elementRelations\records\ElementRelationsRecord;
use internetztube\elementRelations\records\ElementRelationsCacheRecord;

class Install extends Migration
{
public function safeUp()
{
$table = ElementRelationsRecord::tableName();
if (!$this->db->tableExists($table)) {
$this->createTable($table, [
"id" => $this->primaryKey(),
"type" => $this->string(32),
"sourceElementId" => $this->integer()->null(),
"sourceSiteId" => $this->integer()->null(),
"targetElementId" => $this->integer()->notNull(),
"targetSiteId" => $this->integer()->null(),
"dateCreated" => $this->dateTime()->notNull(),
"dateUpdated" => $this->dateTime()->notNull(),
"uid" => $this->uid(),
]);

$this->createIndex(null, $table, ["targetElementId", "targetSiteId", "type"], false);
$this->addForeignKey(null, $table, "sourceElementId", Table::ELEMENTS, "id", "CASCADE");
$this->addForeignKey(null, $table, "targetElementId", Table::ELEMENTS, "id", "CASCADE");
$this->addForeignKey(null, $table, "sourceSiteId", Table::SITES, "id", "CASCADE");
$this->addForeignKey(null, $table, "targetSiteId", Table::SITES, "id", "CASCADE");

// Refresh the db schema caches
Craft::$app->db->schema->refresh();
}
$table = ElementRelationsCacheRecord::tableName();
$this->createTable($table, [
'id' => $this->primaryKey(),
'sourceElementId' => $this->integer()->notNull(),
'sourceSiteId' => $this->integer()->notNull(),
'sourcePrimaryOwnerId' => $this->integer()->notNull(),
'targetElementId' => $this->integer()->notNull(),
'targetSiteId' => $this->integer()->notNull(),
'customFieldUid' => $this->string(),
'fieldId' => $this->integer(),
'type' => $this->string()->notNull(),
'dateCreated' => $this->dateTime()->notNull(),
'dateUpdated' => $this->dateTime()->notNull(),
'uid' => $this->uid(),
]);

// No foreign keys, since elements can be linked that are not there anymore, like in Redactor, CkEditor, ....
return true;
}

public function safeDown()
{
$table = ElementRelationsRecord::tableName();
$table = ElementRelationsCacheRecord::tableName();
if ($this->db->tableExists($table)) {
$this->dropTable($table);
Craft::$app->db->schema->refresh();
Expand Down

0 comments on commit b019f39

Please sign in to comment.