Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
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
PSDI Army
yii2
Commits
ab1f32ef
Commit
ab1f32ef
authored
Nov 16, 2014
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixes #4492: Support PostgreSQL-specific syntax for `QueryBuilder::alterColumn()`
parent
865a94f3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
29 additions
and
3 deletions
+29
-3
CHANGELOG.md
framework/CHANGELOG.md
+1
-0
QueryBuilder.php
framework/db/pgsql/QueryBuilder.php
+7
-3
PostgreSQLQueryBuilderTest.php
tests/unit/framework/db/pgsql/PostgreSQLQueryBuilderTest.php
+21
-0
No files found.
framework/CHANGELOG.md
View file @
ab1f32ef
...
...
@@ -32,6 +32,7 @@ Yii Framework 2 Change Log
-
Enh #4181: Added
`yii\bootstrap\Modal::$headerOptions`
and
`yii\bootstrap\Modal::$footerOptions`
(tuxoff, samdark)
-
Enh #4263: Added migration and SQL schema files for
`yii\log\DbTarget`
(samdark)
-
Enh #4457: Added support for using noscript for css files registered through asset bundles and Html helper (samdark)
-
Enh #4492: Support PostgreSQL-specific syntax for
`QueryBuilder::alterColumn()`
(qiangxue)
-
Enh #4739: Better display of exceptions when the response format is set as "raw" format (qiangxue)
-
Enh #5223: Query builder now supports selecting sub-queries as columns (qiangxue)
-
Enh #5367: Added
`yii\grid\DataColumn::encodeLabel`
(SDKiller)
...
...
framework/db/pgsql/QueryBuilder.php
View file @
ab1f32ef
...
...
@@ -150,14 +150,18 @@ class QueryBuilder extends \yii\db\QueryBuilder
* @param string $type the new column type. The [[getColumnType()]] method will be invoked to convert abstract
* column type (if any) into the physical one. Anything that is not recognized as abstract type will be kept
* in the generated SQL. For example, 'string' will be turned into 'varchar(255)', while 'string not null'
* will become 'varchar(255) not null'.
* will become 'varchar(255) not null'.
You can also use PostgreSQL-specific syntax such as `SET NOT NULL`.
* @return string the SQL statement for changing the definition of a column.
*/
public
function
alterColumn
(
$table
,
$column
,
$type
)
{
// https://github.com/yiisoft/yii2/issues/4492
// http://www.postgresql.org/docs/9.1/static/sql-altertable.html
if
(
!
preg_match
(
'/^(DROP\s+DEFAULT|SET|RESET)\s+/i'
,
$type
))
{
$type
=
'TYPE '
.
$this
->
getColumnType
(
$type
);
}
return
'ALTER TABLE '
.
$this
->
db
->
quoteTableName
(
$table
)
.
' ALTER COLUMN '
.
$this
->
db
->
quoteColumnName
(
$column
)
.
' TYPE '
.
$this
->
getColumnType
(
$type
);
.
$this
->
db
->
quoteColumnName
(
$column
)
.
' '
.
$type
;
}
/**
...
...
tests/unit/framework/db/pgsql/PostgreSQLQueryBuilderTest.php
View file @
ab1f32ef
...
...
@@ -101,4 +101,25 @@ class PostgreSQLQueryBuilderTest extends QueryBuilderTest
[
[
'or not ilike'
,
'name'
,
[
'heyho'
,
'abc'
]],
'"name" NOT ILIKE :qp0 OR "name" NOT ILIKE :qp1'
,
[
':qp0'
=>
'%heyho%'
,
':qp1'
=>
'%abc%'
]
],
]);
}
public
function
testAlterColumn
()
{
$qb
=
$this
->
getQueryBuilder
();
$expected
=
'ALTER TABLE "foo1" ALTER COLUMN "bar" TYPE varchar(255)'
;
$sql
=
$qb
->
alterColumn
(
'foo1'
,
'bar'
,
'varchar(255)'
);
$this
->
assertEquals
(
$expected
,
$sql
);
$expected
=
'ALTER TABLE "foo1" ALTER COLUMN "bar" SET NOT null'
;
$sql
=
$qb
->
alterColumn
(
'foo1'
,
'bar'
,
'SET NOT null'
);
$this
->
assertEquals
(
$expected
,
$sql
);
$expected
=
'ALTER TABLE "foo1" ALTER COLUMN "bar" drop default'
;
$sql
=
$qb
->
alterColumn
(
'foo1'
,
'bar'
,
'drop default'
);
$this
->
assertEquals
(
$expected
,
$sql
);
$expected
=
'ALTER TABLE "foo1" ALTER COLUMN "bar" reset xyz'
;
$sql
=
$qb
->
alterColumn
(
'foo1'
,
'bar'
,
'reset xyz'
);
$this
->
assertEquals
(
$expected
,
$sql
);
}
}
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