Skip to content

Commit c3c3a0e

Browse files
committed
Add a migration for the cookiebar opener (fixes #267)
1 parent ddc145d commit c3c3a0e

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

config/migrations.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,9 @@ services:
44
- '@database_connection'
55
tags:
66
- { name: contao.migration, priority: 0 }
7+
8+
Oveleon\ContaoCookiebar\Migration\ContaoCookiebarOpenerMigration:
9+
arguments:
10+
- '@database_connection'
11+
tags:
12+
- { name: contao.migration, priority: 0 }
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Oveleon\ContaoCookiebar\Migration;
6+
7+
use Contao\CoreBundle\Migration\AbstractMigration;
8+
use Contao\CoreBundle\Migration\MigrationResult;
9+
use Doctrine\DBAL\Connection;
10+
use Doctrine\DBAL\Exception;
11+
12+
class ContaoCookiebarOpenerMigration extends AbstractMigration
13+
{
14+
/**
15+
* @var Connection
16+
*/
17+
private $connection;
18+
19+
public function __construct(Connection $connection)
20+
{
21+
$this->connection = $connection;
22+
}
23+
24+
public function getName(): string
25+
{
26+
return 'Contao Cookiebar: Cookiebar opener migration';
27+
}
28+
29+
/**
30+
* @throws Exception
31+
*/
32+
public function shouldRun(): bool
33+
{
34+
$schemaManager = $this->connection->createSchemaManager();
35+
36+
if (!$schemaManager->tablesExist(['tl_content', 'tl_module']))
37+
{
38+
return false;
39+
}
40+
41+
$columns = $schemaManager->listTableColumns('tl_content');
42+
43+
$total = $this->connection->fetchOne('SELECT COUNT(*) FROM tl_content WHERE type=?', ['cookiebarOpener']);
44+
$total += $this->connection->fetchOne('SELECT COUNT(*) FROM tl_module WHERE type=?', ['cookiebarOpener']);
45+
46+
return $total > 0;
47+
}
48+
49+
public function run(): MigrationResult
50+
{
51+
$elements = $this->connection->fetchAllAssociative('SELECT id FROM tl_content WHERE type=?', ['cookiebarOpener']);
52+
$modules = $this->connection->fetchAllAssociative('SELECT id FROM tl_module WHERE type=?', ['cookiebarOpener']);
53+
54+
foreach ($elements as $element)
55+
{
56+
$this->connection->update('tl_content', ['type' => 'cookiebar_opener'], ['id' => $element['id']]);
57+
}
58+
59+
foreach ($modules as $module)
60+
{
61+
$this->connection->update('tl_module', ['type' => 'cookiebar_opener'], ['id' => $module['id']]);
62+
}
63+
64+
return $this->createResult(true);
65+
}
66+
}

0 commit comments

Comments
 (0)