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 d46e2070
authored
Jun 16, 2018
by
Matt
Committed by
Sushant
Jun 16, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: sync with alter:true doesn't use field name (#9529)
1 parent
4c7b103d
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
36 additions
and
10 deletions
lib/model.js
test/integration/model/sync.test.js
lib/model.js
View file @
d46e207
...
@@ -1220,6 +1220,7 @@ class Model {
...
@@ -1220,6 +1220,7 @@ class Model {
options
.
hooks
=
options
.
hooks
===
undefined
?
true
:
!!
options
.
hooks
;
options
.
hooks
=
options
.
hooks
===
undefined
?
true
:
!!
options
.
hooks
;
const
attributes
=
this
.
tableAttributes
;
const
attributes
=
this
.
tableAttributes
;
const
rawAttributes
=
this
.
fieldRawAttributesMap
;
return
Promise
.
try
(()
=>
{
return
Promise
.
try
(()
=>
{
if
(
options
.
hooks
)
{
if
(
options
.
hooks
)
{
...
@@ -1247,17 +1248,17 @@ class Model {
...
@@ -1247,17 +1248,17 @@ class Model {
_
.
each
(
attributes
,
(
columnDesc
,
columnName
)
=>
{
_
.
each
(
attributes
,
(
columnDesc
,
columnName
)
=>
{
if
(
!
columns
[
columnName
])
{
if
(
!
columns
[
columnName
])
{
changes
.
push
(()
=>
this
.
QueryInterface
.
addColumn
(
this
.
getTableName
(
options
),
columnName
,
attributes
[
columnName
]));
changes
.
push
(()
=>
this
.
QueryInterface
.
addColumn
(
this
.
getTableName
(
options
),
attributes
[
columnName
].
field
||
columnName
,
attributes
[
columnName
]));
}
}
});
});
_
.
each
(
columns
,
(
columnDesc
,
columnName
)
=>
{
_
.
each
(
columns
,
(
columnDesc
,
columnName
)
=>
{
const
currentAttribute
s
=
a
ttributes
[
columnName
];
const
currentAttribute
=
rawA
ttributes
[
columnName
];
if
(
!
currentAttribute
s
)
{
if
(
!
currentAttribute
)
{
changes
.
push
(()
=>
this
.
QueryInterface
.
removeColumn
(
this
.
getTableName
(
options
),
columnName
,
options
));
changes
.
push
(()
=>
this
.
QueryInterface
.
removeColumn
(
this
.
getTableName
(
options
),
columnName
,
options
));
}
else
if
(
!
currentAttribute
s
.
primaryKey
)
{
}
else
if
(
!
currentAttribute
.
primaryKey
)
{
// Check foreign keys. If it's a foreign key, it should remove constraint first.
// Check foreign keys. If it's a foreign key, it should remove constraint first.
const
references
=
currentAttribute
s
.
references
;
const
references
=
currentAttribute
.
references
;
if
(
currentAttribute
s
.
references
)
{
if
(
currentAttribute
.
references
)
{
const
database
=
this
.
sequelize
.
config
.
database
;
const
database
=
this
.
sequelize
.
config
.
database
;
const
schema
=
this
.
sequelize
.
config
.
schema
;
const
schema
=
this
.
sequelize
.
config
.
schema
;
// Find existed foreign keys
// Find existed foreign keys
...
@@ -1276,7 +1277,7 @@ class Model {
...
@@ -1276,7 +1277,7 @@ class Model {
}
}
});
});
}
}
changes
.
push
(()
=>
this
.
QueryInterface
.
changeColumn
(
this
.
getTableName
(
options
),
columnName
,
attributes
[
columnName
]
));
changes
.
push
(()
=>
this
.
QueryInterface
.
changeColumn
(
this
.
getTableName
(
options
),
columnName
,
currentAttribute
));
}
}
});
});
return
changes
.
reduce
((
p
,
fn
)
=>
p
.
then
(
fn
),
Promise
.
resolve
());
return
changes
.
reduce
((
p
,
fn
)
=>
p
.
then
(
fn
),
Promise
.
resolve
());
...
...
test/integration/model/sync.test.js
View file @
d46e207
...
@@ -18,7 +18,8 @@ describe(Support.getTestDialectTeaser('Model'), () => {
...
@@ -18,7 +18,8 @@ describe(Support.getTestDialectTeaser('Model'), () => {
it
(
'should remove a column if it exists in the databases schema but not the model'
,
function
()
{
it
(
'should remove a column if it exists in the databases schema but not the model'
,
function
()
{
const
User
=
this
.
sequelize
.
define
(
'testSync'
,
{
const
User
=
this
.
sequelize
.
define
(
'testSync'
,
{
name
:
Sequelize
.
STRING
,
name
:
Sequelize
.
STRING
,
age
:
Sequelize
.
INTEGER
age
:
Sequelize
.
INTEGER
,
badgeNumber
:
{
type
:
Sequelize
.
INTEGER
,
field
:
'badge_number'
}
});
});
return
this
.
sequelize
.
sync
()
return
this
.
sequelize
.
sync
()
.
then
(()
=>
{
.
then
(()
=>
{
...
@@ -30,6 +31,8 @@ describe(Support.getTestDialectTeaser('Model'), () => {
...
@@ -30,6 +31,8 @@ describe(Support.getTestDialectTeaser('Model'), () => {
.
then
(()
=>
User
.
describe
())
.
then
(()
=>
User
.
describe
())
.
then
(
data
=>
{
.
then
(
data
=>
{
expect
(
data
).
to
.
not
.
have
.
ownProperty
(
'age'
);
expect
(
data
).
to
.
not
.
have
.
ownProperty
(
'age'
);
expect
(
data
).
to
.
not
.
have
.
ownProperty
(
'badge_number'
);
expect
(
data
).
to
.
not
.
have
.
ownProperty
(
'badgeNumber'
);
expect
(
data
).
to
.
have
.
ownProperty
(
'name'
);
expect
(
data
).
to
.
have
.
ownProperty
(
'name'
);
});
});
});
});
...
@@ -41,11 +44,33 @@ describe(Support.getTestDialectTeaser('Model'), () => {
...
@@ -41,11 +44,33 @@ describe(Support.getTestDialectTeaser('Model'), () => {
return
this
.
sequelize
.
sync
()
return
this
.
sequelize
.
sync
()
.
then
(()
=>
this
.
sequelize
.
define
(
'testSync'
,
{
.
then
(()
=>
this
.
sequelize
.
define
(
'testSync'
,
{
name
:
Sequelize
.
STRING
,
name
:
Sequelize
.
STRING
,
age
:
Sequelize
.
INTEGER
age
:
Sequelize
.
INTEGER
,
height
:
{
type
:
Sequelize
.
INTEGER
,
field
:
'height_cm'
}
}))
}))
.
then
(()
=>
this
.
sequelize
.
sync
({
alter
:
true
}))
.
then
(()
=>
this
.
sequelize
.
sync
({
alter
:
true
}))
.
then
(()
=>
testSync
.
describe
())
.
then
(()
=>
testSync
.
describe
())
.
then
(
data
=>
expect
(
data
).
to
.
have
.
ownProperty
(
'age'
));
.
then
(
data
=>
{
expect
(
data
).
to
.
have
.
ownProperty
(
'age'
);
expect
(
data
).
to
.
have
.
ownProperty
(
'height_cm'
);
expect
(
data
).
not
.
to
.
have
.
ownProperty
(
'height'
);
});
});
it
(
'should alter a column using the correct column name (#9515)'
,
function
()
{
const
testSync
=
this
.
sequelize
.
define
(
'testSync'
,
{
name
:
Sequelize
.
STRING
});
return
this
.
sequelize
.
sync
()
.
then
(()
=>
this
.
sequelize
.
define
(
'testSync'
,
{
name
:
Sequelize
.
STRING
,
badgeNumber
:
{
type
:
Sequelize
.
INTEGER
,
field
:
'badge_number'
}
}))
.
then
(()
=>
this
.
sequelize
.
sync
({
alter
:
true
}))
.
then
(()
=>
testSync
.
describe
())
.
then
(
data
=>
{
expect
(
data
).
to
.
have
.
ownProperty
(
'badge_number'
);
expect
(
data
).
not
.
to
.
have
.
ownProperty
(
'badgeNumber'
);
});
});
});
it
(
'should change a column if it exists in the model but is different in the database'
,
function
()
{
it
(
'should change a column if it exists in the model but is different in the database'
,
function
()
{
...
...
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