Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
platform
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
56
0.00% covered (danger)
0.00%
0 / 1
 getAlterTableSQL
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
56
1<?php
2/**
3 *
4 * This file is part of the phpBB Forum Software package.
5 *
6 * @copyright (c) phpBB Limited <https://www.phpbb.com>
7 * @license GNU General Public License, version 2 (GPL-2.0)
8 *
9 * For full copyright and license information, please see
10 * the docs/CREDITS.txt file.
11 *
12 */
13
14namespace phpbb\db\middleware\mysql;
15
16use Doctrine\DBAL\Platforms\AbstractMySQLPlatform;
17use Doctrine\DBAL\Schema\TableDiff;
18
19/**
20 * MySQL specific schema handling.
21 *
22 * While adding auto_increment column to MySQL, it must be indexed.
23 * If it's indexed as primary key, it should be declared as NOT NULL
24 * because MySQL primary key columns cannot be NULL.
25 */
26class platform extends AbstractMySQLPlatform
27{
28    /**
29     * {@inheritDoc}
30     */
31    public function getAlterTableSQL(TableDiff $diff)
32    {
33        $sql = parent::getAlterTableSQL($diff);
34        $table = $diff->getOldTable();
35        $columns = $diff->getAddedColumns();
36
37        foreach ($columns as $column)
38        {
39            $column_name = $column->getName();
40            if (!empty($column->getAutoincrement()) && $table)
41            {
42                foreach ($sql as $i => $query)
43                {
44                    if (stripos($query, "add $column_name"))
45                    {
46                        if (!$table->getPrimaryKey())
47                        {
48                            $sql[$i] = str_replace(' DEFAULT NULL', '', $sql[$i]);
49                            $sql[$i] .= ' PRIMARY KEY';
50                        }
51                        else
52                        {
53                            $sql[$i] .= ", ADD KEY ($column_name)";
54                        }
55                    }
56                }
57            }
58        }
59
60        return $sql;
61    }
62}