Skip to content

Commit

Permalink
Stop associating drafts with revisions
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonkelly committed Jul 12, 2019
1 parent 179745d commit d0d74d4
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 48 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

### Changed
- Craft no longer ensures a recent revision exists before creating a draft for an element.

### Fixed
- Fixed a bug where multi-site element queries with the `unique` and `offset` params set weren’t returning any results.
- Fixed an error that could occur when creating a draft. ([#4515](https://github.com/craftcms/cms/issues/4515))
Expand Down
2 changes: 1 addition & 1 deletion src/config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
'id' => 'CraftCMS',
'name' => 'Craft CMS',
'version' => '3.2.1',
'schemaVersion' => '3.2.14',
'schemaVersion' => '3.2.15',
'minVersionRequired' => '2.6.2788',
'basePath' => dirname(__DIR__), // Defines the @app alias
'runtimePath' => '@storage/runtime', // Defines the @runtime alias
Expand Down
2 changes: 0 additions & 2 deletions src/migrations/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,6 @@ public function createTables()
$this->createTable(Table::DRAFTS, [
'id' => $this->primaryKey(),
'sourceId' => $this->integer(),
'revisionId' => $this->integer(),
'creatorId' => $this->integer()->notNull(),
'name' => $this->string()->notNull(),
'notes' => $this->text(),
Expand Down Expand Up @@ -899,7 +898,6 @@ public function addForeignKeys()
$this->addForeignKey(null, Table::CRAFTIDTOKENS, ['userId'], Table::USERS, ['id'], 'CASCADE', null);
$this->addForeignKey(null, Table::CONTENT, ['siteId'], Table::SITES, ['id'], 'CASCADE', 'CASCADE');
$this->addForeignKey(null, Table::DRAFTS, ['creatorId'], Table::USERS, ['id'], 'CASCADE', null);
$this->addForeignKey(null, Table::DRAFTS, ['revisionId'], Table::REVISIONS, ['id'], 'SET NULL', null);
$this->addForeignKey(null, Table::DRAFTS, ['sourceId'], Table::ELEMENTS, ['id'], 'CASCADE', null);
$this->addForeignKey(null, Table::ELEMENTS, ['draftId'], Table::DRAFTS, ['id'], 'CASCADE', null);
$this->addForeignKey(null, Table::ELEMENTS, ['revisionId'], Table::REVISIONS, ['id'], 'CASCADE', null);
Expand Down
2 changes: 0 additions & 2 deletions src/migrations/m190312_152740_element_revisions.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public function safeUp()
$this->createTable(Table::DRAFTS, [
'id' => $this->primaryKey(),
'sourceId' => $this->integer()->notNull(),
'revisionId' => $this->integer()->notNull(),
'creatorId' => $this->integer()->notNull(),
'name' => $this->string()->notNull(),
'notes' => $this->text(),
Expand All @@ -36,7 +35,6 @@ public function safeUp()
]);

$this->addForeignKey(null, Table::DRAFTS, ['creatorId'], Table::USERS, ['id'], 'CASCADE', null);
$this->addForeignKey(null, Table::DRAFTS, ['revisionId'], Table::REVISIONS, ['id'], 'CASCADE', null);
$this->addForeignKey(null, Table::DRAFTS, ['sourceId'], Table::ELEMENTS, ['id'], 'CASCADE', null);
$this->addForeignKey(null, Table::REVISIONS, ['creatorId'], Table::USERS, ['id'], 'CASCADE', null);
$this->addForeignKey(null, Table::REVISIONS, ['sourceId'], Table::ELEMENTS, ['id'], 'CASCADE', null);
Expand Down
2 changes: 0 additions & 2 deletions src/migrations/m190605_223807_unsaved_drafts.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ public function safeUp()
{
if ($this->db->getIsPgsql()) {
$this->execute('alter table ' . Table::DRAFTS . ' alter column [[sourceId]] drop not null');
$this->execute('alter table ' . Table::DRAFTS . ' alter column [[revisionId]] drop not null');
} else {
$this->alterColumn(Table::DRAFTS, 'sourceId', $this->integer());
$this->alterColumn(Table::DRAFTS, 'revisionId', $this->integer());
}
}

Expand Down
32 changes: 0 additions & 32 deletions src/migrations/m190709_111144_nullable_revision_id.php

This file was deleted.

34 changes: 34 additions & 0 deletions src/migrations/m190712_195914_no_draft_revisions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace craft\migrations;

use Craft;
use craft\db\Migration;
use craft\db\Table;
use craft\helpers\MigrationHelper;

/**
* m190712_195914_no_draft_revisions migration.
*/
class m190712_195914_no_draft_revisions extends Migration
{
/**
* @inheritdoc
*/
public function safeUp()
{
if ($this->db->columnExists(Table::DRAFTS, 'revisionId')) {
MigrationHelper::dropForeignKeyIfExists(Table::DRAFTS, ['revisionId'], $this);
$this->dropColumn(Table::DRAFTS, 'revisionId');
}
}

/**
* @inheritdoc
*/
public function safeDown()
{
echo "m190712_195914_no_draft_revisions cannot be reverted.\n";
return false;
}
}
12 changes: 3 additions & 9 deletions src/services/Drafts.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,8 @@ public function createDraft(ElementInterface $source, int $creatorId, string $na

$transaction = Craft::$app->getDb()->beginTransaction();
try {
// Create a new revision for the source element, or get the latest if nothing has changed since then
/** @var Element|RevisionBehavior $revision */
$revision = Craft::$app->getRevisions()->createRevision($source, $creatorId, 'Created automatically for draft');

// Create the draft row
$draftId = $this->_insertDraftRow($source->id, $revision->revisionId, $creatorId, $name, $notes);
$draftId = $this->_insertDraftRow($source->id, $creatorId, $name, $notes);

$newAttributes['draftId'] = $draftId;
$newAttributes['behaviors']['draft'] = [
Expand Down Expand Up @@ -187,7 +183,7 @@ public function saveElementAsDraft(ElementInterface $element, int $creatorId, st
}

// Create the draft row
$draftId = $this->_insertDraftRow(null, null, $creatorId, $name, $notes);
$draftId = $this->_insertDraftRow(null, $creatorId, $name, $notes);

/** @var Element $element */
$element->draftId = $draftId;
Expand Down Expand Up @@ -339,17 +335,15 @@ public function purgeUnsavedDrafts()
* @param string|null $name
* @param string|null $notes
* @param int|null $sourceId
* @param int|null $revisionId
* @return int The new draft ID
* @throws DbException
*/
private function _insertDraftRow(int $sourceId = null, int $revisionId = null, int $creatorId, string $name = null, string $notes = null): int
private function _insertDraftRow(int $sourceId = null, int $creatorId, string $name = null, string $notes = null): int
{
$db = Craft::$app->getDb();
$db->createCommand()
->insert(Table::DRAFTS, [
'sourceId' => $sourceId,
'revisionId' => $revisionId,
'creatorId' => $creatorId,
'name' => $name,
'notes' => $notes,
Expand Down

0 comments on commit d0d74d4

Please sign in to comment.