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
1c9fe8df
Commit
1c9fe8df
authored
Apr 02, 2014
by
Steve Müller
Committed by
Marco Pivetta
Aug 18, 2014
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
create namespaces in schema explicitly
parent
1625836c
Changes
12
Hide whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
496 additions
and
84 deletions
+496
-84
AbstractSchemaManager.php
lib/Doctrine/DBAL/Schema/AbstractSchemaManager.php
+9
-1
Comparator.php
lib/Doctrine/DBAL/Schema/Comparator.php
+11
-5
Schema.php
lib/Doctrine/DBAL/Schema/Schema.php
+81
-11
SchemaDiff.php
lib/Doctrine/DBAL/Schema/SchemaDiff.php
+7
-0
SchemaException.php
lib/Doctrine/DBAL/Schema/SchemaException.php
+14
-0
AbstractVisitor.php
lib/Doctrine/DBAL/Schema/Visitor/AbstractVisitor.php
+8
-1
CreateSchemaSqlCollector.php
...Doctrine/DBAL/Schema/Visitor/CreateSchemaSqlCollector.php
+29
-44
NamespaceVisitor.php
lib/Doctrine/DBAL/Schema/Visitor/NamespaceVisitor.php
+37
-0
ComparatorTest.php
tests/Doctrine/Tests/DBAL/Schema/ComparatorTest.php
+45
-1
SchemaDiffTest.php
tests/Doctrine/Tests/DBAL/Schema/SchemaDiffTest.php
+11
-2
SchemaTest.php
tests/Doctrine/Tests/DBAL/Schema/SchemaTest.php
+99
-0
CreateSchemaSqlCollectorTest.php
...ests/DBAL/Schema/Visitor/CreateSchemaSqlCollectorTest.php
+145
-19
No files found.
lib/Doctrine/DBAL/Schema/AbstractSchemaManager.php
View file @
1c9fe8df
...
...
@@ -1023,13 +1023,21 @@ abstract class AbstractSchemaManager
*/
public
function
createSchema
()
{
$namespaces
=
array
();
if
(
$this
->
_platform
->
supportsSchemas
())
{
$namespaces
=
$this
->
listNamespaceNames
();
}
$sequences
=
array
();
if
(
$this
->
_platform
->
supportsSequences
())
{
$sequences
=
$this
->
listSequences
();
}
$tables
=
$this
->
listTables
();
return
new
Schema
(
$tables
,
$sequences
,
$this
->
createSchemaConfig
());
return
new
Schema
(
$tables
,
$sequences
,
$this
->
createSchemaConfig
()
,
$namespaces
);
}
/**
...
...
lib/Doctrine/DBAL/Schema/Comparator.php
View file @
1c9fe8df
...
...
@@ -62,13 +62,19 @@ class Comparator
$foreignKeysToTable
=
array
();
foreach
(
$toSchema
->
getTables
()
as
$table
)
{
$namespace
=
$table
->
getNamespaceName
();
if
(
$namespace
&&
!
$fromSchema
->
hasNamespace
(
$namespace
))
{
foreach
(
$toSchema
->
getNamespaces
()
as
$namespace
)
{
if
(
!
$fromSchema
->
hasNamespace
(
$namespace
))
{
$diff
->
newNamespaces
[
$namespace
]
=
$namespace
;
}
}
foreach
(
$fromSchema
->
getNamespaces
()
as
$namespace
)
{
if
(
!
$toSchema
->
hasNamespace
(
$namespace
))
{
$diff
->
removedNamespaces
[
$namespace
]
=
$namespace
;
}
}
foreach
(
$toSchema
->
getTables
()
as
$table
)
{
$tableName
=
$table
->
getShortestName
(
$toSchema
->
getName
());
if
(
!
$fromSchema
->
hasTable
(
$tableName
))
{
$diff
->
newTables
[
$tableName
]
=
$toSchema
->
getTable
(
$tableName
);
...
...
@@ -383,7 +389,7 @@ class Comparator
$changedProperties
[]
=
$property
;
}
}
if
(
$properties1
[
'default'
]
!=
$properties2
[
'default'
]
||
// Null values need to be checked additionally as they tell whether to create or drop a default value.
// null != 0, null != false, null != '' etc. This affects platform's table alteration SQL generation.
...
...
lib/Doctrine/DBAL/Schema/Schema.php
View file @
1c9fe8df
...
...
@@ -54,6 +54,13 @@ use Doctrine\DBAL\Platforms\AbstractPlatform;
*/
class
Schema
extends
AbstractAsset
{
/**
* The namespaces in this schema.
*
* @var array
*/
private
$namespaces
=
array
();
/**
* @var \Doctrine\DBAL\Schema\Table[]
*/
...
...
@@ -73,15 +80,24 @@ class Schema extends AbstractAsset
* @param \Doctrine\DBAL\Schema\Table[] $tables
* @param \Doctrine\DBAL\Schema\Sequence[] $sequences
* @param \Doctrine\DBAL\Schema\SchemaConfig $schemaConfig
* @param array $namespaces
*/
public
function
__construct
(
array
$tables
=
array
(),
array
$sequences
=
array
(),
SchemaConfig
$schemaConfig
=
null
)
{
public
function
__construct
(
array
$tables
=
array
(),
array
$sequences
=
array
(),
SchemaConfig
$schemaConfig
=
null
,
array
$namespaces
=
array
()
)
{
if
(
$schemaConfig
==
null
)
{
$schemaConfig
=
new
SchemaConfig
();
}
$this
->
_schemaConfig
=
$schemaConfig
;
$this
->
_setName
(
$schemaConfig
->
getName
()
?:
'public'
);
foreach
(
$namespaces
as
$namespace
)
{
$this
->
createNamespace
(
$namespace
);
}
foreach
(
$tables
as
$table
)
{
$this
->
_addTable
(
$table
);
}
...
...
@@ -108,11 +124,17 @@ class Schema extends AbstractAsset
*/
protected
function
_addTable
(
Table
$table
)
{
$namespaceName
=
$table
->
getNamespaceName
();
$tableName
=
$table
->
getFullQualifiedName
(
$this
->
getName
());
if
(
isset
(
$this
->
_tables
[
$tableName
]))
{
throw
SchemaException
::
tableAlreadyExists
(
$tableName
);
}
if
(
!
$table
->
isInDefaultNamespace
(
$this
->
getName
())
&&
!
$this
->
hasNamespace
(
$namespaceName
))
{
$this
->
createNamespace
(
$namespaceName
);
}
$this
->
_tables
[
$tableName
]
=
$table
;
$table
->
setSchemaConfig
(
$this
->
_schemaConfig
);
}
...
...
@@ -126,13 +148,30 @@ class Schema extends AbstractAsset
*/
protected
function
_addSequence
(
Sequence
$sequence
)
{
$namespaceName
=
$sequence
->
getNamespaceName
();
$seqName
=
$sequence
->
getFullQualifiedName
(
$this
->
getName
());
if
(
isset
(
$this
->
_sequences
[
$seqName
]))
{
throw
SchemaException
::
sequenceAlreadyExists
(
$seqName
);
}
if
(
!
$sequence
->
isInDefaultNamespace
(
$this
->
getName
())
&&
!
$this
->
hasNamespace
(
$namespaceName
))
{
$this
->
createNamespace
(
$namespaceName
);
}
$this
->
_sequences
[
$seqName
]
=
$sequence
;
}
/**
* Returns the namespaces of this schema.
*
* @return array A list of namespace names.
*/
public
function
getNamespaces
()
{
return
$this
->
namespaces
;
}
/**
* Gets all tables of this schema.
*
...
...
@@ -167,9 +206,8 @@ class Schema extends AbstractAsset
*/
private
function
getFullQualifiedAssetName
(
$name
)
{
if
(
$this
->
isIdentifierQuoted
(
$name
))
{
$name
=
$this
->
trimQuotes
(
$name
);
}
$name
=
$this
->
getUnquotedAssetName
(
$name
);
if
(
strpos
(
$name
,
"."
)
===
false
)
{
$name
=
$this
->
getName
()
.
"."
.
$name
;
}
...
...
@@ -177,6 +215,22 @@ class Schema extends AbstractAsset
return
strtolower
(
$name
);
}
/**
* Returns the unquoted representation of a given asset name.
*
* @param string $assetName Quoted or unquoted representation of an asset name.
*
* @return string
*/
private
function
getUnquotedAssetName
(
$assetName
)
{
if
(
$this
->
isIdentifierQuoted
(
$assetName
))
{
return
$this
->
trimQuotes
(
$assetName
);
}
return
$assetName
;
}
/**
* Does this schema have a namespace with the given name?
*
...
...
@@ -186,13 +240,9 @@ class Schema extends AbstractAsset
*/
public
function
hasNamespace
(
$namespaceName
)
{
foreach
(
$this
->
_tables
as
$table
)
{
if
(
$table
->
getNamespaceName
()
===
$namespaceName
)
{
return
true
;
}
}
$namespaceName
=
strtolower
(
$this
->
getUnquotedAssetName
(
$namespaceName
));
return
false
;
return
isset
(
$this
->
namespaces
[
$namespaceName
])
;
}
/**
...
...
@@ -256,6 +306,26 @@ class Schema extends AbstractAsset
return
$this
->
_sequences
;
}
/**
* Creates a new namespace.
*
* @param string $namespaceName The name of the namespace to create.
*
* @return \Doctrine\DBAL\Schema\Schema This schema instance.
*/
public
function
createNamespace
(
$namespaceName
)
{
$unquotedNamespaceName
=
strtolower
(
$this
->
getUnquotedAssetName
(
$namespaceName
));
if
(
isset
(
$this
->
namespaces
[
$unquotedNamespaceName
]))
{
throw
SchemaException
::
namespaceAlreadyExists
(
$unquotedNamespaceName
);
}
$this
->
namespaces
[
$unquotedNamespaceName
]
=
$namespaceName
;
return
$this
;
}
/**
* Creates a new table.
*
...
...
lib/Doctrine/DBAL/Schema/SchemaDiff.php
View file @
1c9fe8df
...
...
@@ -44,6 +44,13 @@ class SchemaDiff
*/
public
$newNamespaces
=
array
();
/**
* All removed namespaces.
*
* @var string[]
*/
public
$removedNamespaces
=
array
();
/**
* All added tables.
*
...
...
lib/Doctrine/DBAL/Schema/SchemaException.php
View file @
1c9fe8df
...
...
@@ -31,6 +31,7 @@ class SchemaException extends \Doctrine\DBAL\DBALException
const
SEQUENCE_ALREADY_EXISTS
=
80
;
const
INDEX_INVALID_NAME
=
90
;
const
FOREIGNKEY_DOESNT_EXIST
=
100
;
const
NAMESPACE_ALREADY_EXISTS
=
110
;
/**
* @param string $tableName
...
...
@@ -85,6 +86,19 @@ class SchemaException extends \Doctrine\DBAL\DBALException
return
new
self
(
"There is no column with name '
$columnName
' on table '
$table
'."
,
self
::
COLUMN_DOESNT_EXIST
);
}
/**
* @param string $namespaceName
*
* @return \Doctrine\DBAL\Schema\SchemaException
*/
static
public
function
namespaceAlreadyExists
(
$namespaceName
)
{
return
new
self
(
sprintf
(
"The namespace with name '%s' already exists."
,
$namespaceName
),
self
::
NAMESPACE_ALREADY_EXISTS
);
}
/**
* @param string $tableName
*
...
...
lib/Doctrine/DBAL/Schema/Visitor/AbstractVisitor.php
View file @
1c9fe8df
...
...
@@ -29,7 +29,7 @@ use Doctrine\DBAL\Schema\Index;
/**
* Abstract Visitor with empty methods for easy extension.
*/
class
AbstractVisitor
implements
Visitor
class
AbstractVisitor
implements
Visitor
,
NamespaceVisitor
{
/**
* @param \Doctrine\DBAL\Schema\Schema $schema
...
...
@@ -38,6 +38,13 @@ class AbstractVisitor implements Visitor
{
}
/**
* {@inheritdoc}
*/
public
function
acceptNamespace
(
$namespaceName
)
{
}
/**
* @param \Doctrine\DBAL\Schema\Table $table
*/
...
...
lib/Doctrine/DBAL/Schema/Visitor/CreateSchemaSqlCollector.php
View file @
1c9fe8df
...
...
@@ -26,6 +26,11 @@ use Doctrine\DBAL\Schema\Sequence;
class
CreateSchemaSqlCollector
extends
AbstractVisitor
{
/**
* @var array
*/
private
$createNamespaceQueries
=
array
();
/**
* @var array
*/
...
...
@@ -58,14 +63,22 @@ class CreateSchemaSqlCollector extends AbstractVisitor
/**
* {@inheritdoc}
*/
public
function
accept
Table
(
Table
$tabl
e
)
public
function
accept
Namespace
(
$namespaceNam
e
)
{
$namespace
=
$this
->
getNamespace
(
$table
);
if
(
$this
->
platform
->
supportsSchemas
())
{
$this
->
createNamespaceQueries
=
array_merge
(
$this
->
createNamespaceQueries
,
(
array
)
$this
->
platform
->
getCreateSchemaSQL
(
$namespaceName
)
);
}
}
$this
->
createTableQueries
[
$namespace
]
=
array_merge
(
$this
->
createTableQueries
[
$namespace
],
$this
->
platform
->
getCreateTableSQL
(
$table
)
);
/**
* {@inheritdoc}
*/
public
function
acceptTable
(
Table
$table
)
{
$this
->
createTableQueries
=
array_merge
(
$this
->
createTableQueries
,
(
array
)
$this
->
platform
->
getCreateTableSQL
(
$table
));
}
/**
...
...
@@ -73,11 +86,9 @@ class CreateSchemaSqlCollector extends AbstractVisitor
*/
public
function
acceptForeignKey
(
Table
$localTable
,
ForeignKeyConstraint
$fkConstraint
)
{
$namespace
=
$this
->
getNamespace
(
$localTable
);
if
(
$this
->
platform
->
supportsForeignKeyConstraints
())
{
$this
->
createFkConstraintQueries
[
$namespace
]
=
array_merge
(
$this
->
createFkConstraintQueries
[
$namespace
]
,
$this
->
createFkConstraintQueries
=
array_merge
(
$this
->
createFkConstraintQueries
,
(
array
)
$this
->
platform
->
getCreateForeignKeySQL
(
$fkConstraint
,
$localTable
)
...
...
@@ -90,41 +101,18 @@ class CreateSchemaSqlCollector extends AbstractVisitor
*/
public
function
acceptSequence
(
Sequence
$sequence
)
{
$namespace
=
$this
->
getNamespace
(
$sequence
);
$this
->
createSequenceQueries
[
$namespace
]
=
array_merge
(
$this
->
createSequenceQueries
[
$namespace
],
$this
->
createSequenceQueries
=
array_merge
(
$this
->
createSequenceQueries
,
(
array
)
$this
->
platform
->
getCreateSequenceSQL
(
$sequence
)
);
}
/**
* @param \Doctrine\DBAL\Schema\AbstractAsset $asset
*
* @return string
*/
private
function
getNamespace
(
$asset
)
{
$namespace
=
$asset
->
getNamespaceName
();
if
(
!
isset
(
$namespace
))
{
$namespace
=
$this
->
platform
->
supportsSchemas
()
?
$this
->
platform
->
getDefaultSchemaName
()
:
'default'
;
}
if
(
!
isset
(
$this
->
createTableQueries
[
$namespace
]))
{
$this
->
createTableQueries
[
$namespace
]
=
array
();
$this
->
createSequenceQueries
[
$namespace
]
=
array
();
$this
->
createFkConstraintQueries
[
$namespace
]
=
array
();
}
return
$namespace
;
}
/**
* @return void
*/
public
function
resetQueries
()
{
$this
->
createNamespaceQueries
=
array
();
$this
->
createTableQueries
=
array
();
$this
->
createSequenceQueries
=
array
();
$this
->
createFkConstraintQueries
=
array
();
...
...
@@ -139,23 +127,20 @@ class CreateSchemaSqlCollector extends AbstractVisitor
{
$sql
=
array
();
foreach
(
array_keys
(
$this
->
createTableQueries
)
as
$namespace
)
{
if
(
$this
->
platform
->
supportsSchemas
())
{
$query
=
$this
->
platform
->
getCreateSchemaSQL
(
$namespace
);
$sql
[]
=
$query
;
}
foreach
(
$this
->
createNamespaceQueries
as
$schemaSql
)
{
$sql
=
array_merge
(
$sql
,
(
array
)
$schemaSql
);
}
foreach
(
$this
->
createTableQueries
as
$schemaSql
)
{
$sql
=
array_merge
(
$sql
,
$schemaSql
);
$sql
=
array_merge
(
$sql
,
(
array
)
$schemaSql
);
}
foreach
(
$this
->
createSequenceQueries
as
$schemaSql
)
{
$sql
=
array_merge
(
$sql
,
$schemaSql
);
$sql
=
array_merge
(
$sql
,
(
array
)
$schemaSql
);
}
foreach
(
$this
->
createFkConstraintQueries
as
$schemaSql
)
{
$sql
=
array_merge
(
$sql
,
$schemaSql
);
$sql
=
array_merge
(
$sql
,
(
array
)
$schemaSql
);
}
return
$sql
;
...
...
lib/Doctrine/DBAL/Schema/Visitor/NamespaceVisitor.php
0 → 100644
View file @
1c9fe8df
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace
Doctrine\DBAL\Schema\Visitor
;
/**
* Visitor that can visit schema namespaces.
*
* @author Steve Müller <st.mueller@dzh-online.de>
* @link www.doctrine-project.org
* @since 2.5
*/
interface
NamespaceVisitor
{
/**
* Accepts a schema namespace name.
*
* @param string $namespaceName The schema namespace name to accept.
*/
public
function
acceptNamespace
(
$namespaceName
);
}
tests/Doctrine/Tests/DBAL/Schema/ComparatorTest.php
View file @
1c9fe8df
...
...
@@ -802,7 +802,6 @@ class ComparatorTest extends \PHPUnit_Framework_TestCase
$expected
=
new
SchemaDiff
();
$expected
->
fromSchema
=
$oldSchema
;
$expected
->
newNamespaces
[
'foo'
]
=
'foo'
;
$this
->
assertEquals
(
$expected
,
Comparator
::
compareSchemas
(
$oldSchema
,
$newSchema
));
}
...
...
@@ -1063,4 +1062,49 @@ class ComparatorTest extends \PHPUnit_Framework_TestCase
$this
->
assertEquals
(
array
(),
$comparator
->
diffColumn
(
$column1
,
$column2
));
$this
->
assertEquals
(
array
(),
$comparator
->
diffColumn
(
$column2
,
$column1
));
}
/**
* @group DBAL-669
*/
public
function
testComparesNamespaces
()
{
$comparator
=
new
Comparator
();
$fromSchema
=
$this
->
getMock
(
'Doctrine\DBAL\Schema\Schema'
,
array
(
'getNamespaces'
,
'hasNamespace'
));
$toSchema
=
$this
->
getMock
(
'Doctrine\DBAL\Schema\Schema'
,
array
(
'getNamespaces'
,
'hasNamespace'
));
$fromSchema
->
expects
(
$this
->
once
())
->
method
(
'getNamespaces'
)
->
will
(
$this
->
returnValue
(
array
(
'foo'
,
'bar'
)));
$fromSchema
->
expects
(
$this
->
at
(
0
))
->
method
(
'hasNamespace'
)
->
with
(
'bar'
)
->
will
(
$this
->
returnValue
(
true
));
$fromSchema
->
expects
(
$this
->
at
(
1
))
->
method
(
'hasNamespace'
)
->
with
(
'baz'
)
->
will
(
$this
->
returnValue
(
false
));
$toSchema
->
expects
(
$this
->
once
())
->
method
(
'getNamespaces'
)
->
will
(
$this
->
returnValue
(
array
(
'bar'
,
'baz'
)));
$toSchema
->
expects
(
$this
->
at
(
1
))
->
method
(
'hasNamespace'
)
->
with
(
'foo'
)
->
will
(
$this
->
returnValue
(
false
));
$toSchema
->
expects
(
$this
->
at
(
2
))
->
method
(
'hasNamespace'
)
->
with
(
'bar'
)
->
will
(
$this
->
returnValue
(
true
));
$expected
=
new
SchemaDiff
();
$expected
->
fromSchema
=
$fromSchema
;
$expected
->
newNamespaces
=
array
(
'baz'
=>
'baz'
);
$expected
->
removedNamespaces
=
array
(
'foo'
=>
'foo'
);
$this
->
assertEquals
(
$expected
,
$comparator
->
compare
(
$fromSchema
,
$toSchema
));
}
}
tests/Doctrine/Tests/DBAL/Schema/SchemaDiffTest.php
View file @
1c9fe8df
...
...
@@ -18,7 +18,7 @@ class SchemaDiffTest extends \PHPUnit_Framework_TestCase
$sql
=
$diff
->
toSql
(
$platform
);
$expected
=
array
(
'drop_orphan_fk'
,
'alter_seq'
,
'drop_seq'
,
'create_seq'
,
'create_table'
,
'create_foreign_key'
,
'drop_table'
,
'alter_table'
);
$expected
=
array
(
'
create_schema'
,
'
drop_orphan_fk'
,
'alter_seq'
,
'drop_seq'
,
'create_seq'
,
'create_table'
,
'create_foreign_key'
,
'drop_table'
,
'alter_table'
);
$this
->
assertEquals
(
$expected
,
$sql
);
}
...
...
@@ -30,7 +30,7 @@ class SchemaDiffTest extends \PHPUnit_Framework_TestCase
$sql
=
$diff
->
toSaveSql
(
$platform
);
$expected
=
array
(
'alter_seq'
,
'create_seq'
,
'create_table'
,
'create_foreign_key'
,
'alter_table'
);
$expected
=
array
(
'
create_schema'
,
'
alter_seq'
,
'create_seq'
,
'create_table'
,
'create_foreign_key'
,
'alter_table'
);
$this
->
assertEquals
(
$expected
,
$sql
);
}
...
...
@@ -38,6 +38,10 @@ class SchemaDiffTest extends \PHPUnit_Framework_TestCase
public
function
createPlatform
(
$unsafe
=
false
)
{
$platform
=
$this
->
getMock
(
'Doctrine\Tests\DBAL\Mocks\MockPlatform'
);
$platform
->
expects
(
$this
->
exactly
(
1
))
->
method
(
'getCreateSchemaSQL'
)
->
with
(
'foo_ns'
)
->
will
(
$this
->
returnValue
(
'create_schema'
));
if
(
$unsafe
)
{
$platform
->
expects
(
$this
->
exactly
(
1
))
->
method
(
'getDropSequenceSql'
)
...
...
@@ -76,6 +80,9 @@ class SchemaDiffTest extends \PHPUnit_Framework_TestCase
->
with
(
$this
->
isInstanceof
(
'Doctrine\DBAL\Schema\ForeignKeyConstraint'
),
$this
->
equalTo
(
'local_table'
))
->
will
(
$this
->
returnValue
(
'drop_orphan_fk'
));
}
$platform
->
expects
(
$this
->
exactly
(
1
))
->
method
(
'supportsSchemas'
)
->
will
(
$this
->
returnValue
(
true
));
$platform
->
expects
(
$this
->
exactly
(
1
))
->
method
(
'supportsSequences'
)
->
will
(
$this
->
returnValue
(
true
));
...
...
@@ -88,6 +95,8 @@ class SchemaDiffTest extends \PHPUnit_Framework_TestCase
public
function
createSchemaDiff
()
{
$diff
=
new
SchemaDiff
();
$diff
->
newNamespaces
[
'foo_ns'
]
=
'foo_ns'
;
$diff
->
removedNamespaces
[
'bar_ns'
]
=
'bar_ns'
;
$diff
->
changedSequences
[
'foo_seq'
]
=
new
Sequence
(
'foo_seq'
);
$diff
->
newSequences
[
'bar_seq'
]
=
new
Sequence
(
'bar_seq'
);
$diff
->
removedSequences
[
'baz_seq'
]
=
new
Sequence
(
'baz_seq'
);
...
...
tests/Doctrine/Tests/DBAL/Schema/SchemaTest.php
View file @
1c9fe8df
...
...
@@ -245,4 +245,103 @@ class SchemaTest extends \PHPUnit_Framework_TestCase
$this
->
assertTrue
(
$schema
->
hasNamespace
(
'tab'
));
}
/**
* @group DBAL-669
*/
public
function
testCreatesNamespace
()
{
$schema
=
new
Schema
();
$this
->
assertFalse
(
$schema
->
hasNamespace
(
'foo'
));
$schema
->
createNamespace
(
'foo'
);
$this
->
assertTrue
(
$schema
->
hasNamespace
(
'foo'
));
$this
->
assertTrue
(
$schema
->
hasNamespace
(
'FOO'
));
$this
->
assertTrue
(
$schema
->
hasNamespace
(
'`foo`'
));
$this
->
assertTrue
(
$schema
->
hasNamespace
(
'`FOO`'
));
$schema
->
createNamespace
(
'`bar`'
);
$this
->
assertTrue
(
$schema
->
hasNamespace
(
'bar'
));
$this
->
assertTrue
(
$schema
->
hasNamespace
(
'BAR'
));
$this
->
assertTrue
(
$schema
->
hasNamespace
(
'`bar`'
));
$this
->
assertTrue
(
$schema
->
hasNamespace
(
'`BAR`'
));
$this
->
assertSame
(
array
(
'foo'
=>
'foo'
,
'bar'
=>
'`bar`'
),
$schema
->
getNamespaces
());
}
/**
* @group DBAL-669
*
* @expectedException \Doctrine\DBAL\Schema\SchemaException
*/
public
function
testThrowsExceptionOnCreatingNamespaceTwice
()
{
$schema
=
new
Schema
();
$schema
->
createNamespace
(
'foo'
);
$schema
->
createNamespace
(
'foo'
);
}
/**
* @group DBAL-669
*/
public
function
testCreatesNamespaceThroughAddingTableImplicitly
()
{
$schema
=
new
Schema
();
$this
->
assertFalse
(
$schema
->
hasNamespace
(
'foo'
));
$schema
->
createTable
(
'baz'
);
$this
->
assertFalse
(
$schema
->
hasNamespace
(
'foo'
));
$this
->
assertFalse
(
$schema
->
hasNamespace
(
'baz'
));
$schema
->
createTable
(
'foo.bar'
);
$this
->
assertTrue
(
$schema
->
hasNamespace
(
'foo'
));
$this
->
assertFalse
(
$schema
->
hasNamespace
(
'bar'
));
$schema
->
createTable
(
'`baz`.bloo'
);
$this
->
assertTrue
(
$schema
->
hasNamespace
(
'baz'
));
$this
->
assertFalse
(
$schema
->
hasNamespace
(
'bloo'
));
$schema
->
createTable
(
'`baz`.moo'
);
$this
->
assertTrue
(
$schema
->
hasNamespace
(
'baz'
));
$this
->
assertFalse
(
$schema
->
hasNamespace
(
'moo'
));
}
/**
* @group DBAL-669
*/
public
function
testCreatesNamespaceThroughAddingSequenceImplicitly
()
{
$schema
=
new
Schema
();
$this
->
assertFalse
(
$schema
->
hasNamespace
(
'foo'
));
$schema
->
createSequence
(
'baz'
);
$this
->
assertFalse
(
$schema
->
hasNamespace
(
'foo'
));
$this
->
assertFalse
(
$schema
->
hasNamespace
(
'baz'
));
$schema
->
createSequence
(
'foo.bar'
);
$this
->
assertTrue
(
$schema
->
hasNamespace
(
'foo'
));
$this
->
assertFalse
(
$schema
->
hasNamespace
(
'bar'
));
$schema
->
createSequence
(
'`baz`.bloo'
);
$this
->
assertTrue
(
$schema
->
hasNamespace
(
'baz'
));
$this
->
assertFalse
(
$schema
->
hasNamespace
(
'bloo'
));
$schema
->
createSequence
(
'`baz`.moo'
);
$this
->
assertTrue
(
$schema
->
hasNamespace
(
'baz'
));
$this
->
assertFalse
(
$schema
->
hasNamespace
(
'moo'
));
}
}
tests/Doctrine/Tests/DBAL/Schema/Visitor/CreateSchemaSqlCollectorTest.php
View file @
1c9fe8df
...
...
@@ -2,32 +2,158 @@
namespace
Doctrine\Tests\DBAL\Schema\Visitor
;
use
Doctrine\DBAL\Schema\Visitor\CreateSchemaSqlCollector
;
use
\
Doctrine\DBAL\Schema\Visitor\CreateSchemaSqlCollector
;
class
CreateSchemaSqlCollectorTest
extends
\PHPUnit_Framework_TestCase
{
public
function
testGetQueriesForPsql
()
/**
* @var \Doctrine\DBAL\Platforms\AbstractPlatform|\PHPUnit_Framework_MockObject_MockObject
*/
private
$platformMock
;
/**
* @var \Doctrine\DBAL\Schema\Visitor\CreateSchemaSqlCollector
*/
private
$visitor
;
/**
* {@inheritdoc}
*/
protected
function
setUp
()
{
parent
::
setUp
();
$this
->
platformMock
=
$this
->
getMockBuilder
(
'Doctrine\DBAL\Platforms\AbstractPlatform'
)
->
setMethods
(
array
(
'getCreateForeignKeySQL'
,
'getCreateSchemaSQL'
,
'getCreateSequenceSQL'
,
'getCreateTableSQL'
,
'supportsForeignKeyConstraints'
,
'supportsSchemas'
)
)
->
getMockForAbstractClass
();
$this
->
visitor
=
new
CreateSchemaSqlCollector
(
$this
->
platformMock
);
foreach
(
array
(
'getCreateSchemaSQL'
,
'getCreateTableSQL'
,
'getCreateForeignKeySQL'
,
'getCreateSequenceSQL'
)
as
$method
)
{
$this
->
platformMock
->
expects
(
$this
->
any
())
->
method
(
$method
)
->
will
(
$this
->
returnValue
(
'foo'
));
}
}
public
function
testAcceptsNamespace
()
{
$this
->
platformMock
->
expects
(
$this
->
at
(
0
))
->
method
(
'supportsSchemas'
)
->
will
(
$this
->
returnValue
(
false
));
$this
->
platformMock
->
expects
(
$this
->
at
(
1
))
->
method
(
'supportsSchemas'
)
->
will
(
$this
->
returnValue
(
true
));
$this
->
visitor
->
acceptNamespace
(
'foo'
);
$this
->
assertEmpty
(
$this
->
visitor
->
getQueries
());
$this
->
visitor
->
acceptNamespace
(
'foo'
);
$this
->
assertSame
(
array
(
'foo'
),
$this
->
visitor
->
getQueries
());
}
public
function
testAcceptsTable
()
{
$table
=
$this
->
createTableMock
();
$this
->
visitor
->
acceptTable
(
$table
);
$this
->
assertSame
(
array
(
'foo'
),
$this
->
visitor
->
getQueries
());
}
public
function
testAcceptsForeignKey
()
{
$this
->
platformMock
->
expects
(
$this
->
at
(
0
))
->
method
(
'supportsForeignKeyConstraints'
)
->
will
(
$this
->
returnValue
(
false
));
$this
->
platformMock
->
expects
(
$this
->
at
(
1
))
->
method
(
'supportsForeignKeyConstraints'
)
->
will
(
$this
->
returnValue
(
true
));
$table
=
$this
->
createTableMock
();
$foreignKey
=
$this
->
createForeignKeyConstraintMock
();
$this
->
visitor
->
acceptForeignKey
(
$table
,
$foreignKey
);
$this
->
assertEmpty
(
$this
->
visitor
->
getQueries
());
$this
->
visitor
->
acceptForeignKey
(
$table
,
$foreignKey
);
$this
->
assertSame
(
array
(
'foo'
),
$this
->
visitor
->
getQueries
());
}
public
function
testAcceptsSequences
()
{
$sequence
=
$this
->
createSequenceMock
();
$this
->
visitor
->
acceptSequence
(
$sequence
);
$this
->
assertSame
(
array
(
'foo'
),
$this
->
visitor
->
getQueries
());
}
public
function
testResetsQueries
()
{
$platformMock
=
$this
->
getMock
(
'Doctrine\DBAL\Platforms\PostgreSqlPlatform'
,
array
(
'supportsSchemas'
,
'getCreateTableSQL'
)
);
foreach
(
array
(
'supportsSchemas'
,
'supportsForeignKeys'
)
as
$method
)
{
$this
->
platformMock
->
expects
(
$this
->
any
())
->
method
(
$method
)
->
will
(
$this
->
returnValue
(
true
));
}
$table
=
$this
->
createTableMock
();
$foreignKey
=
$this
->
createForeignKeyConstraintMock
();
$sequence
=
$this
->
createSequenceMock
();
$this
->
visitor
->
acceptNamespace
(
'foo'
);
$this
->
visitor
->
acceptTable
(
$table
);
$this
->
visitor
->
acceptForeignKey
(
$table
,
$foreignKey
);
$this
->
visitor
->
acceptSequence
(
$sequence
);
$platformMock
->
expects
(
$this
->
any
())
->
method
(
'supportsSchemas'
)
->
will
(
$this
->
returnValue
(
true
));
$this
->
assertNotEmpty
(
$this
->
visitor
->
getQueries
());
$platformMock
->
expects
(
$this
->
any
())
->
method
(
'getCreateTableSQL'
)
->
will
(
$this
->
returnValue
(
array
(
'foo'
)));
$this
->
visitor
->
resetQueries
();
$tableMock
=
$this
->
getMockBuilder
(
'\Doctrine\DBAL\Schema\Table'
)
->
disableOriginalConstructor
()
->
getMock
();
$this
->
assertEmpty
(
$this
->
visitor
->
getQueries
());
}
$sqlCollector
=
new
CreateSchemaSqlCollector
(
$platformMock
);
$sqlCollector
->
acceptTable
(
$tableMock
);
$sql
=
$sqlCollector
->
getQueries
();
$this
->
assertEquals
(
'CREATE SCHEMA public'
,
$sql
[
0
]);
/**
* @return \Doctrine\DBAL\Schema\ForeignKeyConstraint|\PHPUnit_Framework_MockObject_MockObject
*/
private
function
createForeignKeyConstraintMock
()
{
return
$this
->
getMockBuilder
(
'Doctrine\DBAL\Schema\ForeignKeyConstraint'
)
->
disableOriginalConstructor
()
->
getMock
();
}
/**
* @return \Doctrine\DBAL\Schema\Sequence|\PHPUnit_Framework_MockObject_MockObject
*/
private
function
createSequenceMock
()
{
return
$this
->
getMockBuilder
(
'Doctrine\DBAL\Schema\Sequence'
)
->
disableOriginalConstructor
()
->
getMock
();
}
/**
* @return \Doctrine\DBAL\Schema\Table|\PHPUnit_Framework_MockObject_MockObject
*/
private
function
createTableMock
()
{
return
$this
->
getMockBuilder
(
'Doctrine\DBAL\Schema\Table'
)
->
disableOriginalConstructor
()
->
getMock
();
}
}
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