浏览代码

Add test for DB for a list of actions required to update

Visman 5 年之前
父节点
当前提交
f315502483
共有 3 个文件被更改,包括 118 次插入20 次删除
  1. 82 20
      app/Models/Pages/Admin/Update.php
  2. 18 0
      app/lang/en/admin_update.po
  3. 18 0
      app/lang/ru/admin_update.po

+ 82 - 20
app/Models/Pages/Admin/Update.php

@@ -15,10 +15,12 @@ class Update extends Admin
 {
     const PHP_MIN = '7.3.0';
 
+    const LATEST_REV_WITH_DB_CHANGES = 2;
+
     const LOCK_NAME = 'lock_update';
     const LOCk_TTL  = 1800;
 
-    const CONFIG_FILE = 'config/main.php';
+    const CONFIG_FILE = 'main.php';
 
     /**
      * Конструктор
@@ -126,7 +128,7 @@ class Update extends Admin
 
     protected function loadAndCheckConfig(): bool
     {
-        $this->configFile = \file_get_contents($this->c->DIR_CONFIG . '/main.php');
+        $this->configFile = \file_get_contents($this->c->DIR_CONFIG . '/' . self::CONFIG_FILE);
 
         if (\preg_match('%\[\s+\'BASE_URL\'\s+=>%', $this->configFile, $matches, \PREG_OFFSET_CAPTURE)) {
             $this->configArrPos = $matches[0][1];
@@ -169,31 +171,91 @@ class Update extends Admin
                 ]);
 
                 if ($v->validation($_POST)) {
+                    $e = null;
+
                     // версия PHP
-                    if (\version_compare(\PHP_VERSION, self::PHP_MIN, '<')) {
-                        return $this->c->Message->message(
-                            __('You are running error', 'PHP', \PHP_VERSION, $this->c->FORK_REVISION, self::PHP_MIN),
-                            true,
-                            503
-                        );
+                    if (
+                        null === $e
+                        && \version_compare(\PHP_VERSION, self::PHP_MIN, '<')
+                    ) {
+                        $e = __('You are running error', 'PHP', \PHP_VERSION, $this->c->FORK_REVISION, self::PHP_MIN);
                     }
 
                     // база не от ForkBB ????
-                    if ($this->c->config->i_fork_revision < 1) {
-                        return $this->c->Message->message(
-                            'Version mismatch error',
-                            true,
-                            503
-                        );
+                    if (
+                        null === $e
+                        && $this->c->config->i_fork_revision < 1
+                    ) {
+                        $e = 'Version mismatch error';
                     }
 
                     // загрузка и проверка конфига
-                    if (true !== $this->loadAndCheckConfig()) {
-                        return $this->c->Message->message(
-                            'The structure of the main.php file is undefined',
-                            true,
-                            503
-                        );
+                    if (
+                        null === $e
+                        && true !== $this->loadAndCheckConfig()
+                    ) {
+                        $e = 'The structure of the main.php file is undefined';
+                    }
+
+                    // проверка доступности базы данных на изменения
+                    if (
+                        null === $e
+                        && $this->c->config->i_fork_revision < self::LATEST_REV_WITH_DB_CHANGES
+                    ) {
+                        $test_table = 'test_tb_for_update';
+
+                        if (
+                            null === $e
+                            && true === $this->c->DB->tableExists($test_table)
+                        ) {
+                            $e = __('The %s table already exists. Delete it.', $test_table);
+                        }
+
+                        $schema = [
+                            'FIELDS' => [
+                                'id' => ['SERIAL', false],
+                            ],
+                            'PRIMARY KEY' => ['id'],
+                        ];
+                        if (
+                            null === $e
+                            && false === $this->c->DB->createTable($test_table, $schema)
+                        ) {
+                            $e = __('Unable to create %s table', $test_table);
+                        }
+
+                        if (
+                            null === $e
+                            && false === $this->c->DB->addField($test_table, 'test_field', 'VARCHAR(80)', false, '')
+                        ) {
+                            $e = __('Unable to add test_field field to %s table', $test_table);
+                        }
+
+                        $sql = "INSERT INTO ::{$test_table} (test_field) VALUES ('TEST_VALUE')";
+                        if (
+                            null === $e
+                            && false === $this->c->DB->exec($sql)
+                        ) {
+                            $e = __('Unable to insert line to %s table', $test_table);
+                        }
+
+                        if (
+                            null === $e
+                            && false === $this->c->DB->dropField($test_table, 'test_field')
+                        ) {
+                            $e = __('Unable to drop test_field field from %s table', $test_table);
+                        }
+
+                        if (
+                            null === $e
+                            && false === $this->c->DB->dropTable($test_table)
+                        ) {
+                            $e = __('Unable to drop %s table', $test_table);
+                        }
+                    }
+
+                    if (\is_string($e)) {
+                        return $this->c->Message->message($e, true, 503);
                     }
 
                     $uid = $this->setLock();

+ 18 - 0
app/lang/en/admin_update.po

@@ -206,3 +206,21 @@ msgstr "Stage %1$s (%2$s)"
 
 msgid "The structure of the main.php file is undefined"
 msgstr "The structure of the main.php file is undefined."
+
+msgid "The %s table already exists. Delete it."
+msgstr "The %s table already exists. Delete it."
+
+msgid "Unable to create %s table"
+msgstr "Unable to create %s table."
+
+msgid "Unable to add test_field field to %s table"
+msgstr "Unable to add test_field field to %s table."
+
+msgid "Unable to insert line to %s table"
+msgstr "Unable to insert line to %s table."
+
+msgid "Unable to drop test_field field from %s table"
+msgstr "Unable to drop test_field field from %s table."
+
+msgid "Unable to drop %s table"
+msgstr "Unable to drop %s table."

+ 18 - 0
app/lang/ru/admin_update.po

@@ -206,3 +206,21 @@ msgstr "Шаг %1$s (%2$s)"
 
 msgid "The structure of the main.php file is undefined"
 msgstr "Структура файла main.php не определена."
+
+msgid "The %s table already exists. Delete it."
+msgstr "Таблица %s уже существует. Удалите её."
+
+msgid "Unable to create %s table"
+msgstr "Невозможно создать таблицу %s."
+
+msgid "Unable to add test_field field to %s table"
+msgstr "Невозможно добавить поле test_field в таблицу %s."
+
+msgid "Unable to insert line to %s table"
+msgstr "Невозможно вставить строку в таблицу %s."
+
+msgid "Unable to drop test_field field from %s table"
+msgstr "Невозможно удалить поле test_field из таблицы %s."
+
+msgid "Unable to drop %s table"
+msgstr "Невозможно удалить таблицу %s."