Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
D
doctrine-dbal
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Tomáš Trávníček
doctrine-dbal
Commits
86974572
Commit
86974572
authored
Sep 09, 2016
by
Marco Pivetta
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'fix/#2303-#2302-primary-key-alteration-with-auto-increment-on-mysql-2.5' into 2.5
Close #2302 Close #2303
parents
dcce6f86
488183a8
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
93 additions
and
16 deletions
+93
-16
MySqlPlatform.php
lib/Doctrine/DBAL/Platforms/MySqlPlatform.php
+39
-16
AbstractMySQLPlatformTestCase.php
...ne/Tests/DBAL/Platforms/AbstractMySQLPlatformTestCase.php
+54
-0
No files found.
lib/Doctrine/DBAL/Platforms/MySqlPlatform.php
View file @
86974572
...
...
@@ -626,23 +626,12 @@ class MySqlPlatform extends AbstractPlatform
$sql
=
array
();
$table
=
$diff
->
getName
(
$this
)
->
getQuotedName
(
$this
);
foreach
(
$diff
->
removedIndexes
as
$remKey
=>
$remIndex
)
{
// Dropping primary keys requires to unset autoincrement attribute on the particular column first.
if
(
$remIndex
->
isPrimary
()
&&
$diff
->
fromTable
instanceof
Table
)
{
foreach
(
$remIndex
->
getColumns
()
as
$columnName
)
{
$column
=
$diff
->
fromTable
->
getColumn
(
$columnName
);
if
(
$column
->
getAutoincrement
()
===
true
)
{
$column
->
setAutoincrement
(
false
);
$sql
[]
=
'ALTER TABLE '
.
$table
.
' MODIFY '
.
$this
->
getColumnDeclarationSQL
(
$column
->
getQuotedName
(
$this
),
$column
->
toArray
());
foreach
(
$diff
->
changedIndexes
as
$changedIndex
)
{
$sql
=
array_merge
(
$sql
,
$this
->
getPreAlterTableAlterPrimaryKeySQL
(
$diff
,
$changedIndex
));
}
// original autoincrement information might be needed later on by other parts of the table alteration
$column
->
setAutoincrement
(
true
);
}
}
}
foreach
(
$diff
->
removedIndexes
as
$remKey
=>
$remIndex
)
{
$sql
=
array_merge
(
$sql
,
$this
->
getPreAlterTableAlterPrimaryKeySQL
(
$diff
,
$remIndex
));
foreach
(
$diff
->
addedIndexes
as
$addKey
=>
$addIndex
)
{
if
(
$remIndex
->
getColumns
()
==
$addIndex
->
getColumns
())
{
...
...
@@ -692,6 +681,40 @@ class MySqlPlatform extends AbstractPlatform
return
$sql
;
}
/**
* @param TableDiff $diff
* @param Index $index
*
* @return string[]
*/
private
function
getPreAlterTableAlterPrimaryKeySQL
(
TableDiff
$diff
,
Index
$index
)
{
$sql
=
array
();
if
(
!
$index
->
isPrimary
()
||
!
$diff
->
fromTable
instanceof
Table
)
{
return
$sql
;
}
$tableName
=
$diff
->
getName
(
$this
)
->
getQuotedName
(
$this
);
// Dropping primary keys requires to unset autoincrement attribute on the particular column first.
foreach
(
$index
->
getColumns
()
as
$columnName
)
{
$column
=
$diff
->
fromTable
->
getColumn
(
$columnName
);
if
(
$column
->
getAutoincrement
()
===
true
)
{
$column
->
setAutoincrement
(
false
);
$sql
[]
=
'ALTER TABLE '
.
$tableName
.
' MODIFY '
.
$this
->
getColumnDeclarationSQL
(
$column
->
getQuotedName
(
$this
),
$column
->
toArray
());
// original autoincrement information might be needed later on by other parts of the table alteration
$column
->
setAutoincrement
(
true
);
}
}
return
$sql
;
}
/**
* @param TableDiff $diff The table diff to gather the SQL for.
*
...
...
tests/Doctrine/Tests/DBAL/Platforms/AbstractMySQLPlatformTestCase.php
View file @
86974572
...
...
@@ -384,6 +384,60 @@ abstract class AbstractMySQLPlatformTestCase extends AbstractPlatformTestCase
);
}
/**
* @group DBAL-2302
*/
public
function
testDropNonAutoincrementColumnFromCompositePrimaryKeyWithAutoincrementColumn
()
{
$table
=
new
Table
(
"tbl"
);
$table
->
addColumn
(
'id'
,
'integer'
,
array
(
'autoincrement'
=>
true
));
$table
->
addColumn
(
'foo'
,
'integer'
);
$table
->
addColumn
(
'bar'
,
'integer'
);
$table
->
setPrimaryKey
(
array
(
'id'
,
'foo'
));
$comparator
=
new
Comparator
();
$diffTable
=
clone
$table
;
$diffTable
->
dropPrimaryKey
();
$diffTable
->
setPrimaryKey
(
array
(
'id'
));
$this
->
assertSame
(
array
(
'ALTER TABLE tbl MODIFY id INT NOT NULL'
,
'ALTER TABLE tbl DROP PRIMARY KEY'
,
'ALTER TABLE tbl ADD PRIMARY KEY (id)'
,
),
$this
->
_platform
->
getAlterTableSQL
(
$comparator
->
diffTable
(
$table
,
$diffTable
))
);
}
/**
* @group DBAL-2302
*/
public
function
testAddNonAutoincrementColumnToPrimaryKeyWithAutoincrementColumn
()
{
$table
=
new
Table
(
"tbl"
);
$table
->
addColumn
(
'id'
,
'integer'
,
array
(
'autoincrement'
=>
true
));
$table
->
addColumn
(
'foo'
,
'integer'
);
$table
->
addColumn
(
'bar'
,
'integer'
);
$table
->
setPrimaryKey
(
array
(
'id'
));
$comparator
=
new
Comparator
();
$diffTable
=
clone
$table
;
$diffTable
->
dropPrimaryKey
();
$diffTable
->
setPrimaryKey
(
array
(
'id'
,
'foo'
));
$this
->
assertSame
(
array
(
'ALTER TABLE tbl MODIFY id INT NOT NULL'
,
'ALTER TABLE tbl DROP PRIMARY KEY'
,
'ALTER TABLE tbl ADD PRIMARY KEY (id, foo)'
,
),
$this
->
_platform
->
getAlterTableSQL
(
$comparator
->
diffTable
(
$table
,
$diffTable
))
);
}
/**
* @group DBAL-586
*/
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment