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
35e0121b
Commit
35e0121b
authored
Dec 05, 2009
by
beberlei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[2.0] DDC-169 - Implemented Index and ForeignKey comparison based on properties and not on names.
parent
e9f2f673
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
146 additions
and
61 deletions
+146
-61
Comparator.php
lib/Doctrine/DBAL/Schema/Comparator.php
+57
-47
Index.php
lib/Doctrine/DBAL/Schema/Index.php
+3
-1
TableDiff.php
lib/Doctrine/DBAL/Schema/TableDiff.php
+12
-12
ComparatorTest.php
tests/Doctrine/Tests/DBAL/Schema/ComparatorTest.php
+74
-1
No files found.
lib/Doctrine/DBAL/Schema/Comparator.php
View file @
35e0121b
...
...
@@ -147,14 +147,14 @@ class Comparator
/* See if all the fields in table 1 exist in table 2 */
foreach
(
$table2
->
getColumns
()
as
$columnName
=>
$column
)
{
if
(
!
$table1
->
hasColumn
(
$columnName
)
)
{
$tableDifferences
->
added
Field
s
[
$columnName
]
=
$column
;
$tableDifferences
->
added
Column
s
[
$columnName
]
=
$column
;
$changes
++
;
}
}
/* See if there are any removed fields in table 2 */
foreach
(
$table1
->
getColumns
()
as
$columnName
=>
$column
)
{
if
(
!
$table2
->
hasColumn
(
$columnName
)
)
{
$tableDifferences
->
removed
Field
s
[
$columnName
]
=
true
;
$tableDifferences
->
removed
Column
s
[
$columnName
]
=
true
;
$changes
++
;
}
}
...
...
@@ -162,7 +162,7 @@ class Comparator
foreach
(
$table1
->
getColumns
()
as
$columnName
=>
$column
)
{
if
(
$table2
->
hasColumn
(
$columnName
)
)
{
if
(
$this
->
diffColumn
(
$column
,
$table2
->
getColumn
(
$columnName
)
)
)
{
$tableDifferences
->
changed
Field
s
[
$columnName
]
=
$table2
->
getColumn
(
$columnName
);
$tableDifferences
->
changed
Column
s
[
$columnName
]
=
$table2
->
getColumn
(
$columnName
);
$changes
++
;
}
}
...
...
@@ -171,49 +171,59 @@ class Comparator
$table1Indexes
=
$table1
->
getIndexes
();
$table2Indexes
=
$table2
->
getIndexes
();
/* See if all the indexes in table 1 exist in table 2 */
foreach
(
$table2Indexes
as
$indexName
=>
$indexDefinition
)
{
if
(
!
isset
(
$table1Indexes
[
$indexName
]
)
)
{
$tableDifferences
->
addedIndexes
[
$indexName
]
=
$indexDefinition
;
$changes
++
;
foreach
(
$table2Indexes
AS
$index2Name
=>
$index2Definition
)
{
foreach
(
$table1Indexes
AS
$index1Name
=>
$index1Definition
)
{
if
(
$this
->
diffIndex
(
$index1Definition
,
$index2Definition
)
===
false
)
{
unset
(
$table1Indexes
[
$index1Name
]);
unset
(
$table2Indexes
[
$index2Name
]);
}
else
{
if
(
$index1Name
==
$index2Name
)
{
$tableDifferences
->
changedIndexes
[
$index2Name
]
=
$table2Indexes
[
$index2Name
];
unset
(
$table1Indexes
[
$index1Name
]);
unset
(
$table2Indexes
[
$index2Name
]);
$changes
++
;
}
}
}
}
/* See if there are any removed indexes in table 2 */
foreach
(
$table1Indexes
as
$indexName
=>
$indexDefinition
)
{
if
(
!
isset
(
$table2Indexes
[
$indexName
]
)
)
{
$tableDifferences
->
removedIndexes
[
$indexName
]
=
true
;
$changes
++
;
}
foreach
(
$table1Indexes
AS
$index1Name
=>
$index1Definition
)
{
$tableDifferences
->
removedIndexes
[
$index1Name
]
=
true
;
$changes
++
;
}
/* See if there are any changed indexDefinitions */
foreach
(
$table1Indexes
as
$indexName
=>
$indexDefinition
)
{
if
(
isset
(
$table2Indexes
[
$indexName
]
)
)
{
if
(
$this
->
diffIndex
(
$indexDefinition
,
$table2Indexes
[
$indexName
]
)
)
{
$tableDifferences
->
changedIndexes
[
$indexName
]
=
$table2Indexes
[
$indexName
];
$changes
++
;
}
}
foreach
(
$table2Indexes
AS
$index2Name
=>
$index2Definition
)
{
$tableDifferences
->
addedIndexes
[
$index2Name
]
=
$index2Definition
;
$changes
++
;
}
foreach
(
$table2
->
getForeignKeys
()
AS
$constraint
)
{
$fkName
=
$constraint
->
getName
();
if
(
!
$table1
->
hasForeignKey
(
$fkName
))
{
$tableDifferences
->
addedForeignKeys
[
$fkName
]
=
$constraint
;
$changes
++
;
}
else
{
if
(
$this
->
diffForeignKey
(
$constraint
,
$table1
->
getForeignKey
(
$fkName
)))
{
$tableDifferences
->
changedForeignKeys
[
$fkName
]
=
$constraint
;
$changes
++
;
$fromFkeys
=
$table1
->
getForeignKeys
();
$toFkeys
=
$table2
->
getForeignKeys
();
foreach
(
$fromFkeys
AS
$key1
=>
$constraint1
)
{
foreach
(
$toFkeys
AS
$key2
=>
$constraint2
)
{
if
(
$this
->
diffForeignKey
(
$constraint1
,
$constraint2
)
===
false
)
{
unset
(
$fromFkeys
[
$key1
]);
unset
(
$toFkeys
[
$key2
]);
}
else
{
if
(
strtolower
(
$constraint1
->
getName
())
==
strtolower
(
$constraint2
->
getName
()))
{
$tableDifferences
->
changedForeignKeys
[]
=
$constraint2
;
$changes
++
;
unset
(
$fromFkeys
[
$key1
]);
unset
(
$toFkeys
[
$key2
]);
}
}
}
}
foreach
(
$table1
->
getForeignKeys
()
AS
$constraint
)
{
$fkName
=
$constraint
->
getName
();
if
(
!
$table2
->
hasForeignKey
(
$fkName
))
{
$tableDifferences
->
removedForeignKeys
[
$fkName
]
=
$constraint
;
$changes
++
;
}
foreach
(
$fromFkeys
AS
$key1
=>
$constraint1
)
{
$tableDifferences
->
removedForeignKeys
[]
=
$constraint1
;
$changes
++
;
}
foreach
(
$toFkeys
AS
$key2
=>
$constraint2
)
{
$tableDifferences
->
addedForeignKeys
[]
=
$constraint2
;
$changes
++
;
}
return
$changes
?
$tableDifferences
:
false
;
...
...
@@ -234,19 +244,19 @@ class Comparator
return
true
;
}
if
(
$key1
->
hasOption
(
'onUpdate'
)
!=
$key2
->
hasOption
(
'onUpdate'
))
{
return
true
;
}
if
(
$key1
->
getOption
(
'onUpdate'
)
!=
$key2
->
getOption
(
'onUpdate'
))
{
return
true
;
}
if
(
$key1
->
hasOption
(
'onDelete'
)
!=
$key2
->
hasOption
(
'onDelete'
))
{
if
(
$key1
->
hasOption
(
'onUpdate'
)
&&
$key
->
hasOption
(
'onUpdate'
))
{
if
(
$key1
->
getOption
(
'onUpdate'
)
!=
$key2
->
getOption
(
'onUpdate'
))
{
return
true
;
}
}
else
if
(
$key1
->
hasOption
(
'onUpdate'
)
!=
$key2
->
hasOption
(
'onUpdate'
))
{
return
true
;
}
if
(
$key1
->
getOption
(
'onDelete'
)
!=
$key2
->
getOption
(
'onDelete'
))
{
if
(
$key1
->
hasOption
(
'onDelete'
)
&&
$key2
->
hasOption
(
'onDelete'
))
{
if
(
$key1
->
getOption
(
'onDelete'
)
!=
$key2
->
getOption
(
'onDelete'
))
{
return
true
;
}
}
else
if
(
$key1
->
hasOption
(
'onDelete'
)
!=
$key2
->
hasOption
(
'onDelete'
))
{
return
true
;
}
...
...
lib/Doctrine/DBAL/Schema/Index.php
View file @
35e0121b
...
...
@@ -102,6 +102,8 @@ class Index extends AbstractAsset implements Constraint
*/
public
function
hasColumnAtPosition
(
$columnName
,
$pos
=
0
)
{
return
\array_search
(
$columnName
,
$this
->
getColumns
())
===
$pos
;
$columnName
=
strtolower
(
$columnName
);
$indexColumns
=
\array_map
(
'strtolower'
,
$this
->
getColumns
());
return
\array_search
(
$columnName
,
$indexColumns
)
===
$pos
;
}
}
\ No newline at end of file
lib/Doctrine/DBAL/Schema/TableDiff.php
View file @
35e0121b
...
...
@@ -37,23 +37,23 @@ class TableDiff
/**
* All added fields
*
* @var array(string=>
ezcDbSchemaField
)
* @var array(string=>
Column
)
*/
public
$added
Field
s
;
public
$added
Column
s
;
/**
* All changed fields
*
* @var array(string=>Column)
*/
public
$changed
Field
s
=
array
();
public
$changed
Column
s
=
array
();
/**
* All removed fields
*
* @var array(string=>bool)
*/
public
$removed
Field
s
=
array
();
public
$removed
Column
s
=
array
();
/**
* All added indexes
...
...
@@ -100,20 +100,20 @@ class TableDiff
/**
* Constructs an TableDiff object.
*
* @param array(string=>Column) $added
Field
s
* @param array(string=>Column) $changed
Field
s
* @param array(string=>bool) $removed
Field
s
* @param array(string=>Column) $added
Column
s
* @param array(string=>Column) $changed
Column
s
* @param array(string=>bool) $removed
Column
s
* @param array(string=>Index) $addedIndexes
* @param array(string=>Index) $changedIndexes
* @param array(string=>bool) $removedIndexes
*/
function
__construct
(
$added
Fields
=
array
(),
$changedField
s
=
array
(),
$removed
Field
s
=
array
(),
$addedIndexes
=
array
(),
$changedIndexes
=
function
__construct
(
$added
Columns
=
array
(),
$changedColumn
s
=
array
(),
$removed
Column
s
=
array
(),
$addedIndexes
=
array
(),
$changedIndexes
=
array
(),
$removedIndexes
=
array
()
)
{
$this
->
added
Fields
=
$addedField
s
;
$this
->
changed
Fields
=
$changedField
s
;
$this
->
removed
Fields
=
$removedField
s
;
$this
->
added
Columns
=
$addedColumn
s
;
$this
->
changed
Columns
=
$changedColumn
s
;
$this
->
removed
Columns
=
$removedColumn
s
;
$this
->
addedIndexes
=
$addedIndexes
;
$this
->
changedIndexes
=
$changedIndexes
;
$this
->
removedIndexes
=
$removedIndexes
;
...
...
tests/Doctrine/Tests/DBAL/Schema/ComparatorTest.php
View file @
35e0121b
...
...
@@ -494,11 +494,71 @@ class ComparatorTest extends \PHPUnit_Framework_TestCase
public
function
testSequencesCaseInsenstive
()
{
$schemaA
=
new
Schema
();
$schemaA
->
createSequence
(
'foo'
);
$schemaA
->
createSequence
(
'BAR'
);
$schemaA
->
createSequence
(
'Baz'
);
$schemaA
->
createSequence
(
'new'
);
$schemaB
=
new
Schema
();
$schemaB
->
createSequence
(
'FOO'
);
$schemaB
->
createSequence
(
'Bar'
);
$schemaB
->
createSequence
(
'baz'
);
$schemaB
->
createSequence
(
'old'
);
$c
=
new
Comparator
();
$diff
=
$c
->
compare
(
$schemaA
,
$schemaB
);
$this
->
assertSchemaSequenceChangeCount
(
$diff
,
1
,
0
,
1
);
}
public
function
testCompareColumnCompareCaseInsensitive
()
{
$tableA
=
new
Table
(
"foo"
);
$tableA
->
createColumn
(
'id'
,
'integer'
);
$tableB
=
new
Table
(
"foo"
);
$tableB
->
createColumn
(
'ID'
,
'integer'
);
$c
=
new
Comparator
();
$tableDiff
=
$c
->
diffTable
(
$tableA
,
$tableB
);
$this
->
assertFalse
(
$tableDiff
);
}
public
function
testCompareIndexBasedOnPropertiesNotName
()
{
$tableA
=
new
Table
(
"foo"
);
$tableA
->
createColumn
(
'id'
,
'integer'
);
$tableA
->
addIndex
(
array
(
"id"
),
"foo_bar_idx"
);
$tableB
=
new
Table
(
"foo"
);
$tableB
->
createColumn
(
'ID'
,
'integer'
);
$tableB
->
addIndex
(
array
(
"id"
),
"bar_foo_idx"
);
$c
=
new
Comparator
();
$tableDiff
=
$c
->
diffTable
(
$tableA
,
$tableB
);
$this
->
assertFalse
(
$tableDiff
);
}
public
function
testCompareForeignKeyBasedOnPropertiesNotName
()
{
$tableA
=
new
Table
(
"foo"
);
$tableA
->
createColumn
(
'id'
,
'integer'
);
$tableA
->
addNamedForeignKeyConstraint
(
'foo_constraint'
,
'bar'
,
array
(
'id'
),
array
(
'id'
));
$tableB
=
new
Table
(
"foo"
);
$tableB
->
createColumn
(
'ID'
,
'integer'
);
$tableB
->
addNamedForeignKeyConstraint
(
'bar_constraint'
,
'bar'
,
array
(
'id'
),
array
(
'id'
));
$c
=
new
Comparator
();
$tableDiff
=
$c
->
diffTable
(
$tableA
,
$tableB
);
$this
->
assertFalse
(
$tableDiff
);
}
/**
*
* @param SchemaDiff $diff
* @param int $newTableCount
* @param int $changeTableCount
...
...
@@ -510,4 +570,17 @@ class ComparatorTest extends \PHPUnit_Framework_TestCase
$this
->
assertEquals
(
$changeTableCount
,
count
(
$diff
->
changedTables
));
$this
->
assertEquals
(
$removeTableCount
,
count
(
$diff
->
removedTables
));
}
/**
* @param SchemaDiff $diff
* @param int $newSequenceCount
* @param int $changeSequenceCount
* @param int $changeSequenceCount
*/
public
function
assertSchemaSequenceChangeCount
(
$diff
,
$newSequenceCount
=
0
,
$changeSequenceCount
=
0
,
$removeSequenceCount
=
0
)
{
$this
->
assertEquals
(
$newSequenceCount
,
count
(
$diff
->
newSequences
),
"Expected number of new sequences is wrong."
);
$this
->
assertEquals
(
$changeSequenceCount
,
count
(
$diff
->
changedSequences
),
"Expected number of changed sequences is wrong."
);
$this
->
assertEquals
(
$removeSequenceCount
,
count
(
$diff
->
removedSequences
),
"Expected number of removed sequences is wrong."
);
}
}
\ No newline at end of file
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