pgsqlschema.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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($field)) {
  104. $field['default'] = (int) $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. private function isNumericType(array $cd): bool
  244. {
  245. $ints = ['int', 'numeric', 'serial'];
  246. return in_array(strtolower($cd['type']), $ints);
  247. }
  248. /**
  249. * Return the proper SQL for creating or
  250. * altering a column.
  251. *
  252. * Appropriate for use in CREATE TABLE or
  253. * ALTER TABLE statements.
  254. *
  255. * @param string $name column name to create
  256. * @param array $cd column to create
  257. *
  258. * @return string correct SQL for that column
  259. */
  260. public function columnSql(string $name, array $cd)
  261. {
  262. $line = [];
  263. $line[] = parent::columnSql($name, $cd);
  264. /*
  265. if ($table['foreign keys'][$name]) {
  266. foreach ($table['foreign keys'][$name] as $foreignTable => $foreignColumn) {
  267. $line[] = 'references';
  268. $line[] = $this->quoteIdentifier($foreignTable);
  269. $line[] = '(' . $this->quoteIdentifier($foreignColumn) . ')';
  270. }
  271. }
  272. */
  273. // This'll have been added from our transform of 'serial' type
  274. if (!empty($cd['auto_increment'])) {
  275. $line[] = 'GENERATED BY DEFAULT AS IDENTITY';
  276. } elseif (!empty($cd['enum'])) {
  277. foreach ($cd['enum'] as &$val) {
  278. $vals[] = "'" . $val . "'";
  279. }
  280. $line[] = 'CHECK (' . $name . ' IN (' . implode(',', $vals) . '))';
  281. }
  282. return implode(' ', $line);
  283. }
  284. /**
  285. * Append phrase(s) to an array of partial ALTER TABLE chunks in order
  286. * to alter the given column from its old state to a new one.
  287. *
  288. * @param array $phrase
  289. * @param string $columnName
  290. * @param array $old previous column definition as found in DB
  291. * @param array $cd current column definition
  292. */
  293. public function appendAlterModifyColumn(array &$phrase, $columnName, array $old, array $cd)
  294. {
  295. $prefix = 'ALTER COLUMN ' . $this->quoteIdentifier($columnName) . ' ';
  296. $oldType = $this->typeAndSize($columnName, $old);
  297. $newType = $this->typeAndSize($columnName, $cd);
  298. if ($oldType != $newType) {
  299. $phrase[] = $prefix . 'TYPE ' . $newType;
  300. }
  301. if (!empty($old['not null']) && empty($cd['not null'])) {
  302. $phrase[] = $prefix . 'DROP NOT NULL';
  303. } elseif (empty($old['not null']) && !empty($cd['not null'])) {
  304. $phrase[] = $prefix . 'SET NOT NULL';
  305. }
  306. if (isset($old['default']) && !isset($cd['default'])) {
  307. $phrase[] = $prefix . 'DROP DEFAULT';
  308. } elseif (!isset($old['default']) && isset($cd['default'])) {
  309. $phrase[] = $prefix . 'SET DEFAULT ' . $this->quoteDefaultValue($cd);
  310. }
  311. }
  312. public function appendAlterDropPrimary(array &$phrase, string $tableName)
  313. {
  314. // name hack -- is this reliable?
  315. $phrase[] = 'DROP CONSTRAINT ' . $this->quoteIdentifier($tableName . '_pkey');
  316. }
  317. /**
  318. * Append an SQL statement to drop an index from a table.
  319. * Note that in PostgreSQL, index names are DB-unique.
  320. *
  321. * @param array $statements
  322. * @param string $table
  323. * @param string $name
  324. */
  325. public function appendDropIndex(array &$statements, $table, $name)
  326. {
  327. $statements[] = "DROP INDEX $name";
  328. }
  329. public function mapType($column)
  330. {
  331. $map = [
  332. 'integer' => 'int',
  333. 'char' => 'bpchar',
  334. 'datetime' => 'timestamp',
  335. 'blob' => 'bytea',
  336. 'enum' => 'text',
  337. ];
  338. $type = $column['type'];
  339. if (isset($map[$type])) {
  340. $type = $map[$type];
  341. }
  342. $size = $column['size'] ?? null;
  343. if ($type === 'int') {
  344. if (in_array($size, ['tiny', 'small'])) {
  345. $type = 'int2';
  346. } elseif ($size === 'big') {
  347. $type = 'int8';
  348. } else {
  349. $type = 'int4';
  350. }
  351. } elseif ($type === 'float') {
  352. $type = ($size !== 'big') ? 'float4' : 'float8';
  353. }
  354. return $type;
  355. }
  356. /**
  357. * Filter the given table definition array to match features available
  358. * in this database.
  359. *
  360. * This lets us strip out unsupported things like comments, foreign keys,
  361. * or type variants that we wouldn't get back from getTableDef().
  362. *
  363. * @param string $tableName
  364. * @param array $tableDef
  365. * @return array
  366. */
  367. public function filterDef(string $tableName, array $tableDef)
  368. {
  369. $tableDef = parent::filterDef($tableName, $tableDef);
  370. foreach ($tableDef['fields'] as $name => &$col) {
  371. // No convenient support for field descriptions
  372. unset($col['description']);
  373. if ($col['type'] === 'serial') {
  374. $col['type'] = 'int';
  375. $col['auto_increment'] = true;
  376. }
  377. $col['type'] = $this->mapType($col);
  378. unset($col['size']);
  379. }
  380. if (!empty($tableDef['primary key'])) {
  381. $tableDef['primary key'] = $this->filterKeyDef($tableDef['primary key']);
  382. }
  383. if (!empty($tableDef['unique keys'])) {
  384. foreach ($tableDef['unique keys'] as $i => $def) {
  385. $tableDef['unique keys'][$i] = $this->filterKeyDef($def);
  386. }
  387. }
  388. return $tableDef;
  389. }
  390. /**
  391. * Filter the given key/index definition to match features available
  392. * in this database.
  393. *
  394. * @param array $def
  395. * @return array
  396. */
  397. public function filterKeyDef(array $def)
  398. {
  399. // PostgreSQL doesn't like prefix lengths specified on keys...?
  400. foreach ($def as $i => $item) {
  401. if (is_array($item)) {
  402. $def[$i] = $item[0];
  403. }
  404. }
  405. return $def;
  406. }
  407. }