Browse code

Add a migration to populate grape varieties table with a default selection

Benjamin Roth authored on07/08/2023 14:20:02
Showing1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,87 @@
1
+<?php
2
+
3
+declare(strict_types=1);
4
+
5
+/*
6
+ * This file is part of contao-weinanlieferung-bundle.
7
+ *
8
+ * (c) vonRotenberg
9
+ *
10
+ * @license commercial
11
+ */
12
+
13
+namespace vonRotenberg\WeinanlieferungBundle\Migration;
14
+
15
+use Contao\CoreBundle\Migration\AbstractMigration;
16
+use Contao\CoreBundle\Migration\MigrationResult;
17
+use Doctrine\DBAL\Connection;
18
+
19
+class RebsortenMigration extends AbstractMigration
20
+{
21
+    /** @var Connection */
22
+    private $db;
23
+
24
+    public function __construct(Connection $db)
25
+    {
26
+        $this->db = $db;
27
+    }
28
+
29
+
30
+    public function shouldRun(): bool
31
+    {
32
+        $schemaManager = $this->db->createSchemaManager();
33
+
34
+        // If the database table itself does not exist we should do nothing
35
+        if (!$schemaManager->tablesExist(['tl_vr_wa_rebsorte'])) {
36
+            return false;
37
+        }
38
+
39
+        $columns = $schemaManager->listTableColumns('tl_vr_wa_rebsorte');
40
+
41
+        $isPopulated = (bool) $this->db->executeQuery("SELECT count(id) FROM tl_vr_wa_rebsorte")->fetchOne();
42
+
43
+        return !$isPopulated && isset($columns['title']);
44
+    }
45
+
46
+    public function run(): MigrationResult
47
+    {
48
+        $time = time();
49
+
50
+        $arrRebsorten = [
51
+            ['title' => 'Cabernet'],
52
+            ['title' => 'Cabernet Dorsa'],
53
+            ['title' => 'Chardonnay'],
54
+            ['title' => 'Cuveé'],
55
+            ['title' => 'Gewürztraminer'],
56
+            ['title' => 'Grauer Burgunder'],
57
+            ['title' => 'Grüner Silvaner'],
58
+            ['title' => 'Klingelberger (Riesling)'],
59
+            ['title' => 'Merlot'],
60
+            ['title' => 'Müller-Thurgau'],
61
+            ['title' => 'Muskateller'],
62
+            ['title' => 'Räuschling'],
63
+            ['title' => 'Regent'],
64
+            ['title' => 'Riesling'],
65
+            ['title' => 'Rivaner'],
66
+            ['title' => 'Rotwein Cuvée'],
67
+            ['title' => 'Ruländer'],
68
+            ['title' => 'Sauvignon Blanc'],
69
+            ['title' => 'Sauvitage'],
70
+            ['title' => 'Scheurebe'],
71
+            ['title' => 'Silvaner'],
72
+            ['title' => 'Spätburgunder'],
73
+            ['title' => 'Syrah'],
74
+            ['title' => 'Traminer'],
75
+            ['title' => 'Viognier'],
76
+            ['title' => 'Weißer Burgunder'],
77
+            ['title' => 'Weißwein Cuvée'],
78
+        ];
79
+
80
+        for ($i = 0; $i < count($arrRebsorten); $i++)
81
+        {
82
+            $this->db->insert('tl_vr_wa_rebsorte',array_merge($arrRebsorten[$i],['tstamp'=>$time]));
83
+        }
84
+
85
+        return $this->createResult(true,'Inserted ' . $i . ' grape varieties.');
86
+    }
87
+}