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 59b218d0
authored
Dec 06, 2014
by
Mick Hansen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix(queryInterface): schema support for changeColumn
1 parent
8086e72b
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
83 additions
and
24 deletions
lib/dialects/mysql/query-generator.js
lib/dialects/postgres/query-generator.js
lib/dialects/sqlite/query-generator.js
test/query-interface.test.js
lib/dialects/mysql/query-generator.js
View file @
59b218d
...
@@ -132,7 +132,7 @@ module.exports = (function() {
...
@@ -132,7 +132,7 @@ module.exports = (function() {
},
},
changeColumnQuery
:
function
(
tableName
,
attributes
)
{
changeColumnQuery
:
function
(
tableName
,
attributes
)
{
var
query
=
'ALTER TABLE
`<%= tableName %>`
CHANGE <%= attributes %>;'
;
var
query
=
'ALTER TABLE
<%= tableName %>
CHANGE <%= attributes %>;'
;
var
attrString
=
[];
var
attrString
=
[];
for
(
var
attrName
in
attributes
)
{
for
(
var
attrName
in
attributes
)
{
...
@@ -144,7 +144,7 @@ module.exports = (function() {
...
@@ -144,7 +144,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
(
', '
)
});
},
},
renameColumnQuery
:
function
(
tableName
,
attrBefore
,
attributes
)
{
renameColumnQuery
:
function
(
tableName
,
attrBefore
,
attributes
)
{
...
...
lib/dialects/postgres/query-generator.js
View file @
59b218d
...
@@ -250,30 +250,30 @@ module.exports = (function() {
...
@@ -250,30 +250,30 @@ module.exports = (function() {
var
definition
=
this
.
pgDataTypeMapping
(
tableName
,
attributeName
,
attributes
[
attributeName
]);
var
definition
=
this
.
pgDataTypeMapping
(
tableName
,
attributeName
,
attributes
[
attributeName
]);
var
attrSql
=
''
;
var
attrSql
=
''
;
if
(
definition
.
indexOf
(
'NOT NULL'
)
>
0
)
{
/*
if (definition.indexOf('NOT NULL') > 0) {
attrSql += Utils._.template(query)({
attrSql += Utils._.template(query)({
tableName
:
this
.
quote
Identifiers
(
tableName
),
tableName: this.quote
Table
(tableName),
query: this.quoteIdentifier(attributeName) + ' SET NOT NULL'
query: this.quoteIdentifier(attributeName) + ' SET NOT NULL'
});
});
definition = definition.replace('NOT NULL', '').trim();
definition = definition.replace('NOT NULL', '').trim();
} else {
} else {
attrSql += Utils._.template(query)({
attrSql += Utils._.template(query)({
tableName
:
this
.
quote
Identifiers
(
tableName
),
tableName: this.quote
Table
(tableName),
query: this.quoteIdentifier(attributeName) + ' DROP NOT NULL'
query: this.quoteIdentifier(attributeName) + ' DROP NOT NULL'
});
});
}
}
if (definition.indexOf('DEFAULT') > 0) {
if (definition.indexOf('DEFAULT') > 0) {
attrSql += Utils._.template(query)({
attrSql += Utils._.template(query)({
tableName
:
this
.
quote
Identifiers
(
tableName
),
tableName: this.quote
Table
(tableName),
query: this.quoteIdentifier(attributeName) + ' SET DEFAULT ' + definition.match(/DEFAULT ([^;]+)/)[1]
query: this.quoteIdentifier(attributeName) + ' SET DEFAULT ' + definition.match(/DEFAULT ([^;]+)/)[1]
});
});
definition = definition.replace(/(DEFAULT[^;]+)/, '').trim();
definition = definition.replace(/(DEFAULT[^;]+)/, '').trim();
} else {
} else {
attrSql += Utils._.template(query)({
attrSql += Utils._.template(query)({
tableName
:
this
.
quote
Identifiers
(
tableName
),
tableName: this.quote
Table
(tableName),
query: this.quoteIdentifier(attributeName) + ' DROP DEFAULT'
query: this.quoteIdentifier(attributeName) + ' DROP DEFAULT'
});
});
}
}
...
@@ -288,13 +288,13 @@ module.exports = (function() {
...
@@ -288,13 +288,13 @@ module.exports = (function() {
definition = definition.replace(/UNIQUE;*$/, '');
definition = definition.replace(/UNIQUE;*$/, '');
attrSql += Utils._.template(query.replace('ALTER COLUMN', ''))({
attrSql += Utils._.template(query.replace('ALTER COLUMN', ''))({
tableName
:
this
.
quote
Identifiers
(
tableName
),
tableName: this.quote
Table
(tableName),
query: 'ADD CONSTRAINT ' + this.quoteIdentifier(attributeName + '_unique_idx') + ' UNIQUE (' + this.quoteIdentifier(attributeName) + ')'
query: 'ADD CONSTRAINT ' + this.quoteIdentifier(attributeName + '_unique_idx') + ' UNIQUE (' + this.quoteIdentifier(attributeName) + ')'
});
});
}
}
*/
attrSql
+=
Utils
.
_
.
template
(
query
)({
attrSql
+=
Utils
.
_
.
template
(
query
)({
tableName
:
this
.
quote
Identifiers
(
tableName
),
tableName
:
this
.
quote
Table
(
tableName
),
query
:
this
.
quoteIdentifier
(
attributeName
)
+
' TYPE '
+
definition
query
:
this
.
quoteIdentifier
(
attributeName
)
+
' TYPE '
+
definition
});
});
...
...
lib/dialects/sqlite/query-generator.js
View file @
59b218d
...
@@ -369,20 +369,32 @@ module.exports = (function() {
...
@@ -369,20 +369,32 @@ module.exports = (function() {
},
},
removeColumnQuery
:
function
(
tableName
,
attributes
)
{
removeColumnQuery
:
function
(
tableName
,
attributes
)
{
var
backupTableName
,
query
;
attributes
=
this
.
attributesToSQL
(
attributes
);
attributes
=
this
.
attributesToSQL
(
attributes
);
var
backupTableName
=
tableName
+
"_backup"
;
if
(
typeof
tableName
===
'object'
)
{
var
query
=
[
backupTableName
=
{
tableName
:
tableName
.
tableName
+
"_backup"
,
schema
:
tableName
.
schema
};
}
else
{
backupTableName
=
tableName
+
"_backup"
;
}
query
=
[
this
.
createTableQuery
(
backupTableName
,
attributes
).
replace
(
'CREATE TABLE'
,
'CREATE TEMPORARY TABLE'
),
this
.
createTableQuery
(
backupTableName
,
attributes
).
replace
(
'CREATE TABLE'
,
'CREATE TEMPORARY TABLE'
),
"INSERT INTO <%=
tableName %>_backup
SELECT <%= attributeNames %> FROM <%= tableName %>;"
,
"INSERT INTO <%=
backupTableName %>
SELECT <%= attributeNames %> FROM <%= tableName %>;"
,
"DROP TABLE <%= tableName %>;"
,
"DROP TABLE <%= tableName %>;"
,
this
.
createTableQuery
(
tableName
,
attributes
),
this
.
createTableQuery
(
tableName
,
attributes
),
"INSERT INTO <%= tableName %> SELECT <%= attributeNames %> FROM <%=
tableName %>_backup
;"
,
"INSERT INTO <%= tableName %> SELECT <%= attributeNames %> FROM <%=
backupTableName %>
;"
,
"DROP TABLE <%=
tableName %>_backup
;"
"DROP TABLE <%=
backupTableName %>
;"
].
join
(
""
);
].
join
(
""
);
return
Utils
.
_
.
template
(
query
,
{
return
Utils
.
_
.
template
(
query
,
{
tableName
:
tableName
,
tableName
:
this
.
quoteTable
(
tableName
),
backupTableName
:
this
.
quoteTable
(
backupTableName
),
attributeNames
:
Utils
.
_
.
keys
(
attributes
).
join
(
', '
)
attributeNames
:
Utils
.
_
.
keys
(
attributes
).
join
(
', '
)
});
});
},
},
...
...
test/query-interface.test.js
View file @
59b218d
...
@@ -9,9 +9,9 @@ chai.config.includeStack = true
...
@@ -9,9 +9,9 @@ chai.config.includeStack = true
describe
(
Support
.
getTestDialectTeaser
(
"QueryInterface"
),
function
()
{
describe
(
Support
.
getTestDialectTeaser
(
"QueryInterface"
),
function
()
{
beforeEach
(
function
(
done
)
{
beforeEach
(
function
(
done
)
{
this
.
sequelize
.
options
.
quoteIdenifiers
=
true
this
.
sequelize
.
options
.
quoteIdenifiers
=
true
;
this
.
queryInterface
=
this
.
sequelize
.
getQueryInterface
()
this
.
queryInterface
=
this
.
sequelize
.
getQueryInterface
()
;
done
()
done
()
;
})
})
describe
(
'dropAllTables'
,
function
()
{
describe
(
'dropAllTables'
,
function
()
{
...
@@ -244,7 +244,7 @@ describe(Support.getTestDialectTeaser("QueryInterface"), function () {
...
@@ -244,7 +244,7 @@ describe(Support.getTestDialectTeaser("QueryInterface"), function () {
return
self
.
queryInterface
.
renameColumn
(
'_Users'
,
'active'
,
'enabled'
)
return
self
.
queryInterface
.
renameColumn
(
'_Users'
,
'active'
,
'enabled'
)
})
})
})
})
it
(
'renames a column primary key auto
i
ncrement column'
,
function
()
{
it
(
'renames a column primary key auto
I
ncrement column'
,
function
()
{
var
self
=
this
var
self
=
this
var
Fruits
=
self
.
sequelize
.
define
(
'Fruit'
,
{
var
Fruits
=
self
.
sequelize
.
define
(
'Fruit'
,
{
fruitId
:
{
fruitId
:
{
...
@@ -261,13 +261,40 @@ describe(Support.getTestDialectTeaser("QueryInterface"), function () {
...
@@ -261,13 +261,40 @@ describe(Support.getTestDialectTeaser("QueryInterface"), function () {
})
})
});
});
describe
(
'changeColumn'
,
function
()
{
it
(
'should support schemas'
,
function
()
{
return
this
.
sequelize
.
dropAllSchemas
().
bind
(
this
).
then
(
function
()
{
return
this
.
sequelize
.
createSchema
(
"archive"
);
}).
then
(
function
()
{
return
this
.
queryInterface
.
createTable
({
tableName
:
'users'
,
schema
:
'archive'
},
{
id
:
{
type
:
DataTypes
.
INTEGER
,
primaryKey
:
true
,
autoIncrement
:
true
},
currency
:
DataTypes
.
INTEGER
}).
bind
(
this
).
then
(
function
()
{
return
this
.
queryInterface
.
changeColumn
({
tableName
:
'users'
,
schema
:
'archive'
},
'currency'
,
{
type
:
DataTypes
.
FLOAT
});
});
});
});
});
describe
(
'addColumn'
,
function
()
{
describe
(
'addColumn'
,
function
()
{
beforeEach
(
function
()
{
beforeEach
(
function
()
{
return
this
.
queryInterface
.
createTable
(
'users'
,
{
return
this
.
queryInterface
.
createTable
(
'users'
,
{
id
:
{
id
:
{
type
:
DataTypes
.
INTEGER
,
type
:
DataTypes
.
INTEGER
,
primaryKey
:
true
,
primaryKey
:
true
,
auto
i
ncrement
:
true
auto
I
ncrement
:
true
}
}
});
});
});
});
...
@@ -277,7 +304,7 @@ describe(Support.getTestDialectTeaser("QueryInterface"), function () {
...
@@ -277,7 +304,7 @@ describe(Support.getTestDialectTeaser("QueryInterface"), function () {
id
:
{
id
:
{
type
:
DataTypes
.
INTEGER
,
type
:
DataTypes
.
INTEGER
,
primaryKey
:
true
,
primaryKey
:
true
,
auto
i
ncrement
:
true
auto
I
ncrement
:
true
}
}
}).
bind
(
this
).
then
(
function
()
{
}).
bind
(
this
).
then
(
function
()
{
return
this
.
queryInterface
.
addColumn
(
'users'
,
'level_id'
,
{
return
this
.
queryInterface
.
addColumn
(
'users'
,
'level_id'
,
{
...
@@ -290,6 +317,26 @@ describe(Support.getTestDialectTeaser("QueryInterface"), function () {
...
@@ -290,6 +317,26 @@ describe(Support.getTestDialectTeaser("QueryInterface"), function () {
});
});
});
});
it
(
'should work with schemas'
,
function
()
{
return
this
.
queryInterface
.
createTable
({
tableName
:
'users'
,
schema
:
'archive'
},
{
id
:
{
type
:
DataTypes
.
INTEGER
,
primaryKey
:
true
,
autoIncrement
:
true
}
}).
bind
(
this
).
then
(
function
()
{
return
this
.
queryInterface
.
addColumn
({
tableName
:
'users'
,
schema
:
'archive'
},
'level_id'
,
{
type
:
DataTypes
.
INTEGER
});
});
});
it
(
'should work with enums (1)'
,
function
()
{
it
(
'should work with enums (1)'
,
function
()
{
return
this
.
queryInterface
.
addColumn
(
'users'
,
'someEnum'
,
DataTypes
.
ENUM
(
'value1'
,
'value2'
,
'value3'
));
return
this
.
queryInterface
.
addColumn
(
'users'
,
'someEnum'
,
DataTypes
.
ENUM
(
'value1'
,
'value2'
,
'value3'
));
});
});
...
@@ -308,14 +355,14 @@ describe(Support.getTestDialectTeaser("QueryInterface"), function () {
...
@@ -308,14 +355,14 @@ describe(Support.getTestDialectTeaser("QueryInterface"), function () {
id
:
{
id
:
{
type
:
DataTypes
.
INTEGER
,
type
:
DataTypes
.
INTEGER
,
primaryKey
:
true
,
primaryKey
:
true
,
auto
i
ncrement
:
true
auto
I
ncrement
:
true
},
},
}).
bind
(
this
).
then
(
function
()
{
}).
bind
(
this
).
then
(
function
()
{
return
this
.
queryInterface
.
createTable
(
'hosts'
,
{
return
this
.
queryInterface
.
createTable
(
'hosts'
,
{
id
:
{
id
:
{
type
:
DataTypes
.
INTEGER
,
type
:
DataTypes
.
INTEGER
,
primaryKey
:
true
,
primaryKey
:
true
,
auto
i
ncrement
:
true
auto
I
ncrement
:
true
},
},
admin
:
{
admin
:
{
type
:
DataTypes
.
INTEGER
,
type
:
DataTypes
.
INTEGER
,
...
...
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