diff --git a/ErrorReport2.php b/ErrorReport2.php index 27b4e21..2211736 100644 --- a/ErrorReport2.php +++ b/ErrorReport2.php @@ -9,10 +9,32 @@ * ErrorReport2 Model * * @package ErrorReport2 - * @version 2.0.0 + * @version 2.0.2 */ final class ErrorReport2 extends SuperStorage -{} +{ + public static function databaseTableVersion() : ?int + { + $storage_engine = self::storageEngine(); + $database = $storage_engine->database(); + $database_name = $storage_engine->database()->getDatabaseConfig()->getDatabase(); + $table_name = $storage_engine->tableName(); + $query = 'SHOW TABLE STATUS FROM ' . $database_name . ' LIKE "' . $table_name . '"'; + + $results = $database->query($query); + if ($results = mysqli_fetch_object($results)) { + $table_comment = $results->Comment; + $comments = preg_split('/^\\\\,/', $table_comment); + foreach ($comments as $comment) { + if (strncmp($comment, 'er2-db-vers:', 12) === 0) { + return (int)substr($comment, 12); + } + } + } + + return null; + } +} ?> \ No newline at end of file diff --git a/ErrorReport2Server.php b/ErrorReport2Server.php index 7cfb2fe..482ae84 100644 --- a/ErrorReport2Server.php +++ b/ErrorReport2Server.php @@ -13,11 +13,13 @@ * ErrorReport2 Server * * @package ErrorReport2 - * @version 2.0.1 + * @version 2.0.2 */ final class ErrorReport2Server extends Controller { - private const ER2_VERSION = '2.0.1'; + private const ER2_VERSION = '2.0.2'; + + public const REQUIRED_DATABASE_VERSION = 2; private const ERROR_RESPONSE_CODE = 400; private const SUCCESS_RESPONSE_CODE = 201; @@ -60,6 +62,15 @@ public function __construct(string $method_name, Request $request) } $this->config = $er2_server_config; + + if (!self::checkDatabaseVersion()) { + throw new \RuntimeException('Unmatching database version'); + } + } + + public static function checkDatabaseVersion() : bool + { + return ErrorReport2::databaseTableVersion() === self::REQUIRED_DATABASE_VERSION; } private function checkHttpMethod(Request $request) : void diff --git a/README.md b/README.md index 432556d..9a6c6bc 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,12 @@ __Example for 6.:__ $config->setER2Config((new \ErrorReport2\ErrorReport2ServerConfig())->addToken('3915753d8765a0')); ``` +## Update +When ER2 is included as git submodule, update the submodule. +Some updates come with database structure changes. +The database updates (sql batch files) can be found in db-directory. +They must be executed in the database manually. + ## Configuration ### Required configuration diff --git a/db/update_201->202.sql b/db/update_201->202.sql new file mode 100644 index 0000000..32bf385 --- /dev/null +++ b/db/update_201->202.sql @@ -0,0 +1,5 @@ +-- Error Report 2 Module +-- Update from ER2 2.0.1 to 2.0.2 + +ALTER TABLE ErrorReport2 CHANGE COLUMN `debug_mode` `debug_mode` tinyint unsigned COMMENT 'BOOL'; +ALTER TABLE ErrorReport2 COMMENT = 'er2-db-vers:2'; diff --git a/er2.sql b/er2.sql index d56cddd..63f5ec0 100644 --- a/er2.sql +++ b/er2.sql @@ -1,5 +1,5 @@ -- Error Report 2 Module --- Version 2.0.1 +-- Version 2.0.2 CREATE TABLE ErrorReport2 ( @@ -17,7 +17,7 @@ CREATE TABLE ErrorReport2 ( `php_version` varchar(20) NOT NULL, `php_mode` varchar(30) NOT NULL, `php_mem_usage` bigint unsigned NOT NULL, - `debug_mode` tinyint unsigned NOT NULL COMMENT 'BOOL', + `debug_mode` tinyint unsigned COMMENT 'BOOL', `request_method` enum('CONNECT', 'DELETE', 'GET', 'HEAD', 'OPTIONS', 'PATCH', 'POST', 'PUT', 'TRACE') NOT NULL, `request_domain` varchar(255) NOT NULL, `request_subdomain` varchar(255) NOT NULL, @@ -33,4 +33,4 @@ CREATE TABLE ErrorReport2 ( `session` text, `errors` text, `throwable` text -) ENGINE = InnoDB; +) ENGINE = InnoDB, COMMENT = 'er2-db-vers:2';