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
File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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       
Note: See TracChangeset for help on using the changeset viewer.