Browse code

Add migration from the old root page based config to the new config table

Benjamin Roth authored on08/05/2025 14:03:22
Showing2 changed files
... ...
@@ -14,3 +14,7 @@ services:
14 14
         arguments:
15 15
             - "@database_connection"
16 16
             - "@logger"
17
+
18
+    vonRotenberg\MemberfilesBundle\Migration\MemberfilesConfigMigration:
19
+        tags:
20
+            - { name: contao.migration, priority: 0 }
17 21
new file mode 100644
... ...
@@ -0,0 +1,109 @@
1
+<?php
2
+
3
+declare(strict_types=1);
4
+
5
+namespace vonRotenberg\MemberfilesBundle\Migration;
6
+
7
+use Contao\CoreBundle\Migration\AbstractMigration;
8
+use Contao\CoreBundle\Migration\MigrationResult;
9
+use Doctrine\DBAL\Connection;
10
+use Doctrine\DBAL\Schema\Schema;
11
+
12
+class MemberfilesConfigMigration extends AbstractMigration
13
+{
14
+    private Connection $connection;
15
+
16
+    public function __construct(Connection $connection)
17
+    {
18
+        $this->connection = $connection;
19
+    }
20
+
21
+    public function shouldRun(): bool
22
+    {
23
+        $schemaManager = $this->connection->createSchemaManager();
24
+
25
+        // Prüfen ob die Quelltabelle existiert
26
+        if (!$schemaManager->tablesExist(['tl_page'])) {
27
+            return false;
28
+        }
29
+
30
+        // Prüfen ob die notwendige Spalte in tl_page existiert
31
+        $columns = $schemaManager->listTableColumns('tl_page');
32
+        if (!isset($columns['securedownloadsenabled'])) {
33
+            return false;
34
+        }
35
+
36
+        // Prüfen ob es Einträge gibt, die migriert werden müssen
37
+        $result = $this->connection->fetchOne(
38
+            "SELECT COUNT(*) FROM tl_page WHERE secureDownloadsEnabled='1'"
39
+        );
40
+
41
+        return $result > 0;
42
+    }
43
+
44
+    public function run(): MigrationResult
45
+    {
46
+        $schemaManager = $this->connection->createSchemaManager();
47
+
48
+        // Erstelle die Zieltabelle, falls sie nicht existiert
49
+        if (!$schemaManager->tablesExist(['tl_memberfiles_config'])) {
50
+            $this->createMemberfilesConfigTable();
51
+        }
52
+
53
+        $pages = $this->connection->fetchAllAssociative(
54
+            "SELECT id, title, secureDownloadsSRC, secureDownloadsTarget, " .
55
+            "secureDownloadsRegExp, secureDownloadsFields, sd_nc_enable, sd_nc_notification " .
56
+            "FROM tl_page WHERE secureDownloadsEnabled='1'"
57
+        );
58
+
59
+        foreach ($pages as $page) {
60
+            $this->connection->insert('tl_memberfiles_config', [
61
+                'tstamp' => time(),
62
+                'title' => $page['title'] . ' (migriert von Seite ID ' . $page['id'] . ')',
63
+                'source' => $page['secureDownloadsSRC'],
64
+                'target' => $page['secureDownloadsTarget'],
65
+                '`regexp`' => $page['secureDownloadsRegExp'],
66
+                'fields' => $page['secureDownloadsFields'],
67
+                'hasNotification' => $page['sd_nc_enable'],
68
+                'notification' => $page['sd_nc_notification'],
69
+                'enabled' => '1'
70
+            ]);
71
+
72
+            $this->connection->update('tl_page', ['secureDownloadsEnabled' => ''], ['id' => $page['id']]);
73
+        }
74
+
75
+
76
+
77
+        return new MigrationResult(
78
+            true,
79
+            sprintf('Erfolgreich %d Secure Downloads Konfigurationen von tl_page nach tl_memberfiles_config migriert.', count($pages))
80
+        );
81
+    }
82
+
83
+    private function createMemberfilesConfigTable(): void
84
+    {
85
+        $schema = new Schema();
86
+        $table = $schema->createTable('tl_memberfiles_config');
87
+
88
+        // Pflichtfelder
89
+        $table->addColumn('id', 'integer', ['unsigned' => true, 'autoincrement' => true]);
90
+        $table->addColumn('tstamp', 'integer', ['unsigned' => true, 'default' => 0]);
91
+        $table->addColumn('title', 'string', ['length' => 255, 'default' => '']);
92
+        $table->addColumn('source', 'binary', ['length' => 16, 'fixed' => true, 'notnull' => false]);
93
+        $table->addColumn('target', 'binary', ['length' => 16, 'fixed' => true, 'notnull' => false]);
94
+        $table->addColumn('regexp', 'string', ['length' => 64, 'notnull' => true, 'default' => '']);
95
+        $table->addColumn('fields', 'blob', ['notnull' => false]);
96
+        $table->addColumn('hasNotification', 'string', ['length' => 1, 'fixed' => true, 'default' => '']);
97
+        $table->addColumn('notification', 'integer', ['unsigned' => true, 'default' => 0]);
98
+        $table->addColumn('enabled', 'string', ['length' => 1, 'fixed' => true, 'default' => '']);
99
+
100
+        // Primary Key
101
+        $table->setPrimaryKey(['id']);
102
+
103
+        // Schema anwenden
104
+        $queries = $schema->toSql($this->connection->getDatabasePlatform());
105
+        foreach ($queries as $query) {
106
+            $this->connection->executeStatement($query);
107
+        }
108
+    }
109
+}