mysqlschema.php 14 KB

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