Pārlūkot izejas kodu

add the post-upgrade script

Olivier Massot 3 gadi atpakaļ
vecāks
revīzija
b7fdf0e331
2 mainītis faili ar 118 papildinājumiem un 0 dzēšanām
  1. 1 0
      .env
  2. 117 0
      src/Commands/PostUpgrade/V0_2/PostUpgradeCommand.php

+ 1 - 0
.env

@@ -12,6 +12,7 @@
 #
 # Run "composer dump-env prod" to compile .env files for production use (requires symfony/flex >=1.2).
 # https://symfony.com/doc/current/best_practices.html#use-environment-variables-for-infrastructure-configuration
+VERSION=0.2
 
 ###> symfony/framework-bundle ###
 APP_ENV=prod

+ 117 - 0
src/Commands/PostUpgrade/V0_2/PostUpgradeCommand.php

@@ -0,0 +1,117 @@
+<?php
+
+namespace App\Commands\PostUpgrade\V0_2;
+
+use PDO;
+use Psr\Log\LoggerInterface;
+use Symfony\Component\Console\Attribute\AsCommand;
+use Symfony\Component\Console\Command\Command;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+
+#[AsCommand(
+    name: 'opentalent:upgrade:0.2',
+    description: 'Execute the post-upgrade scripts for Ap2i v0.2'
+)]
+class PostUpgradeCommand extends Command
+{
+    public const TARGETED_VERSION = "0.2";
+
+    public function __construct(private LoggerInterface $logger) {
+        parent::__construct();
+    }
+
+    protected function configure(): void
+    {}
+
+    protected function execute(InputInterface $input, OutputInterface $output): int
+    {
+        $version = $_ENV['VERSION'];
+        if ($version !== self::TARGETED_VERSION) {
+            throw new \RuntimeException(
+                'Targeted version (' . self::TARGETED_VERSION . ') is different from ' .
+                'current version (' . $version . ')'
+            );
+        }
+        $this->logger->info('Run post-upgrade scripts for version ' . $version);
+
+        $this->populateSubdomains();
+
+        $output->writeln("Post-upgrade operations successfully executed");
+        return Command::SUCCESS;
+    }
+
+    /**
+     * Populate the new Subdomain table
+     * @throws \Exception
+     */
+    public function populateSubdomains() {
+
+        $dbUrl = $_ENV['DATABASE_URL'];
+        $matches = [];
+        $match = preg_match(
+            "/^mysql:\/\/(\w+):([^\s@]+)@(\w+):(\d+)\/(\w+)/",
+            $dbUrl,
+            $matches
+        );
+        $dbUser = $matches[1];
+        $dbPwd = $matches[2];
+        $dbHost = $matches[3];
+        $dbPort = $matches[4];
+        $dbName = $matches[5];
+
+        $cnn = new PDO(
+            "mysql:host=" . $dbHost . ";dbname=" . $dbName,
+            $dbUser,
+            $dbPwd,
+            array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
+        $cnn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+
+        $cnn->beginTransaction();
+
+        try {
+            $stmt = $cnn->query("select count(*) from opentalent.Subdomain;");
+            if ($stmt->fetchColumn(0)[0] > 0) {
+                throw new \RuntimeException('Subdomain table is not empty');
+            }
+
+            $this->logger->info('Populate Subdomain table from openassos.sys_domain');
+            $sql = "INSERT INTO opentalent.Subdomain (parameters_id, subdomain)
+                    SELECT o.parameters_id, SUBSTRING_INDEX(d.domainName,'.',1)
+                    FROM openassos.sys_domain d
+                    inner join opentalent.Organization o on o.cmsId = d.pid
+                    where d.domainName like '%.opentalent.fr';";
+            $cnn->query($sql);
+
+            $this->logger->info('Complete with subdomains from Parameters table');
+            $sql = "insert into opentalent.Subdomain (parameters_id, subdomain)
+                    select p.id, p.subDomain
+                    from opentalent.Parameters p
+                    left join opentalent.Subdomain s 
+                    on s.parameters_id = p.id
+                    where s.id is null;";
+            $cnn->query($sql);
+
+            $sql = "insert into opentalent.Subdomain (parameters_id, subdomain)
+                    select p.id, p.subDomain
+                    from opentalent.Parameters p
+                    inner join opentalent.Subdomain s 
+                    on s.parameters_id = p.id
+                    where p.subDomain not in (select subdomain from opentalent.Subdomain where parameters_id=p.id);";
+            $cnn->query($sql);
+
+            $this->logger->info('Set the current subdomains');
+            $sql = "update opentalent.Parameters p
+                    inner join opentalent.Subdomain s on s.parameters_id = p.id and s.subdomain = p.subDomain 
+                    set p.activeSubdomain_id = s.id;";
+            $cnn->query($sql);
+
+            $cnn->commit();
+            $this->logger->info('Subdomain table was successfully populated');
+        } catch (\Exception $e) {
+            $cnn->rollBack();
+            $this->logger->critical('Error while running the post-upgrade script, abort and rollback');
+            throw $e;
+        }
+    }
+}