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 0c1ec1cc
authored
Sep 09, 2014
by
Mick Hansen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(query-interface/migrator): provide foreign key constraint/references support to addColumn
1 parent
cb6a51cb
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
123 additions
and
90 deletions
lib/dialects/mysql/query-generator.js
lib/dialects/postgres/query-generator.js
lib/query-interface.js
test/dao-factory.test.js
test/query-interface.test.js
lib/dialects/mysql/query-generator.js
View file @
0c1ec1c
...
...
@@ -113,20 +113,19 @@ module.exports = (function() {
}
},
addColumnQuery
:
function
(
tableName
,
attributes
)
{
var
query
=
'ALTER TABLE `<%= tableName %>` ADD <%= attributes %>;'
,
attrString
=
[];
for
(
var
attrName
in
attributes
)
{
var
definition
=
attributes
[
attrName
];
attrString
.
push
(
Utils
.
_
.
template
(
'`<%= attrName %>` <%= definition %>'
)({
attrName
:
attrName
,
definition
:
this
.
mysqlDataTypeMapping
(
tableName
,
attrName
,
definition
)
}));
}
addColumnQuery
:
function
(
table
,
key
,
dataType
)
{
var
query
=
'ALTER TABLE <%= table %> ADD <%= attribute %>;'
,
attribute
=
Utils
.
_
.
template
(
'<%= key %> <%= definition %>'
)({
key
:
this
.
quoteIdentifier
(
key
),
definition
:
this
.
attributeToSQL
(
dataType
,
{
context
:
'addColumn'
})
});
return
Utils
.
_
.
template
(
query
)({
tableName
:
tableName
,
attributes
:
attrString
.
join
(
', '
)
});
return
Utils
.
_
.
template
(
query
)({
table
:
this
.
quoteTable
(
table
),
attribute
:
attribute
});
},
removeColumnQuery
:
function
(
tableName
,
attributeName
)
{
...
...
@@ -278,10 +277,6 @@ module.exports = (function() {
template
+=
' PRIMARY KEY'
;
}
if
(
attribute
.
comment
&&
Utils
.
_
.
isString
(
attribute
.
comment
)
&&
attribute
.
comment
.
length
)
{
template
+=
' COMMENT '
+
this
.
escape
(
attribute
.
comment
);
}
if
(
attribute
.
references
)
{
template
+=
' REFERENCES '
+
this
.
quoteTable
(
attribute
.
references
);
...
...
lib/dialects/postgres/query-generator.js
View file @
0c1ec1c
...
...
@@ -119,26 +119,24 @@ module.exports = (function() {
}
},
addColumnQuery
:
function
(
table
Name
,
attributes
)
{
var
query
=
'ALTER TABLE <%= table
Name %> ADD COLUMN <%= attributes
%>;'
,
attr
String
=
[]
;
addColumnQuery
:
function
(
table
,
key
,
dataType
)
{
var
query
=
'ALTER TABLE <%= table
%> ADD COLUMN <%= attribute
%>;'
,
attr
ibute
;
for
(
var
attrName
in
attributes
)
{
var
definition
=
attributes
[
attrName
];
attrString
.
push
(
Utils
.
_
.
template
(
'<%= attrName %> <%= definition %>'
)({
attrName
:
this
.
quoteIdentifier
(
attrName
),
definition
:
this
.
pgDataTypeMapping
(
tableName
,
attrName
,
definition
)
}));
if
(
definition
.
match
(
/^ENUM
\(
/
))
{
query
=
this
.
pgEnum
(
tableName
,
attrName
,
definition
)
+
query
;
}
if
(
dataType
.
toString
()
===
DataTypes
.
ENUM
.
toString
())
{
query
=
this
.
pgEnum
(
table
,
key
,
dataType
)
+
query
;
}
attribute
=
Utils
.
_
.
template
(
'<%= key %> <%= definition %>'
)({
key
:
this
.
quoteIdentifier
(
key
),
definition
:
this
.
attributeToSQL
(
dataType
,
{
context
:
'addColumn'
})
});
return
Utils
.
_
.
template
(
query
)({
table
Name
:
this
.
quoteTable
(
tableNam
e
),
attribute
s
:
attrString
.
join
(
', '
)
table
:
this
.
quoteTable
(
tabl
e
),
attribute
:
attribute
});
},
...
...
@@ -386,20 +384,19 @@ module.exports = (function() {
return
query
;
},
attribute
sToSQL
:
function
(
attribute
s
)
{
var
result
=
{};
for
(
var
name
in
attributes
)
{
var
dataType
=
attributes
[
name
]
;
var
fieldName
=
dataType
.
field
||
name
;
attribute
ToSQL
:
function
(
attribute
,
option
s
)
{
if
(
!
Utils
.
_
.
isPlainObject
(
attribute
))
{
attribute
=
{
type
:
attribute
}
;
}
if
(
Utils
.
_
.
isObject
(
dataType
))
{
var
template
=
'<%= type %>'
,
replacements
=
{
type
:
dataTyp
e
.
type
};
,
replacements
=
{
type
:
attribut
e
.
type
};
if
(
dataTyp
e
.
type
.
toString
()
===
DataTypes
.
ENUM
.
toString
())
{
if
(
Array
.
isArray
(
dataType
.
values
)
&&
(
dataTyp
e
.
values
.
length
>
0
))
{
replacements
.
type
=
'ENUM('
+
Utils
.
_
.
map
(
dataTyp
e
.
values
,
function
(
value
)
{
if
(
attribut
e
.
type
.
toString
()
===
DataTypes
.
ENUM
.
toString
())
{
if
(
Array
.
isArray
(
attribute
.
values
)
&&
(
attribut
e
.
values
.
length
>
0
))
{
replacements
.
type
=
'ENUM('
+
Utils
.
_
.
map
(
attribut
e
.
values
,
function
(
value
)
{
return
this
.
escape
(
value
);
}.
bind
(
this
)).
join
(
', '
)
+
')'
;
}
else
{
...
...
@@ -407,71 +404,68 @@ module.exports = (function() {
}
}
if
(
dataType
.
type
===
DataTypes
.
BOOLEAN
)
{
dataTyp
e
.
type
=
'BOOLEAN'
;
if
(
attribute
.
type
===
attribute
.
BOOLEAN
)
{
attribut
e
.
type
=
'BOOLEAN'
;
}
if
(
dataType
.
type
===
DataTypes
.
DATE
)
{
dataTyp
e
.
_typeName
=
'DATETIME'
;
dataTyp
e
.
type
=
'TIMESTAMP WITH TIME ZONE'
;
if
(
attribute
.
type
===
attribute
.
DATE
)
{
attribut
e
.
_typeName
=
'DATETIME'
;
attribut
e
.
type
=
'TIMESTAMP WITH TIME ZONE'
;
}
if
(
dataType
.
hasOwnProperty
(
'allowNull'
)
&&
(
!
dataTyp
e
.
allowNull
))
{
if
(
attribute
.
hasOwnProperty
(
'allowNull'
)
&&
(
!
attribut
e
.
allowNull
))
{
template
+=
' NOT NULL'
;
}
if
(
dataTyp
e
.
autoIncrement
)
{
if
(
attribut
e
.
autoIncrement
)
{
template
+=
' SERIAL'
;
}
if
(
Utils
.
defaultValueSchemable
(
dataType
.
defaultValue
))
{
// TODO thoroughly check that DataTypes.NOW will properly
// get populated on all databases as DEFAULT value
// i.e. mysql requires: DEFAULT CURRENT_TIMESTAMP
if
(
Utils
.
defaultValueSchemable
(
attribute
.
defaultValue
))
{
template
+=
' DEFAULT <%= defaultValue %>'
;
replacements
.
defaultValue
=
this
.
escape
(
dataType
.
defaultValue
,
dataTyp
e
);
replacements
.
defaultValue
=
this
.
escape
(
attribute
.
defaultValue
,
attribut
e
);
}
if
(
dataTyp
e
.
unique
===
true
)
{
if
(
attribut
e
.
unique
===
true
)
{
template
+=
' UNIQUE'
;
}
if
(
dataTyp
e
.
primaryKey
)
{
if
(
attribut
e
.
primaryKey
)
{
template
+=
' PRIMARY KEY'
;
}
if
(
dataTyp
e
.
references
)
{
if
(
attribut
e
.
references
)
{
template
+=
' REFERENCES <%= referencesTable %> (<%= referencesKey %>)'
;
replacements
.
referencesTable
=
this
.
quoteTable
(
dataTyp
e
.
references
);
replacements
.
referencesTable
=
this
.
quoteTable
(
attribut
e
.
references
);
if
(
dataTyp
e
.
referencesKey
)
{
replacements
.
referencesKey
=
this
.
quoteIdentifiers
(
dataTyp
e
.
referencesKey
);
if
(
attribut
e
.
referencesKey
)
{
replacements
.
referencesKey
=
this
.
quoteIdentifiers
(
attribut
e
.
referencesKey
);
}
else
{
replacements
.
referencesKey
=
this
.
quoteIdentifier
(
'id'
);
}
if
(
dataTyp
e
.
onDelete
)
{
if
(
attribut
e
.
onDelete
)
{
template
+=
' ON DELETE <%= onDeleteAction %>'
;
replacements
.
onDeleteAction
=
dataTyp
e
.
onDelete
.
toUpperCase
();
replacements
.
onDeleteAction
=
attribut
e
.
onDelete
.
toUpperCase
();
}
if
(
dataTyp
e
.
onUpdate
)
{
if
(
attribut
e
.
onUpdate
)
{
template
+=
' ON UPDATE <%= onUpdateAction %>'
;
replacements
.
onUpdateAction
=
dataTyp
e
.
onUpdate
.
toUpperCase
();
replacements
.
onUpdateAction
=
attribut
e
.
onUpdate
.
toUpperCase
();
}
}
if
(
dataType
.
comment
&&
Utils
.
_
.
isString
(
dataType
.
comment
))
{
template
+=
' COMMENT ON COLUMN <%= tableName %>.<%= columnName %> IS <%= comment %>'
;
replacements
.
columnName
=
this
.
quoteIdentifier
(
name
);
replacements
.
tableName
=
'<%= table %>'
;
// Hacky, table name will be inserted by create table
replacements
.
comment
=
this
.
escape
(
dataType
.
comment
);
}
return
Utils
.
_
.
template
(
template
)(
replacements
);
},
result
[
fieldName
]
=
Utils
.
_
.
template
(
template
)(
replacements
);
}
else
{
result
[
fieldName
]
=
dataType
;
}
attributesToSQL
:
function
(
attributes
,
options
)
{
var
result
=
{}
,
key
,
attribute
;
for
(
key
in
attributes
)
{
attribute
=
attributes
[
key
];
result
[
attribute
.
field
||
key
]
=
this
.
attributeToSQL
(
attribute
,
options
);
}
return
result
;
...
...
lib/query-interface.js
View file @
0c1ec1c
...
...
@@ -300,19 +300,8 @@ module.exports = (function() {
});
};
QueryInterface
.
prototype
.
addColumn
=
function
(
tableName
,
attributeName
,
dataTypeOrOptions
)
{
var
attributes
=
{};
if
(
Utils
.
_
.
values
(
DataTypes
).
indexOf
(
dataTypeOrOptions
)
>
-
1
)
{
attributes
[
attributeName
]
=
{
type
:
dataTypeOrOptions
,
allowNull
:
true
};
}
else
{
attributes
[
attributeName
]
=
dataTypeOrOptions
;
}
var
options
=
this
.
QueryGenerator
.
attributesToSQL
(
attributes
)
,
sql
=
this
.
QueryGenerator
.
addColumnQuery
(
tableName
,
options
);
return
this
.
sequelize
.
query
(
sql
,
null
,
{
logging
:
this
.
sequelize
.
options
.
logging
});
QueryInterface
.
prototype
.
addColumn
=
function
(
table
,
key
,
dataType
)
{
return
this
.
sequelize
.
query
(
this
.
QueryGenerator
.
addColumnQuery
(
table
,
key
,
dataType
),
null
,
{
logging
:
this
.
sequelize
.
options
.
logging
});
};
QueryInterface
.
prototype
.
removeColumn
=
function
(
tableName
,
attributeName
)
{
...
...
test/dao-factory.test.js
View file @
0c1ec1c
...
...
@@ -2377,4 +2377,29 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
})
})
it
(
'should be possible to use a key named UUID as foreign key'
,
function
()
{
var
project
=
this
.
sequelize
.
define
(
'project'
,
{
UserId
:
{
type
:
Sequelize
.
STRING
,
references
:
'Users'
,
referencesKey
:
'UUID'
}
});
var
user
=
this
.
sequelize
.
define
(
'Users'
,
{
UUID
:
{
type
:
Sequelize
.
STRING
,
primaryKey
:
true
,
unique
:
true
,
allowNull
:
false
,
validate
:
{
notNull
:
true
,
notEmpty
:
true
}
}
});
return
this
.
sequelize
.
sync
({
force
:
true
});
});
})
test/query-interface.test.js
View file @
0c1ec1c
...
...
@@ -218,5 +218,35 @@ describe(Support.getTestDialectTeaser("QueryInterface"), function () {
return
self
.
queryInterface
.
renameColumn
(
'Fruit'
,
'fruitId'
,
'fruit_id'
)
})
})
})
});
describe
(
'addColumn'
,
function
()
{
beforeEach
(
function
()
{
return
this
.
queryInterface
.
createTable
(
'users'
,
{
id
:
{
type
:
DataTypes
.
INTEGER
,
primaryKey
:
true
,
autoincrement
:
true
}
});
});
it
(
'should be able to add a foreign key reference'
,
function
()
{
return
this
.
queryInterface
.
createTable
(
'level'
,
{
id
:
{
type
:
DataTypes
.
INTEGER
,
primaryKey
:
true
,
autoincrement
:
true
}
}).
bind
(
this
).
then
(
function
()
{
return
this
.
queryInterface
.
addColumn
(
'users'
,
'level_id'
,
{
type
:
DataTypes
.
INTEGER
,
references
:
'level'
,
referenceKey
:
'id'
,
onUpdate
:
'cascade'
,
onDelete
:
'restrict'
});
});
});
});
})
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