Changeset d2faced for src/webservice


Ignore:
Timestamp:
Sep 23, 2015, 7:43:40 PM (11 years ago)
Author:
Joachim Langenbach <joachim.langenbach@…>
Branches:
master
Children:
363c1af
Parents:
f8c242e
Message:
  • Introduced database schema version
    • Added creationDate field to the stations table
Location:
src/webservice
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • src/webservice/config/internalconfig.php.in

    rf8c242e rd2faced  
    2525        "version" => "@VERSION@",
    2626        "webserviceApiVersion" => "@WEBSERVICE_API_VERSION@",
     27        "databaseVersionCore" => "@WEBSERVICE_DATABASE_SCHEMA_VERSION_CORE@"
    2728];
    2829
  • src/webservice/htdocs/SiWeather/Webservice/DatabaseHandler.php

    rf8c242e rd2faced  
    7272        }
    7373
    74         public function connect()
     74        /**
     75         * @brief Connects to the database and does some checks.
     76         *
     77         * @note This function should only called by the setup with @p ignoreVersion = true!
     78         *       Otherwise the database version is not checked! Data loss may be the consequence!
     79         */
     80        public function connect($ignoreVersion = false)
    7581        {
    7682                if(!is_null($this->_database)){
    … …  
    99105                }
    100106                $this->_database = $db;
     107
     108                // check schema version
     109                if(!$this->checkVersions() && !$ignoreVersion)
     110                        return false;
     111               
    101112                return true;
    102113        }
    … …  
    113124                $this->setLastErrorMessage("Failed to close the database: "+ $this->_database->lastErrorMsg());
    114125                return false;
     126        }
     127       
     128        public function databaseTableExists($table){
     129                if(!$this->_database || !is_object($this->_database)){
     130                        return false;
     131                }
     132                $statement = $this->prepare("SELECT COUNT(*) FROM sqlite_master WHERE type=\"table\" AND name=:name");
     133                if(!$statement){
     134                        $this->setLastErrorMessage(\Translator::swGettext("Could not prepare query to check if table %s exists", $table));
     135                        return false;
     136                }
     137                $statement->bindValue(":name", $table);
     138                $result = $statement->execute();
     139                $row = $result->fetchArray();
     140                if(!$row || !is_array($row) || count($row) < 1 || $row[0] != 1)
     141                        return false;
     142                return true;
     143        }
     144       
     145        public function checkVersions(){
     146                if(is_null($this->_database)){
     147                        $this->setLastErrorMessage("No database available to check versions");
     148                        return false;
     149                }
     150                $table = "schema";
     151                if(!$this->databaseTableExists($table)){
     152                        $this->setLastErrorMessage("Database table with schema versions does not exists!");
     153                        return false;
     154                }
     155
     156                // check schema version
     157                $neededVersion = $this->_config->getConfig("databaseVersionCore");
     158                $currentVersion = $this->exec("SELECT version FROM ". $table ." WHERE name='core'");
     159                if(empty($neededVersion) || !is_numeric($neededVersion) || !$currentVersion || $currentVersion != $neededVersion){
     160                        $this->setLastErrorMessage("Found wrong database core version. Expected ". $neededVersion ." got ". $currentVersion);
     161                        return false;
     162                }
     163                return true;
    115164        }
    116165       
  • src/webservice/htdocs/SiWeather/Webservice/Setup.php

    rf8c242e rd2faced  
    182182                        return false;
    183183                }
    184                 if(!$this->_databaseHandler->connect()){
     184                if(!$this->_databaseHandler->connect(true)){
    185185                        $errorMessage = \Translator::swGettext("Could not connect to the database (%s)", $this->_databaseHandler->lastErrorMessage());
    186186                        return false;
     187                }
     188                // check version, if an schema already exists
     189                if($this->_databaseHandler->databaseTableExists("schema")){
     190                        if(!$this->_databaseHandler->checkVersions()){
     191                                $errorMessage = \Translator::swGettext("Database schema versions check failed (%s)", $this->_databaseHandler->lastErrorMessage());
     192                                return false;
     193                        }
    187194                }
    188195               
    189196                // prepare Spatalite
    190                 if(!$this->databaseTableExists("spatial_ref_sys")){
     197                if(!$this->_databaseHandler->databaseTableExists("spatial_ref_sys")){
    191198                        if(!$this->_databaseHandler->exec("BEGIN")){
    192199                                $errorMessage = _("Could not start transaction to init Spatialite");
    … …  
    202209                        }
    203210                }
    204                
     211
    205212//              // prepare countries table
    206213//              $table = "countries";
    … …  
    231238//              }
    232239
     240                // prepare schema table
     241                $table = "schema";
     242                if(!$this->_databaseHandler->databaseTableExists($table)){
     243                        $sql  = "CREATE TABLE ". $table ." (";
     244                        $sql .= "  id INTEGER NOT NULL PRIMARY KEY,";
     245                        $sql .= "  name TEXT NOT NULL,";
     246                        $sql .= "  version INTEGER NOT NULL";
     247                        $sql .= ")";
     248                        if(!$this->databaseTableCreate($table, $sql, $errorMessage)){
     249                                return false;
     250                        }
     251                        $sql  = "INSERT INTO ". $table ." (name, version) VALUES(:name, :version)";
     252                        $sqlStatement = $this->_databaseHandler->prepare($sql);
     253                        $sqlStatement->clear();
     254                        $sqlStatement->bindValue(":name", "core");
     255                        $sqlStatement->bindValue(":version", $this->_webserviceConfig->getConfig("databaseVersionCore"));
     256                        if(!$sqlStatement->execute()){
     257                                $errorMessage = Translation::swGettext("Could not add schema version %s to the database", "core");
     258                                error_log($errorMessage);
     259                                return false;
     260                        }
     261                        $sqlStatement->reset();
     262                }
     263
    233264                // prepare cities table
    234265                $table = "cities";
    235                 if(!$this->databaseTableExists($table)){
     266                if(!$this->_databaseHandler->databaseTableExists($table)){
    236267                        $sql  = "CREATE TABLE ". $table ." (";
    237268                        $sql .= "  id INTEGER NOT NULL PRIMARY KEY,";
    … …  
    253284                // prepare status table
    254285                $table = "status";
    255                 if(!$this->databaseTableExists($table)){
     286                if(!$this->_databaseHandler->databaseTableExists($table)){
    256287                        $sql  = "CREATE TABLE ". $table ." (";
    257288                        $sql .= "  id INTEGER NOT NULL PRIMARY KEY,";
    … …  
    278309                // prepare stations table
    279310                $table = "stations";
    280                 if(!$this->databaseTableExists($table)){
     311                if(!$this->_databaseHandler->databaseTableExists($table)){
    281312                        $sql  = "CREATE TABLE ". $table ." (";
    282313                        $sql .= "  id INTEGER NOT NULL PRIMARY KEY,";
    … …  
    287318                        $sql .= "  type TEXT NOT NULL,";
    288319                        $sql .= "  typeUrl TEXT,";
    289                         $sql .= "  status INTEGER NOT NULL,";
     320                        $sql .= "  creationDate TIMESTAMP DEFAULT (strftime('%s', 'now')),";
     321//                      $sql .= "  creationDate TIMESTAMP DEFAULT CURRENT_TIMESTAMP,";
     322                        $sql .= "  status INTEGER DEFAULT 1,";
    290323                        $sql .= "  FOREIGN KEY(city) REFERENCES cities(id)";
    291324                        $sql .= "  FOREIGN KEY(status) REFERENCES status(id)";
    … …  
    399432        }
    400433       
    401         private function databaseTableExists($table){
    402                 if(!$this->_databaseHandler || !is_object($this->_databaseHandler)){
    403                         return false;
    404                 }
    405                 $statement = $this->_databaseHandler->prepare("SELECT COUNT(*) FROM sqlite_master WHERE type=\"table\" AND name=:name");
    406                 if(!$statement){
    407                         $errorMessage = \Translator::swGettext("Could not prepare query to check if table %s exists", $table);
    408                         return false;
    409                 }
    410                 $statement->bindValue(":name", $table);
    411                 $result = $statement->execute();
    412                 $row = $result->fetchArray();
    413                 if(!$row || !is_array($row) || count($row) < 1 || $row[0] != 1)
    414                         return false;
    415                 return true;
    416         }
    417        
    418434        private function databaseTableCreate($table, $sqlStatement, &$errorMessage){
    419435                if(!$this->_databaseHandler || !is_object($this->_databaseHandler)){
Note: See TracChangeset for help on using the changeset viewer.