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 8086e72b
authored
Dec 06, 2014
by
Mick Hansen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix(queryInterface): migration support for renameColumn
1 parent
e913ba3f
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
49 additions
and
10 deletions
lib/dialects/abstract/query-generator.js
lib/dialects/mysql/query-generator.js
lib/dialects/postgres/query-generator.js
lib/dialects/sqlite/query-generator.js
lib/query-interface.js
test/query-interface.test.js
lib/dialects/abstract/query-generator.js
View file @
8086e72
...
...
@@ -555,7 +555,7 @@ module.exports = (function() {
table
+=
this
.
quoteIdentifier
(
param
.
tableName
);
}
else
{
if
(
param
.
schema
)
{
table
+=
param
.
schema
+
param
.
delimiter
;
table
+=
param
.
schema
+
(
param
.
delimiter
||
'.'
)
;
}
table
+=
param
.
tableName
;
...
...
lib/dialects/mysql/query-generator.js
View file @
8086e72
...
...
@@ -148,7 +148,7 @@ module.exports = (function() {
},
renameColumnQuery
:
function
(
tableName
,
attrBefore
,
attributes
)
{
var
query
=
'ALTER TABLE
`<%= tableName %>`
CHANGE <%= attributes %>;'
;
var
query
=
'ALTER TABLE
<%= tableName %>
CHANGE <%= attributes %>;'
;
var
attrString
=
[];
for
(
var
attrName
in
attributes
)
{
...
...
@@ -161,7 +161,7 @@ module.exports = (function() {
}));
}
return
Utils
.
_
.
template
(
query
)({
tableName
:
t
ableName
,
attributes
:
attrString
.
join
(
', '
)
});
return
Utils
.
_
.
template
(
query
)({
tableName
:
t
his
.
quoteTable
(
tableName
)
,
attributes
:
attrString
.
join
(
', '
)
});
},
upsertQuery
:
function
(
tableName
,
insertValues
,
updateValues
,
where
,
rawAttributes
,
options
)
{
...
...
lib/dialects/postgres/query-generator.js
View file @
8086e72
...
...
@@ -316,7 +316,7 @@ module.exports = (function() {
}
return
Utils
.
_
.
template
(
query
)({
tableName
:
this
.
quote
Identifiers
(
tableName
),
tableName
:
this
.
quote
Table
(
tableName
),
attributes
:
attrString
.
join
(
', '
)
});
},
...
...
lib/dialects/sqlite/query-generator.js
View file @
8086e72
...
...
@@ -388,20 +388,32 @@ module.exports = (function() {
},
renameColumnQuery
:
function
(
tableName
,
attrNameBefore
,
attrNameAfter
,
attributes
)
{
var
backupTableName
,
query
;
attributes
=
this
.
attributesToSQL
(
attributes
);
var
backupTableName
=
tableName
+
"_backup"
;
var
query
=
[
if
(
typeof
tableName
===
'object'
)
{
backupTableName
=
{
tableName
:
tableName
.
tableName
+
"_backup"
,
schema
:
tableName
.
schema
};
}
else
{
backupTableName
=
tableName
+
"_backup"
;
}
query
=
[
this
.
createTableQuery
(
backupTableName
,
attributes
).
replace
(
'CREATE TABLE'
,
'CREATE TEMPORARY TABLE'
),
"INSERT INTO <%=
tableName %>_backup
SELECT <%= attributeNamesImport %> FROM <%= tableName %>;"
,
"INSERT INTO <%=
backupTableName %>
SELECT <%= attributeNamesImport %> FROM <%= tableName %>;"
,
"DROP TABLE <%= tableName %>;"
,
this
.
createTableQuery
(
tableName
,
attributes
),
"INSERT INTO <%= tableName %> SELECT <%= attributeNamesExport %> FROM <%=
tableName %>_backup
;"
,
"DROP TABLE <%=
tableName %>_backup
;"
"INSERT INTO <%= tableName %> SELECT <%= attributeNamesExport %> FROM <%=
backupTableName %>
;"
,
"DROP TABLE <%=
backupTableName %>
;"
].
join
(
""
);
return
Utils
.
_
.
template
(
query
,
{
tableName
:
tableName
,
tableName
:
this
.
quoteTable
(
tableName
),
backupTableName
:
this
.
quoteTable
(
backupTableName
),
attributeNamesImport
:
Utils
.
_
.
keys
(
attributes
).
map
(
function
(
attr
)
{
return
(
attrNameAfter
===
attr
)
?
this
.
quoteIdentifier
(
attrNameBefore
)
+
' AS '
+
this
.
quoteIdentifier
(
attr
)
:
this
.
quoteIdentifier
(
attr
);
}.
bind
(
this
)).
join
(
', '
),
...
...
lib/query-interface.js
View file @
8086e72
...
...
@@ -300,6 +300,11 @@ module.exports = (function() {
schemaDelimiter
=
options
.
schemaDelimiter
||
null
;
}
if
(
typeof
tableName
===
'object'
)
{
schema
=
tableName
.
schema
;
tableName
=
tableName
.
tableName
;
}
var
sql
=
this
.
QueryGenerator
.
describeTableQuery
(
tableName
,
schema
,
schemaDelimiter
);
return
this
.
sequelize
.
query
(
sql
,
null
,
{
raw
:
true
}).
then
(
function
(
data
)
{
...
...
test/query-interface.test.js
View file @
8086e72
...
...
@@ -195,6 +195,28 @@ describe(Support.getTestDialectTeaser("QueryInterface"), function () {
return
self
.
queryInterface
.
renameColumn
(
'_Users'
,
'username'
,
'pseudo'
)
})
})
it
(
'works with schemas'
,
function
()
{
var
self
=
this
var
Users
=
self
.
sequelize
.
define
(
'User'
,
{
username
:
DataTypes
.
STRING
},
{
tableName
:
'Users'
,
schema
:
'archive'
})
return
self
.
sequelize
.
dropAllSchemas
().
then
(
function
()
{
return
self
.
sequelize
.
createSchema
(
"archive"
);
}).
then
(
function
()
{
return
Users
.
sync
({
force
:
true
}).
then
(
function
()
{
return
self
.
queryInterface
.
renameColumn
({
schema
:
'archive'
,
tableName
:
'Users'
},
'username'
,
'pseudo'
);
});
});
});
it
(
'rename a column non-null without default value'
,
function
()
{
var
self
=
this
var
Users
=
self
.
sequelize
.
define
(
'_Users'
,
{
...
...
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