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
18a8040a
Unverified
Commit
18a8040a
authored
Oct 05, 2018
by
Sergei Morozov
Committed by
GitHub
Oct 05, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #3308 from bezhermoso/custom-asset-filter-as-callable
Implement {Configuration#getSchemaAssetsFilter}
parents
ed25dc9c
5110a192
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
176 additions
and
9 deletions
+176
-9
Configuration.php
lib/Doctrine/DBAL/Configuration.php
+37
-0
AbstractSchemaManager.php
lib/Doctrine/DBAL/Schema/AbstractSchemaManager.php
+3
-9
DB2SchemaManagerTest.php
tests/Doctrine/Tests/DBAL/Schema/DB2SchemaManagerTest.php
+136
-0
No files found.
lib/Doctrine/DBAL/Configuration.php
View file @
18a8040a
...
...
@@ -4,6 +4,8 @@ namespace Doctrine\DBAL;
use
Doctrine\Common\Cache\Cache
;
use
Doctrine\DBAL\Logging\SQLLogger
;
use
Doctrine\DBAL\Schema\AbstractAsset
;
use
function
preg_match
;
/**
* Configuration container for the Doctrine DBAL.
...
...
@@ -75,6 +77,11 @@ class Configuration
public
function
setFilterSchemaAssetsExpression
(
$filterExpression
)
{
$this
->
_attributes
[
'filterSchemaAssetsExpression'
]
=
$filterExpression
;
if
(
$filterExpression
)
{
$this
->
_attributes
[
'filterSchemaAssetsExpressionCallable'
]
=
$this
->
buildSchemaAssetsFilterFromExpression
(
$filterExpression
);
}
else
{
$this
->
_attributes
[
'filterSchemaAssetsExpressionCallable'
]
=
null
;
}
}
/**
...
...
@@ -87,6 +94,36 @@ class Configuration
return
$this
->
_attributes
[
'filterSchemaAssetsExpression'
]
??
null
;
}
/**
* @param string $filterExpression
*/
private
function
buildSchemaAssetsFilterFromExpression
(
$filterExpression
)
:
callable
{
return
static
function
(
$assetName
)
use
(
$filterExpression
)
{
if
(
$assetName
instanceof
AbstractAsset
)
{
$assetName
=
$assetName
->
getName
();
}
return
preg_match
(
$filterExpression
,
$assetName
);
};
}
/**
* Sets the callable to use to filter schema assets.
*/
public
function
setSchemaAssetsFilter
(
?
callable
$callable
=
null
)
:
?
callable
{
$this
->
_attributes
[
'filterSchemaAssetsExpression'
]
=
null
;
return
$this
->
_attributes
[
'filterSchemaAssetsExpressionCallable'
]
=
$callable
;
}
/**
* Returns the callable to use to filter schema assets.
*/
public
function
getSchemaAssetsFilter
()
:
?
callable
{
return
$this
->
_attributes
[
'filterSchemaAssetsExpressionCallable'
]
??
null
;
}
/**
* Sets the default auto-commit mode for connections.
*
...
...
lib/Doctrine/DBAL/Schema/AbstractSchemaManager.php
View file @
18a8040a
...
...
@@ -219,18 +219,12 @@ abstract class AbstractSchemaManager
*/
protected
function
filterAssetNames
(
$assetNames
)
{
$filter
Expr
=
$this
->
getFilterSchemaAssetsExpression
();
if
(
!
$filter
Expr
)
{
$filter
=
$this
->
_conn
->
getConfiguration
()
->
getSchemaAssetsFilter
();
if
(
!
$filter
)
{
return
$assetNames
;
}
return
array_values
(
array_filter
(
$assetNames
,
static
function
(
$assetName
)
use
(
$filterExpr
)
{
$assetName
=
$assetName
instanceof
AbstractAsset
?
$assetName
->
getName
()
:
$assetName
;
return
preg_match
(
$filterExpr
,
$assetName
);
})
);
return
array_values
(
array_filter
(
$assetNames
,
$filter
));
}
/**
...
...
tests/Doctrine/Tests/DBAL/Schema/DB2SchemaManagerTest.php
View file @
18a8040a
...
...
@@ -10,6 +10,7 @@ use Doctrine\DBAL\Platforms\DB2Platform;
use
Doctrine\DBAL\Schema\DB2SchemaManager
;
use
PHPUnit\Framework\TestCase
;
use
PHPUnit_Framework_MockObject_MockObject
;
use
function
in_array
;
/**
* @covers \Doctrine\DBAL\Schema\DB2SchemaManager
...
...
@@ -60,4 +61,139 @@ final class DB2SchemaManagerTest extends TestCase
$this
->
manager
->
listTableNames
()
);
}
/**
* @return void
*
* @group DBAL-2701
*/
public
function
testAssetFilteringSetsACallable
()
{
$filterExpression
=
'/^(?!T_)/'
;
$this
->
conn
->
getConfiguration
()
->
setFilterSchemaAssetsExpression
(
$filterExpression
);
$this
->
conn
->
expects
(
$this
->
once
())
->
method
(
'fetchAll'
)
->
will
(
$this
->
returnValue
([
[
'name'
=>
'FOO'
],
[
'name'
=>
'T_FOO'
],
[
'name'
=>
'BAR'
],
[
'name'
=>
'T_BAR'
],
]));
self
::
assertSame
(
[
'FOO'
,
'BAR'
,
],
$this
->
manager
->
listTableNames
()
);
$callable
=
$this
->
conn
->
getConfiguration
()
->
getSchemaAssetsFilter
();
$this
->
assertInternalType
(
'callable'
,
$callable
);
// BC check: Test that regexp expression is still preserved & accessible.
$this
->
assertEquals
(
$filterExpression
,
$this
->
conn
->
getConfiguration
()
->
getFilterSchemaAssetsExpression
());
}
/**
* @return void
*/
public
function
testListTableNamesFiltersAssetNamesCorrectlyWithCallable
()
{
$accepted
=
[
'T_FOO'
,
'T_BAR'
];
$this
->
conn
->
getConfiguration
()
->
setSchemaAssetsFilter
(
static
function
(
$assetName
)
use
(
$accepted
)
{
return
in_array
(
$assetName
,
$accepted
);
});
$this
->
conn
->
expects
(
$this
->
once
())
->
method
(
'fetchAll'
)
->
will
(
$this
->
returnValue
([
[
'name'
=>
'FOO'
],
[
'name'
=>
'T_FOO'
],
[
'name'
=>
'BAR'
],
[
'name'
=>
'T_BAR'
],
]));
self
::
assertSame
(
[
'T_FOO'
,
'T_BAR'
,
],
$this
->
manager
->
listTableNames
()
);
$this
->
assertNull
(
$this
->
conn
->
getConfiguration
()
->
getFilterSchemaAssetsExpression
());
}
/**
* @return void
*/
public
function
testSettingNullExpressionWillResetCallable
()
{
$accepted
=
[
'T_FOO'
,
'T_BAR'
];
$this
->
conn
->
getConfiguration
()
->
setSchemaAssetsFilter
(
static
function
(
$assetName
)
use
(
$accepted
)
{
return
in_array
(
$assetName
,
$accepted
);
});
$this
->
conn
->
expects
(
$this
->
atLeastOnce
())
->
method
(
'fetchAll'
)
->
will
(
$this
->
returnValue
([
[
'name'
=>
'FOO'
],
[
'name'
=>
'T_FOO'
],
[
'name'
=>
'BAR'
],
[
'name'
=>
'T_BAR'
],
]));
self
::
assertSame
(
[
'T_FOO'
,
'T_BAR'
,
],
$this
->
manager
->
listTableNames
()
);
$this
->
conn
->
getConfiguration
()
->
setFilterSchemaAssetsExpression
(
null
);
self
::
assertSame
(
[
'FOO'
,
'T_FOO'
,
'BAR'
,
'T_BAR'
,
],
$this
->
manager
->
listTableNames
()
);
$this
->
assertNull
(
$this
->
conn
->
getConfiguration
()
->
getSchemaAssetsFilter
());
}
/**
* @return void
*/
public
function
testSettingNullAsCallableClearsExpression
()
{
$filterExpression
=
'/^(?!T_)/'
;
$this
->
conn
->
getConfiguration
()
->
setFilterSchemaAssetsExpression
(
$filterExpression
);
$this
->
conn
->
expects
(
$this
->
exactly
(
2
))
->
method
(
'fetchAll'
)
->
will
(
$this
->
returnValue
([
[
'name'
=>
'FOO'
],
[
'name'
=>
'T_FOO'
],
[
'name'
=>
'BAR'
],
[
'name'
=>
'T_BAR'
],
]));
self
::
assertSame
(
[
'FOO'
,
'BAR'
,
],
$this
->
manager
->
listTableNames
()
);
$this
->
conn
->
getConfiguration
()
->
setSchemaAssetsFilter
(
null
);
self
::
assertSame
(
[
'FOO'
,
'T_FOO'
,
'BAR'
,
'T_BAR'
,
],
$this
->
manager
->
listTableNames
()
);
$this
->
assertNull
(
$this
->
conn
->
getConfiguration
()
->
getFilterSchemaAssetsExpression
());
}
}
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