Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
public
/
sequelize
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
不要怂,就是干,撸起袖子干!
Commit 2c18e084
authored
Apr 15, 2019
by
Mirko Jotic
Committed by
Sushant
Apr 15, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(sync): allow altering comments (#10758)
1 parent
2f6e01b5
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
34 additions
and
7 deletions
lib/dialects/mssql/query-generator.js
lib/dialects/postgres/query-generator.js
lib/query-interface.js
test/integration/query-interface/changeColumn.test.js
lib/dialects/mssql/query-generator.js
View file @
2c18e08
...
@@ -265,10 +265,18 @@ class MSSQLQueryGenerator extends AbstractQueryGenerator {
...
@@ -265,10 +265,18 @@ class MSSQLQueryGenerator extends AbstractQueryGenerator {
changeColumnQuery
(
tableName
,
attributes
)
{
changeColumnQuery
(
tableName
,
attributes
)
{
const
attrString
=
[],
const
attrString
=
[],
constraintString
=
[];
constraintString
=
[];
let
commentString
=
''
;
for
(
const
attributeName
in
attributes
)
{
for
(
const
attributeName
in
attributes
)
{
const
quotedAttrName
=
this
.
quoteIdentifier
(
attributeName
);
const
quotedAttrName
=
this
.
quoteIdentifier
(
attributeName
);
const
definition
=
attributes
[
attributeName
];
let
definition
=
attributes
[
attributeName
];
if
(
definition
.
includes
(
'COMMENT '
))
{
const
commentMatch
=
definition
.
match
(
/^
(
.+
)
(
COMMENT.*
)
$/
);
const
commentText
=
commentMatch
[
2
].
replace
(
'COMMENT'
,
''
).
trim
();
commentString
+=
this
.
commentTemplate
(
commentText
,
tableName
,
attributeName
);
// remove comment related substring from dataType
definition
=
commentMatch
[
1
];
}
if
(
definition
.
includes
(
'REFERENCES'
))
{
if
(
definition
.
includes
(
'REFERENCES'
))
{
constraintString
.
push
(
`FOREIGN KEY (
${
quotedAttrName
}
)
${
definition
.
replace
(
/.+
?(?=
REFERENCES
)
/
,
''
)}
`
);
constraintString
.
push
(
`FOREIGN KEY (
${
quotedAttrName
}
)
${
definition
.
replace
(
/.+
?(?=
REFERENCES
)
/
,
''
)}
`
);
}
else
{
}
else
{
...
@@ -285,7 +293,7 @@ class MSSQLQueryGenerator extends AbstractQueryGenerator {
...
@@ -285,7 +293,7 @@ class MSSQLQueryGenerator extends AbstractQueryGenerator {
finalQuery
+=
`ADD
${
constraintString
.
join
(
', '
)}
`
;
finalQuery
+=
`ADD
${
constraintString
.
join
(
', '
)}
`
;
}
}
return
`ALTER TABLE
${
this
.
quoteTable
(
tableName
)}
${
finalQuery
}
;`
;
return
`ALTER TABLE
${
this
.
quoteTable
(
tableName
)}
${
finalQuery
}
;
${
commentString
}
`
;
}
}
renameColumnQuery
(
tableName
,
attrBefore
,
attributes
)
{
renameColumnQuery
(
tableName
,
attrBefore
,
attributes
)
{
...
...
lib/dialects/postgres/query-generator.js
View file @
2c18e08
...
@@ -268,7 +268,6 @@ class PostgresQueryGenerator extends AbstractQueryGenerator {
...
@@ -268,7 +268,6 @@ class PostgresQueryGenerator extends AbstractQueryGenerator {
changeColumnQuery
(
tableName
,
attributes
)
{
changeColumnQuery
(
tableName
,
attributes
)
{
const
query
=
subQuery
=>
`ALTER TABLE
${
this
.
quoteTable
(
tableName
)}
ALTER COLUMN
${
subQuery
}
;`
;
const
query
=
subQuery
=>
`ALTER TABLE
${
this
.
quoteTable
(
tableName
)}
ALTER COLUMN
${
subQuery
}
;`
;
const
sql
=
[];
const
sql
=
[];
for
(
const
attributeName
in
attributes
)
{
for
(
const
attributeName
in
attributes
)
{
let
definition
=
this
.
dataTypeMapping
(
tableName
,
attributeName
,
attributes
[
attributeName
]);
let
definition
=
this
.
dataTypeMapping
(
tableName
,
attributeName
,
attributes
[
attributeName
]);
let
attrSql
=
''
;
let
attrSql
=
''
;
...
@@ -536,11 +535,13 @@ class PostgresQueryGenerator extends AbstractQueryGenerator {
...
@@ -536,11 +535,13 @@ class PostgresQueryGenerator extends AbstractQueryGenerator {
}
}
if
(
attribute
.
comment
&&
typeof
attribute
.
comment
===
'string'
)
{
if
(
attribute
.
comment
&&
typeof
attribute
.
comment
===
'string'
)
{
if
(
options
&&
options
.
context
===
'addColumn'
)
{
if
(
options
&&
(
options
.
context
===
'addColumn'
||
options
.
context
===
'changeColumn'
)
)
{
const
quotedAttr
=
this
.
quoteIdentifier
(
options
.
key
);
const
quotedAttr
=
this
.
quoteIdentifier
(
options
.
key
);
const
escapedCommentText
=
this
.
escape
(
attribute
.
comment
);
const
escapedCommentText
=
this
.
escape
(
attribute
.
comment
);
sql
+=
`; COMMENT ON COLUMN
${
this
.
quoteTable
(
options
.
table
)}
.
${
quotedAttr
}
IS
${
escapedCommentText
}
`
;
sql
+=
`; COMMENT ON COLUMN
${
this
.
quoteTable
(
options
.
table
)}
.
${
quotedAttr
}
IS
${
escapedCommentText
}
`
;
}
else
{
}
else
{
// for createTable event which does it's own parsing
// TODO: centralize creation of comment statements here
sql
+=
` COMMENT
${
attribute
.
comment
}
`
;
sql
+=
` COMMENT
${
attribute
.
comment
}
`
;
}
}
}
}
...
@@ -575,7 +576,7 @@ class PostgresQueryGenerator extends AbstractQueryGenerator {
...
@@ -575,7 +576,7 @@ class PostgresQueryGenerator extends AbstractQueryGenerator {
for
(
const
key
in
attributes
)
{
for
(
const
key
in
attributes
)
{
const
attribute
=
attributes
[
key
];
const
attribute
=
attributes
[
key
];
result
[
attribute
.
field
||
key
]
=
this
.
attributeToSQL
(
attribute
,
options
);
result
[
attribute
.
field
||
key
]
=
this
.
attributeToSQL
(
attribute
,
Object
.
assign
({
key
},
options
||
{})
);
}
}
return
result
;
return
result
;
...
...
lib/query-interface.js
View file @
2c18e08
...
@@ -230,7 +230,7 @@ class QueryInterface {
...
@@ -230,7 +230,7 @@ class QueryInterface {
});
});
}
}
attributes
=
this
.
QueryGenerator
.
attributesToSQL
(
attributes
,
{
context
:
'createTable'
});
attributes
=
this
.
QueryGenerator
.
attributesToSQL
(
attributes
,
{
table
:
tableName
,
context
:
'createTable'
});
sql
=
this
.
QueryGenerator
.
createTableQuery
(
tableName
,
attributes
,
options
);
sql
=
this
.
QueryGenerator
.
createTableQuery
(
tableName
,
attributes
,
options
);
return
promise
.
then
(()
=>
this
.
sequelize
.
query
(
sql
,
options
));
return
promise
.
then
(()
=>
this
.
sequelize
.
query
(
sql
,
options
));
...
@@ -567,7 +567,10 @@ class QueryInterface {
...
@@ -567,7 +567,10 @@ class QueryInterface {
// sqlite needs some special treatment as it cannot change a column
// sqlite needs some special treatment as it cannot change a column
return
SQLiteQueryInterface
.
changeColumn
(
this
,
tableName
,
attributes
,
options
);
return
SQLiteQueryInterface
.
changeColumn
(
this
,
tableName
,
attributes
,
options
);
}
}
const
query
=
this
.
QueryGenerator
.
attributesToSQL
(
attributes
);
const
query
=
this
.
QueryGenerator
.
attributesToSQL
(
attributes
,
{
context
:
'changeColumn'
,
table
:
tableName
});
const
sql
=
this
.
QueryGenerator
.
changeColumnQuery
(
tableName
,
query
);
const
sql
=
this
.
QueryGenerator
.
changeColumnQuery
(
tableName
,
query
);
return
this
.
sequelize
.
query
(
sql
,
options
);
return
this
.
sequelize
.
query
(
sql
,
options
);
...
...
test/integration/query-interface/changeColumn.test.js
View file @
2c18e08
...
@@ -220,6 +220,21 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => {
...
@@ -220,6 +220,21 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => {
});
});
});
});
it
(
'should change the comment of column'
,
function
()
{
return
this
.
queryInterface
.
describeTable
({
tableName
:
'users'
}).
then
(
describedTable
=>
{
expect
(
describedTable
.
level_id
.
comment
).
to
.
be
.
equal
(
null
);
return
this
.
queryInterface
.
changeColumn
(
'users'
,
'level_id'
,
{
type
:
DataTypes
.
INTEGER
,
comment
:
'FooBar'
});
}).
then
(()
=>
{
return
this
.
queryInterface
.
describeTable
({
tableName
:
'users'
});
}).
then
(
describedTable2
=>
{
expect
(
describedTable2
.
level_id
.
comment
).
to
.
be
.
equal
(
'FooBar'
);
});
});
});
});
}
}
});
});
...
...
Write
Preview
Markdown
is supported
Attach a file
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 post a comment