mysqlschema.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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 MariaDB
  18. *
  19. * @category Database
  20. * @package GNUsocial
  21. * @author Evan Prodromou <evan@status.net>
  22. * @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
  23. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  24. */
  25. defined('GNUSOCIAL') || die();
  26. /**
  27. * Class representing the database schema for MariaDB
  28. *
  29. * A class representing the database schema. Can be used to
  30. * manipulate the schema -- especially for plugins and upgrade
  31. * utilities.
  32. *
  33. * @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
  34. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  35. */
  36. class MysqlSchema extends Schema
  37. {
  38. static $_single = null;
  39. protected $conn = null;
  40. /**
  41. * Main public entry point. Use this to get
  42. * the singleton object.
  43. *
  44. * @param null $conn
  45. * @return Schema the (single) Schema object
  46. */
  47. static function get($conn = null)
  48. {
  49. if (empty(self::$_single)) {
  50. self::$_single = new Schema($conn);
  51. }
  52. return self::$_single;
  53. }
  54. /**
  55. * Returns a TableDef object for the table
  56. * in the schema with the given name.
  57. *
  58. * Throws an exception if the table is not found.
  59. *
  60. * @param string $table Name of the table to get
  61. *
  62. * @return array of tabledef for that table.
  63. * @throws PEAR_Exception
  64. * @throws SchemaTableMissingException
  65. */
  66. public function getTableDef($table)
  67. {
  68. $def = [];
  69. $hasKeys = false;
  70. // Pull column data from INFORMATION_SCHEMA
  71. $columns = $this->fetchMetaInfo($table, 'COLUMNS', 'ORDINAL_POSITION');
  72. if (count($columns) == 0) {
  73. throw new SchemaTableMissingException("No such table: $table");
  74. }
  75. foreach ($columns as $row) {
  76. $name = $row['COLUMN_NAME'];
  77. $field = [];
  78. // warning -- 'unsigned' attr on numbers isn't given in DATA_TYPE and friends.
  79. // It is stuck in on COLUMN_TYPE though (eg 'bigint(20) unsigned')
  80. $field['type'] = $type = $row['DATA_TYPE'];
  81. if ($type == 'char' || $type == 'varchar') {
  82. if ($row['CHARACTER_MAXIMUM_LENGTH'] !== null) {
  83. $field['length'] = intval($row['CHARACTER_MAXIMUM_LENGTH']);
  84. }
  85. }
  86. if ($type == 'decimal') {
  87. // Other int types may report these values, but they're irrelevant.
  88. // Just ignore them!
  89. if ($row['NUMERIC_PRECISION'] !== null) {
  90. $field['precision'] = intval($row['NUMERIC_PRECISION']);
  91. }
  92. if ($row['NUMERIC_SCALE'] !== null) {
  93. $field['scale'] = intval($row['NUMERIC_SCALE']);
  94. }
  95. }
  96. if ($row['IS_NULLABLE'] == 'NO') {
  97. $field['not null'] = true;
  98. }
  99. if ($row['COLUMN_DEFAULT'] !== null) {
  100. // Hack for timestamp cols
  101. if ($type == 'timestamp' && $row['COLUMN_DEFAULT'] == 'CURRENT_TIMESTAMP') {
  102. // skip because timestamp is numerical, but it accepts datetime strings as well
  103. } else {
  104. $field['default'] = $row['COLUMN_DEFAULT'];
  105. if ($this->isNumericType($type)) {
  106. $field['default'] = intval($field['default']);
  107. }
  108. }
  109. }
  110. if ($row['COLUMN_KEY'] !== null) {
  111. // We'll need to look up key info...
  112. $hasKeys = true;
  113. }
  114. if ($row['COLUMN_COMMENT'] !== null && $row['COLUMN_COMMENT'] != '') {
  115. $field['description'] = $row['COLUMN_COMMENT'];
  116. }
  117. $extra = $row['EXTRA'];
  118. if ($extra) {
  119. if (preg_match('/(^|\s)auto_increment(\s|$)/i', $extra)) {
  120. $field['auto_increment'] = true;
  121. }
  122. // $row['EXTRA'] may contain 'on update CURRENT_TIMESTAMP'
  123. // ^ ...... how to specify?
  124. }
  125. /* @fixme check against defaults?
  126. if ($row['CHARACTER_SET_NAME'] !== null) {
  127. $def['charset'] = $row['CHARACTER_SET_NAME'];
  128. $def['collate'] = $row['COLLATION_NAME'];
  129. }*/
  130. $def['fields'][$name] = $field;
  131. }
  132. if ($hasKeys) {
  133. // INFORMATION_SCHEMA's CONSTRAINTS and KEY_COLUMN_USAGE tables give
  134. // good info on primary and unique keys but don't list ANY info on
  135. // multi-value keys, which is lame-o. Sigh.
  136. //
  137. // Let's go old school and use SHOW INDEX :D
  138. //
  139. $keyInfo = $this->fetchIndexInfo($table);
  140. $keys = [];
  141. $keyTypes = [];
  142. foreach ($keyInfo as $row) {
  143. $name = $row['Key_name'];
  144. $column = $row['Column_name'];
  145. if (!isset($keys[$name])) {
  146. $keys[$name] = [];
  147. }
  148. $keys[$name][] = $column;
  149. if ($name == 'PRIMARY') {
  150. $type = 'primary key';
  151. } else if ($row['Non_unique'] == 0) {
  152. $type = 'unique keys';
  153. } else if ($row['Index_type'] == 'FULLTEXT') {
  154. $type = 'fulltext indexes';
  155. } else {
  156. $type = 'indexes';
  157. }
  158. $keyTypes[$name] = $type;
  159. }
  160. foreach ($keyTypes as $name => $type) {
  161. if ($type == 'primary key') {
  162. // there can be only one
  163. $def[$type] = $keys[$name];
  164. } else {
  165. $def[$type][$name] = $keys[$name];
  166. }
  167. }
  168. }
  169. return $def;
  170. }
  171. /**
  172. * Pull the given table properties from INFORMATION_SCHEMA.
  173. * Most of the good stuff is MySQL extensions.
  174. *
  175. * @param $table
  176. * @param $props
  177. * @return array
  178. * @throws PEAR_Exception
  179. * @throws SchemaTableMissingException
  180. */
  181. function getTableProperties($table, $props)
  182. {
  183. $data = $this->fetchMetaInfo($table, 'TABLES');
  184. if ($data) {
  185. return $data[0];
  186. } else {
  187. throw new SchemaTableMissingException("No such table: $table");
  188. }
  189. }
  190. /**
  191. * Pull some INFORMATION.SCHEMA data for the given table.
  192. *
  193. * @param string $table
  194. * @param $infoTable
  195. * @param null $orderBy
  196. * @return array of arrays
  197. * @throws PEAR_Exception
  198. */
  199. function fetchMetaInfo($table, $infoTable, $orderBy = null)
  200. {
  201. $query = "SELECT * FROM INFORMATION_SCHEMA.%s " .
  202. "WHERE TABLE_SCHEMA='%s' AND TABLE_NAME='%s'";
  203. $schema = $this->conn->dsn['database'];
  204. $sql = sprintf($query, $infoTable, $schema, $table);
  205. if ($orderBy) {
  206. $sql .= ' ORDER BY ' . $orderBy;
  207. }
  208. return $this->fetchQueryData($sql);
  209. }
  210. /**
  211. * Pull 'SHOW INDEX' data for the given table.
  212. *
  213. * @param string $table
  214. * @return array of arrays
  215. * @throws PEAR_Exception
  216. */
  217. function fetchIndexInfo($table)
  218. {
  219. $query = "SHOW INDEX FROM `%s`";
  220. $sql = sprintf($query, $table);
  221. return $this->fetchQueryData($sql);
  222. }
  223. /**
  224. * Append an SQL statement with an index definition for a full-text search
  225. * index over one or more columns on a table.
  226. *
  227. * @param array $statements
  228. * @param string $table
  229. * @param string $name
  230. * @param array $def
  231. */
  232. function appendCreateFulltextIndex(array &$statements, $table, $name, array $def)
  233. {
  234. $statements[] = "CREATE FULLTEXT INDEX $name ON $table " . $this->buildIndexList($def);
  235. }
  236. /**
  237. * Close out a 'create table' SQL statement.
  238. *
  239. * @param string $name
  240. * @param array $def
  241. * @return string;
  242. *
  243. */
  244. function endCreateTable($name, array $def)
  245. {
  246. $engine = $this->preferredEngine($def);
  247. return ") ENGINE=$engine CHARACTER SET utf8mb4 COLLATE utf8mb4_bin";
  248. }
  249. function preferredEngine($def)
  250. {
  251. /* MyISAM is no longer required for fulltext indexes, fortunately
  252. if (!empty($def['fulltext indexes'])) {
  253. return 'MyISAM';
  254. }
  255. */
  256. return 'InnoDB';
  257. }
  258. /**
  259. * Get the unique index key name for a given column on this table
  260. * @param $tableName
  261. * @param $columnName
  262. * @return string
  263. */
  264. function _uniqueKey($tableName, $columnName)
  265. {
  266. return $this->_key($tableName, $columnName);
  267. }
  268. /**
  269. * Get the index key name for a given column on this table
  270. * @param $tableName
  271. * @param $columnName
  272. * @return string
  273. */
  274. function _key($tableName, $columnName)
  275. {
  276. return "{$tableName}_{$columnName}_idx";
  277. }
  278. /**
  279. * MySQL doesn't take 'DROP CONSTRAINT', need to treat primary keys as
  280. * if they were indexes here, but can use 'PRIMARY KEY' special name.
  281. *
  282. * @param array $phrase
  283. */
  284. function appendAlterDropPrimary(array &$phrase)
  285. {
  286. $phrase[] = 'DROP PRIMARY KEY';
  287. }
  288. /**
  289. * MySQL doesn't take 'DROP CONSTRAINT', need to treat unique keys as
  290. * if they were indexes here.
  291. *
  292. * @param array $phrase
  293. * @param string $keyName MySQL
  294. */
  295. function appendAlterDropUnique(array &$phrase, $keyName)
  296. {
  297. $phrase[] = 'DROP INDEX ' . $keyName;
  298. }
  299. /**
  300. * Throw some table metadata onto the ALTER TABLE if we have a mismatch
  301. * in expected type, collation.
  302. * @param array $phrase
  303. * @param $tableName
  304. * @param array $def
  305. * @throws Exception
  306. */
  307. function appendAlterExtras(array &$phrase, $tableName, array $def)
  308. {
  309. // Check for table properties: make sure we're using a sane
  310. // engine type and charset/collation.
  311. // @fixme make the default engine configurable?
  312. $oldProps = $this->getTableProperties($tableName, ['ENGINE', 'TABLE_COLLATION']);
  313. $engine = $this->preferredEngine($def);
  314. if (strtolower($oldProps['ENGINE']) != strtolower($engine)) {
  315. $phrase[] = "ENGINE=$engine";
  316. }
  317. if (strtolower($oldProps['TABLE_COLLATION']) != 'utf8mb4_bin') {
  318. $phrase[] = 'CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_bin';
  319. $phrase[] = 'DEFAULT CHARACTER SET = utf8mb4';
  320. $phrase[] = 'DEFAULT COLLATE = utf8mb4_bin';
  321. }
  322. }
  323. /**
  324. * Is this column a string type?
  325. * @param array $cd
  326. * @return bool
  327. */
  328. private function _isString(array $cd)
  329. {
  330. $strings = ['char', 'varchar', 'text'];
  331. return in_array(strtolower($cd['type']), $strings);
  332. }
  333. /**
  334. * Return the proper SQL for creating or
  335. * altering a column.
  336. *
  337. * Appropriate for use in CREATE TABLE or
  338. * ALTER TABLE statements.
  339. *
  340. * @param array $cd column to create
  341. *
  342. * @return string correct SQL for that column
  343. */
  344. function columnSql(array $cd)
  345. {
  346. $line = [];
  347. $line[] = parent::columnSql($cd);
  348. // This'll have been added from our transform of 'serial' type
  349. if (!empty($cd['auto_increment'])) {
  350. $line[] = 'auto_increment';
  351. }
  352. if (!empty($cd['description'])) {
  353. $line[] = 'comment';
  354. $line[] = $this->quoteValue($cd['description']);
  355. }
  356. return implode(' ', $line);
  357. }
  358. function mapType($column)
  359. {
  360. $map = [
  361. 'serial' => 'int',
  362. 'integer' => 'int',
  363. 'numeric' => 'decimal'
  364. ];
  365. $type = $column['type'];
  366. if (isset($map[$type])) {
  367. $type = $map[$type];
  368. }
  369. if (!empty($column['size'])) {
  370. $size = $column['size'];
  371. if ($type == 'int' &&
  372. in_array($size, ['tiny', 'small', 'medium', 'big'])) {
  373. $type = $size . $type;
  374. } else if (in_array($type, ['blob', 'text']) &&
  375. in_array($size, ['tiny', 'medium', 'long'])) {
  376. $type = $size . $type;
  377. }
  378. }
  379. return $type;
  380. }
  381. function typeAndSize($column)
  382. {
  383. if ($column['type'] == 'enum') {
  384. $vals = array_map([$this, 'quote'], $column['enum']);
  385. return 'enum(' . implode(',', $vals) . ')';
  386. } else if ($this->_isString($column)) {
  387. $col = parent::typeAndSize($column);
  388. if (!empty($column['charset'])) {
  389. $col .= ' CHARSET ' . $column['charset'];
  390. }
  391. if (!empty($column['collate'])) {
  392. $col .= ' COLLATE ' . $column['collate'];
  393. }
  394. return $col;
  395. } else {
  396. return parent::typeAndSize($column);
  397. }
  398. }
  399. /**
  400. * Filter the given table definition array to match features available
  401. * in this database.
  402. *
  403. * This lets us strip out unsupported things like comments, foreign keys,
  404. * or type variants that we wouldn't get back from getTableDef().
  405. *
  406. * @param array $tableDef
  407. * @return array
  408. */
  409. function filterDef(array $tableDef)
  410. {
  411. $version = $this->conn->getVersion();
  412. foreach ($tableDef['fields'] as $name => &$col) {
  413. if ($col['type'] == 'serial') {
  414. $col['type'] = 'int';
  415. $col['auto_increment'] = true;
  416. }
  417. $col['type'] = $this->mapType($col);
  418. unset($col['size']);
  419. }
  420. if (!common_config('db', 'mysql_foreign_keys')) {
  421. unset($tableDef['foreign keys']);
  422. }
  423. return $tableDef;
  424. }
  425. }