Explorar o código

Fix check default category, and PostgreSQL seq bug (#1907)

checkDefault() was broken as it was not necessarily creating the default
category with the right ID.
PostgreSQL required additional care.
https://github.com/FreshRSS/FreshRSS/issues/1890#issuecomment-392869777
https://github.com/FreshRSS/FreshRSS/pull/1322
https://github.com/FreshRSS/FreshRSS/issues/1312#issuecomment-254009397
Alexandre Alapetite %!s(int64=7) %!d(string=hai) anos
pai
achega
e98e40b7b4
Modificáronse 2 ficheiros con 23 adicións e 5 borrados
  1. 22 4
      app/Models/CategoryDAO.php
  2. 1 1
      app/SQL/install.sql.pgsql.php

+ 22 - 4
app/Models/CategoryDAO.php

@@ -134,7 +134,11 @@ class FreshRSS_CategoryDAO extends Minz_ModelPdo implements FreshRSS_Searchable
 		if (isset($cat[0])) {
 			return $cat[0];
 		} else {
-			return false;
+			if (FreshRSS_Context::$isCli) {
+				fwrite(STDERR, 'FreshRSS database error: Default category not found!' . "\n");
+			}
+			Minz_Log::error('FreshRSS database error: Default category not found!');
+			return null;
 		}
 	}
 	public function checkDefault() {
@@ -144,13 +148,27 @@ class FreshRSS_CategoryDAO extends Minz_ModelPdo implements FreshRSS_Searchable
 			$cat = new FreshRSS_Category(_t('gen.short.default_category'));
 			$cat->_id(self::DEFAULTCATEGORYID);
 
+			$sql = 'INSERT INTO `' . $this->prefix . 'category`(id, name) VALUES(?, ?)';
+			if (parent::$sharedDbType === 'pgsql') {
+				//Force call to nextval()
+				$sql .= " RETURNING nextval('" . $this->prefix . "category_id_seq');";
+			}
+			$stm = $this->bd->prepare($sql);
+
 			$values = array(
-				'id' => $cat->id(),
-				'name' => $cat->name(),
+				$cat->id(),
+				$cat->name(),
 			);
 
-			$this->addCategory($values);
+			if ($stm && $stm->execute($values)) {
+				return $this->bd->lastInsertId('"' . $this->prefix . 'category_id_seq"');
+			} else {
+				$info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+				Minz_Log::error('SQL error check default category: ' . json_encode($info));
+				return false;
+			}
 		}
+		return true;
 	}
 
 	public function count() {

+ 1 - 1
app/SQL/install.sql.pgsql.php

@@ -52,7 +52,7 @@ $SQL_CREATE_TABLES = array(
 'CREATE INDEX %1$sis_read_index ON "%1$sentry" ("is_read");',
 'CREATE INDEX %1$sentry_lastSeen_index ON "%1$sentry" ("lastSeen");',
 
-'INSERT INTO "%1$scategory" (name) SELECT \'%2$s\' WHERE NOT EXISTS (SELECT id FROM "%1$scategory" WHERE id = 1);',
+'INSERT INTO "%1$scategory" (id, name) SELECT 1, \'%2$s\' WHERE NOT EXISTS (SELECT id FROM "%1$scategory" WHERE id = 1) RETURNING nextval(\'%1$scategory_id_seq\');',
 );
 
 global $SQL_CREATE_TABLE_ENTRYTMP;