summaryrefslogtreecommitdiffstats
path: root/lib/Migration
diff options
context:
space:
mode:
authorSean Molenaar <sean@seanmolenaar.eu>2020-10-04 20:45:33 +0200
committerBenjamin Brahmer <info@b-brahmer.de>2020-11-03 19:58:37 +0100
commit10e8c28feaf6d858948285a291231f651ef74728 (patch)
tree5c48216dc1ce6a6dd8f292d489bb4a7d03cb5595 /lib/Migration
parent361cfa55b7302b29e158647060085550b9a3dfe1 (diff)
Add migration with foreign keys
Closes #829 Signed-off-by: Sean Molenaar <sean@seanmolenaar.eu>
Diffstat (limited to 'lib/Migration')
-rw-r--r--lib/Migration/Version150005Date20201009192341.php98
1 files changed, 98 insertions, 0 deletions
diff --git a/lib/Migration/Version150005Date20201009192341.php b/lib/Migration/Version150005Date20201009192341.php
new file mode 100644
index 000000000..937d3f50b
--- /dev/null
+++ b/lib/Migration/Version150005Date20201009192341.php
@@ -0,0 +1,98 @@
+<?php
+
+declare(strict_types=1);
+
+namespace OCA\News\Migration;
+
+use Closure;
+use OCP\DB\ISchemaWrapper;
+use OCP\DB\QueryBuilder\IQueryBuilder;
+use OCP\IDBConnection;
+use OCP\Migration\IOutput;
+use OCP\Migration\SimpleMigrationStep;
+
+/**
+ * Auto-generated migration step: Please modify to your needs!
+ */
+class Version150005Date20201009192341 extends SimpleMigrationStep {
+
+ protected $connection;
+
+ public function __construct(IDBConnection $connection)
+ {
+ $this->connection = $connection;
+ }
+
+ /**
+ * @param IOutput $output
+ * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
+ * @param array $options
+ */
+ public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options) {
+ $qb = $this->connection->getQueryBuilder();
+
+ $qb->update('news_feeds')
+ ->set('folder_id', $qb->createPositionalParameter(null, IQueryBuilder::PARAM_NULL))
+ ->where('folder_id = 0')
+ ->execute();
+
+ $feed_name = $this->connection->getQueryBuilder()->getTableName('news_feeds');
+ $folder_name = $this->connection->getQueryBuilder()->getTableName('news_folders');
+
+ $items_query = "DELETE FROM ${feed_name} WHERE ${feed_name}.`folder_id` NOT IN (SELECT DISTINCT id FROM ${folder_name}) AND ${feed_name}.`folder_id` IS NOT NULL";
+ $this->connection->executeQuery($items_query);
+
+ $item_name = $this->connection->getQueryBuilder()->getTableName('news_items');
+ $feed_name = $this->connection->getQueryBuilder()->getTableName('news_feeds');
+
+ $items_query = "DELETE FROM ${item_name} WHERE ${item_name}.`feed_id` NOT IN (SELECT DISTINCT id FROM ${feed_name})";
+ $this->connection->executeQuery($items_query);
+ }
+
+ /**
+ * @param IOutput $output
+ * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
+ * @param array $options
+ * @return null|ISchemaWrapper
+ */
+ public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) {
+ /** @var ISchemaWrapper $schema */
+ $schema = $schemaClosure();
+
+ if ($schema->hasTable('news_items') &&
+ !$schema->getTable('news_items')->hasForeignKey('feed')) {
+
+ $schema->getTable('news_items')
+ ->addForeignKeyConstraint(
+ $schema->getTable('news_feeds')->getName(),
+ ['feed_id'],
+ ['id'],
+ ['onDelete' => 'CASCADE'],
+ 'feed'
+ );
+ }
+
+ if ($schema->hasTable('news_feeds') &&
+ !$schema->getTable('news_feeds')->hasForeignKey('folder')) {
+
+ $schema->getTable('news_feeds')
+ ->addForeignKeyConstraint(
+ $schema->getTable('news_folders')->getName(),
+ ['folder_id'],
+ ['id'],
+ ['onDelete' => 'CASCADE'],
+ 'folder'
+ );
+ }
+
+ return $schema;
+ }
+
+ /**
+ * @param IOutput $output
+ * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
+ * @param array $options
+ */
+ public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options) {
+ }
+}