pgsqlschema.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Database schema for PostgreSQL
  18. *
  19. * @category Database
  20. * @package GNUsocial
  21. * @author Evan Prodromou <evan@status.net>
  22. * @author Brenda Wallace <shiny@cpan.org>
  23. * @author Brion Vibber <brion@status.net>
  24. * @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
  25. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  26. */
  27. defined('GNUSOCIAL') || die();
  28. /**
  29. * Class representing the database schema for PostgreSQL
  30. *
  31. * A class representing the database schema. Can be used to
  32. * manipulate the schema -- especially for plugins and upgrade
  33. * utilities.
  34. *
  35. * @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
  36. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  37. */
  38. class PgsqlSchema extends Schema
  39. {
  40. public static $_single = null;
  41. /**
  42. * Main public entry point. Use this to get
  43. * the singleton object.
  44. *
  45. * @param object|null $conn
  46. * @param string|null dummy param
  47. * @return Schema the (single) Schema object
  48. */
  49. public static function get($conn = null, $_ = 'pgsql')
  50. {
  51. if (empty(self::$_single)) {
  52. self::$_single = new Schema($conn, 'pgsql');
  53. }
  54. return self::$_single;
  55. }
  56. /**
  57. * Returns a table definition array for the table
  58. * in the schema with the given name.
  59. *
  60. * Throws an exception if the table is not found.
  61. *
  62. * @param string $table Name of the table to get
  63. *
  64. * @return array tabledef for that table.
  65. * @throws SchemaTableMissingException
  66. */
  67. public function getTableDef($table)
  68. {
  69. $def = [];
  70. $hasKeys = false;
  71. // Pull column data from INFORMATION_SCHEMA
  72. $columns = $this->fetchMetaInfo($table, 'columns', 'ordinal_position');
  73. if (count($columns) == 0) {
  74. throw new SchemaTableMissingException("No such table: $table");
  75. }
  76. // We'll need to match up fields by ordinal reference
  77. $orderedFields = [];
  78. foreach ($columns as $row) {
  79. $name = $row['column_name'];
  80. $orderedFields[$row['ordinal_position']] = $name;
  81. $field = [];
  82. $field['type'] = $type = $row['udt_name'];
  83. if (in_array($type, ['char', 'bpchar', 'varchar'])) {
  84. if ($row['character_maximum_length'] !== null) {
  85. $field['length'] = intval($row['character_maximum_length']);
  86. }
  87. }
  88. if ($type == 'numeric') {
  89. // Other int types may report these values, but they're irrelevant.
  90. // Just ignore them!
  91. if ($row['numeric_precision'] !== null) {
  92. $field['precision'] = intval($row['numeric_precision']);
  93. }
  94. if ($row['numeric_scale'] !== null) {
  95. $field['scale'] = intval($row['numeric_scale']);
  96. }
  97. }
  98. if ($row['is_nullable'] == 'NO') {
  99. $field['not null'] = true;
  100. }
  101. if ($row['column_default'] !== null) {
  102. $field['default'] = $row['column_default'];
  103. if ($this->isNumericType($type)) {
  104. $field['default'] = intval($field['default']);
  105. }
  106. }
  107. $def['fields'][$name] = $field;
  108. }
  109. // Pulling index info from pg_class & pg_index
  110. // This can give us primary & unique key info, but not foreign key constraints
  111. // so we exclude them and pick them up later.
  112. $indexInfo = $this->fetchIndexInfo($table);
  113. foreach ($indexInfo as $row) {
  114. $keyName = $row['key_name'];
  115. // Dig the column references out!
  116. //
  117. // These are inconvenient arrays with partial references to the
  118. // pg_att table, but since we've already fetched up the column
  119. // info on the current table, we can look those up locally.
  120. $cols = [];
  121. $colPositions = explode(' ', $row['indkey']);
  122. foreach ($colPositions as $ord) {
  123. if ($ord == 0) {
  124. $cols[] = 'FUNCTION'; // @fixme
  125. } else {
  126. $cols[] = $orderedFields[$ord];
  127. }
  128. }
  129. $def['indexes'][$keyName] = $cols;
  130. }
  131. // Pull constraint data from INFORMATION_SCHEMA:
  132. // Primary key, unique keys, foreign keys
  133. $keyColumns = $this->fetchMetaInfo($table, 'key_column_usage', 'constraint_name,ordinal_position');
  134. $keys = [];
  135. foreach ($keyColumns as $row) {
  136. $keyName = $row['constraint_name'];
  137. $keyCol = $row['column_name'];
  138. if (!isset($keys[$keyName])) {
  139. $keys[$keyName] = [];
  140. }
  141. $keys[$keyName][] = $keyCol;
  142. }
  143. foreach ($keys as $keyName => $cols) {
  144. // name hack -- is this reliable?
  145. if ($keyName == "{$table}_pkey") {
  146. $def['primary key'] = $cols;
  147. } elseif (preg_match("/^{$table}_(.*)_fkey$/", $keyName, $matches)) {
  148. $fkey = $this->fetchForeignKeyInfo($table, $keyName);
  149. $colMap = array_combine($cols, $fkey['col_names']);
  150. $def['foreign keys'][$keyName] = [$fkey['table_name'], $colMap];
  151. } else {
  152. $def['unique keys'][$keyName] = $cols;
  153. }
  154. }
  155. return $def;
  156. }
  157. /**
  158. * Pull some INFORMATION.SCHEMA data for the given table.
  159. *
  160. * @param string $table
  161. * @param $infoTable
  162. * @param null $orderBy
  163. * @return array of arrays
  164. * @throws PEAR_Exception
  165. */
  166. public function fetchMetaInfo($table, $infoTable, $orderBy = null)
  167. {
  168. $query = "SELECT * FROM information_schema.%s " .
  169. "WHERE table_name='%s'";
  170. $sql = sprintf($query, $infoTable, $table);
  171. if ($orderBy) {
  172. $sql .= ' ORDER BY ' . $orderBy;
  173. }
  174. return $this->fetchQueryData($sql);
  175. }
  176. /**
  177. * Pull some PG-specific index info
  178. * @param string $table
  179. * @return array of arrays
  180. * @throws PEAR_Exception
  181. */
  182. public function fetchIndexInfo(string $table): array
  183. {
  184. $query = 'SELECT indexname AS key_name, indexdef AS key_def, pg_index.* ' .
  185. 'FROM pg_index INNER JOIN pg_indexes ON pg_index.indexrelid = CAST(pg_indexes.indexname AS regclass) ' .
  186. 'WHERE tablename = \'%s\' AND indisprimary = FALSE AND indisunique = FALSE ' .
  187. 'ORDER BY indrelid, indexrelid';
  188. $sql = sprintf($query, $table);
  189. return $this->fetchQueryData($sql);
  190. }
  191. /**
  192. * @param string $table
  193. * @param string $constraint_name
  194. * @return array array of rows with keys: table_name, col_names (array of strings)
  195. * @throws PEAR_Exception
  196. */
  197. public function fetchForeignKeyInfo(string $table, string $constraint_name): array
  198. {
  199. // In a sane world, it'd be easier to query the column names directly.
  200. // But it's pretty hard to work with arrays such as col_indexes in direct SQL here.
  201. $query = 'SELECT ' .
  202. '(SELECT relname FROM pg_class WHERE oid = confrelid) AS table_name, ' .
  203. 'confrelid AS table_id, ' .
  204. '(SELECT indkey FROM pg_index WHERE indexrelid = conindid) AS col_indices ' .
  205. 'FROM pg_constraint ' .
  206. 'WHERE conrelid = CAST(\'%s\' AS regclass) AND conname = \'%s\' AND contype = \'f\'';
  207. $sql = sprintf($query, $table, $constraint_name);
  208. $data = $this->fetchQueryData($sql);
  209. if (count($data) < 1) {
  210. throw new Exception('Could not find foreign key ' . $constraint_name . ' on table ' . $table);
  211. }
  212. $row = $data[0];
  213. return [
  214. 'table_name' => $row['table_name'],
  215. 'col_names' => $this->getTableColumnNames($row['table_id'], $row['col_indices'])
  216. ];
  217. }
  218. /**
  219. *
  220. * @param int $table_id
  221. * @param array $col_indexes
  222. * @return array of strings
  223. * @throws PEAR_Exception
  224. */
  225. public function getTableColumnNames($table_id, $col_indexes)
  226. {
  227. $indexes = array_map('intval', explode(' ', $col_indexes));
  228. $query = 'SELECT attnum AS col_index, attname AS col_name ' .
  229. 'FROM pg_attribute where attrelid=%d ' .
  230. 'AND attnum IN (%s)';
  231. $sql = sprintf($query, $table_id, implode(',', $indexes));
  232. $data = $this->fetchQueryData($sql);
  233. $byId = [];
  234. foreach ($data as $row) {
  235. $byId[$row['col_index']] = $row['col_name'];
  236. }
  237. $out = [];
  238. foreach ($indexes as $id) {
  239. $out[] = $byId[$id];
  240. }
  241. return $out;
  242. }
  243. /**
  244. * Return the proper SQL for creating or
  245. * altering a column.
  246. *
  247. * Appropriate for use in CREATE TABLE or
  248. * ALTER TABLE statements.
  249. *
  250. * @param string $name column name to create
  251. * @param array $cd column to create
  252. *
  253. * @return string correct SQL for that column
  254. */
  255. public function columnSql(string $name, array $cd)
  256. {
  257. $line = [];
  258. $line[] = parent::columnSql($name, $cd);
  259. /*
  260. if ($table['foreign keys'][$name]) {
  261. foreach ($table['foreign keys'][$name] as $foreignTable => $foreignColumn) {
  262. $line[] = 'references';
  263. $line[] = $this->quoteIdentifier($foreignTable);
  264. $line[] = '(' . $this->quoteIdentifier($foreignColumn) . ')';
  265. }
  266. }
  267. */
  268. // This'll have been added from our transform of 'serial' type
  269. if (!empty($cd['auto_increment'])) {
  270. $line[] = 'GENERATED BY DEFAULT AS IDENTITY';
  271. } elseif (!empty($cd['enum'])) {
  272. foreach ($cd['enum'] as &$val) {
  273. $vals[] = "'" . $val . "'";
  274. }
  275. $line[] = 'CHECK (' . $name . ' IN (' . implode(',', $vals) . '))';
  276. }
  277. return implode(' ', $line);
  278. }
  279. /**
  280. * Append phrase(s) to an array of partial ALTER TABLE chunks in order
  281. * to alter the given column from its old state to a new one.
  282. *
  283. * @param array $phrase
  284. * @param string $columnName
  285. * @param array $old previous column definition as found in DB
  286. * @param array $cd current column definition
  287. */
  288. public function appendAlterModifyColumn(array &$phrase, $columnName, array $old, array $cd)
  289. {
  290. $prefix = 'ALTER COLUMN ' . $this->quoteIdentifier($columnName) . ' ';
  291. $oldType = $this->typeAndSize($columnName, $old);
  292. $newType = $this->typeAndSize($columnName, $cd);
  293. if ($oldType != $newType) {
  294. $phrase[] = $prefix . 'TYPE ' . $newType;
  295. }
  296. if (!empty($old['not null']) && empty($cd['not null'])) {
  297. $phrase[] = $prefix . 'DROP NOT NULL';
  298. } elseif (empty($old['not null']) && !empty($cd['not null'])) {
  299. $phrase[] = $prefix . 'SET NOT NULL';
  300. }
  301. if (isset($old['default']) && !isset($cd['default'])) {
  302. $phrase[] = $prefix . 'DROP DEFAULT';
  303. } elseif (!isset($old['default']) && isset($cd['default'])) {
  304. $phrase[] = $prefix . 'SET DEFAULT ' . $this->quoteDefaultValue($cd);
  305. }
  306. }
  307. public function appendAlterDropPrimary(array &$phrase, string $tableName)
  308. {
  309. // name hack -- is this reliable?
  310. $phrase[] = 'DROP CONSTRAINT ' . $this->quoteIdentifier($tableName . '_pkey');
  311. }
  312. /**
  313. * Append an SQL statement to drop an index from a table.
  314. * Note that in PostgreSQL, index names are DB-unique.
  315. *
  316. * @param array $statements
  317. * @param string $table
  318. * @param string $name
  319. */
  320. public function appendDropIndex(array &$statements, $table, $name)
  321. {
  322. $statements[] = "DROP INDEX $name";
  323. }
  324. public function mapType($column)
  325. {
  326. $map = [
  327. 'integer' => 'int',
  328. 'char' => 'bpchar',
  329. 'datetime' => 'timestamp',
  330. 'blob' => 'bytea',
  331. 'enum' => 'text',
  332. ];
  333. $type = $column['type'];
  334. if (isset($map[$type])) {
  335. $type = $map[$type];
  336. }
  337. $size = $column['size'] ?? null;
  338. if ($type === 'int') {
  339. if (in_array($size, ['tiny', 'small'])) {
  340. $type = 'int2';
  341. } elseif ($size === 'big') {
  342. $type = 'int8';
  343. } else {
  344. $type = 'int4';
  345. }
  346. } elseif ($type === 'float') {
  347. $type = ($size !== 'big') ? 'float4' : 'float8';
  348. }
  349. return $type;
  350. }
  351. /**
  352. * Filter the given table definition array to match features available
  353. * in this database.
  354. *
  355. * This lets us strip out unsupported things like comments, foreign keys,
  356. * or type variants that we wouldn't get back from getTableDef().
  357. *
  358. * @param array $tableDef
  359. * @return array
  360. */
  361. public function filterDef(array $tableDef)
  362. {
  363. foreach ($tableDef['fields'] as $name => &$col) {
  364. // No convenient support for field descriptions
  365. unset($col['description']);
  366. switch ($col['type']) {
  367. case 'serial':
  368. $col['type'] = 'int';
  369. $col['auto_increment'] = true;
  370. break;
  371. case 'datetime':
  372. // Replace archaic MySQL-specific zero-dates with NULL
  373. if (($col['default'] ?? null) === '0000-00-00 00:00:00') {
  374. $col['default'] = null;
  375. $col['not null'] = false;
  376. }
  377. break;
  378. case 'timestamp':
  379. // In MariaDB: If the column does not permit NULL values,
  380. // assigning NULL (or not referencing the column at all
  381. // when inserting) will set the column to CURRENT_TIMESTAMP
  382. // FIXME: ON UPDATE CURRENT_TIMESTAMP
  383. if ($col['not null'] && !isset($col['default'])) {
  384. $col['default'] = 'CURRENT_TIMESTAMP';
  385. }
  386. break;
  387. }
  388. $col['type'] = $this->mapType($col);
  389. unset($col['size']);
  390. }
  391. if (!empty($tableDef['primary key'])) {
  392. $tableDef['primary key'] = $this->filterKeyDef($tableDef['primary key']);
  393. }
  394. if (!empty($tableDef['unique keys'])) {
  395. foreach ($tableDef['unique keys'] as $i => $def) {
  396. $tableDef['unique keys'][$i] = $this->filterKeyDef($def);
  397. }
  398. }
  399. return $tableDef;
  400. }
  401. /**
  402. * Filter the given key/index definition to match features available
  403. * in this database.
  404. *
  405. * @param array $def
  406. * @return array
  407. */
  408. public function filterKeyDef(array $def)
  409. {
  410. // PostgreSQL doesn't like prefix lengths specified on keys...?
  411. foreach ($def as $i => $item) {
  412. if (is_array($item)) {
  413. $def[$i] = $item[0];
  414. }
  415. }
  416. return $def;
  417. }
  418. }