+ if(!$this->cfg_db->db_check_table_exists("meta")) {
+ $this->cfg_db->db_exec("
+ CREATE TABLE meta (
+ meta_key varchar(255),
+ meta_value varchar(255)
+ )
+ ");
+
+ /* db_version was added with phpfspot 1.7, before changes
+ on the phpfspot database where not necessary.
+ */
+
+ $this->cfg_db->db_exec("
+ INSERT INTO meta (
+ meta_key, meta_value
+ ) VALUES (
+ 'phpfspot Database Version',
+ '". $this->cfg->db_version ."'
+ )
+ ");
+ }
+
+ /* if version <= 2 and column img_version_idx does not exist yet */
+ if($this->get_db_version() <= 2 &&
+ !$this->cfg_db->db_check_column_exists("images", "img_version_idx")) {
+
+ if(!$this->cfg_db->db_start_transaction())
+ die("Can not start database transaction");
+
+ $result = $this->cfg_db->db_exec("
+ CREATE TEMPORARY TABLE images_temp (
+ img_idx int,
+ img_version_idx int,
+ img_md5 varchar(32),
+ UNIQUE(img_idx, img_version_idx)
+ )
+ ");
+
+ if(!$result) {
+ $this->cfg_db->db_rollback_transaction();
+ die("Upgrade failed - transaction rollback");
+ }
+
+ $result = $this->cfg_db->db_exec("
+ INSERT INTO images_temp
+ SELECT
+ img_idx,
+ 0,
+ img_md5
+ FROM images
+ ");
+
+ if(!$result) {
+ $this->cfg_db->db_rollback_transaction();
+ die("Upgrade failed - transaction rollback");
+ }
+
+ $result = $this->cfg_db->db_exec("
+ DROP TABLE images
+ ");
+
+ if(!$result) {
+ $this->cfg_db->db_rollback_transaction();
+ die("Upgrade failed - transaction rollback");
+ }
+
+ $result = $this->cfg_db->db_exec("
+ CREATE TABLE images (
+ img_idx int,
+ img_version_idx int,
+ img_md5 varchar(32),
+ UNIQUE(img_idx, img_version_idx)
+ )
+ ");
+
+ if(!$result) {
+ $this->cfg_db->db_rollback_transaction();
+ die("Upgrade failed - transaction rollback");
+ }
+
+ $result = $this->cfg_db->db_exec("
+ INSERT INTO images
+ SELECT *
+ FROM images_temp
+ ");
+
+ if(!$result) {
+ $this->cfg_db->db_rollback_transaction();
+ die("Upgrade failed - transaction rollback");
+ }
+
+ $result = $this->cfg_db->db_exec("
+ DROP TABLE images_temp
+ ");
+
+ if(!$result) {
+ $this->cfg_db->db_rollback_transaction();
+ die("Upgrade failed - transaction rollback");
+ }
+
+ if(!$this->cfg_db->db_commit_transaction())
+ die("Can not commit database transaction");
+
+ }
+
+ } // check_phpfspot_db