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() {
...
@@ -113,20 +113,19 @@ module.exports = (function() {
}
}
},
},
addColumnQuery
:
function
(
tableName
,
attributes
)
{
addColumnQuery
:
function
(
table
,
key
,
dataType
)
{
var
query
=
'ALTER TABLE `<%= tableName %>` ADD <%= attributes %>;'
var
query
=
'ALTER TABLE <%= table %> ADD <%= attribute %>;'
,
attrString
=
[];
,
attribute
=
Utils
.
_
.
template
(
'<%= key %> <%= definition %>'
)({
key
:
this
.
quoteIdentifier
(
key
),
for
(
var
attrName
in
attributes
)
{
definition
:
this
.
attributeToSQL
(
dataType
,
{
var
definition
=
attributes
[
attrName
];
context
:
'addColumn'
})
attrString
.
push
(
Utils
.
_
.
template
(
'`<%= attrName %>` <%= definition %>'
)({
});
attrName
:
attrName
,
definition
:
this
.
mysqlDataTypeMapping
(
tableName
,
attrName
,
definition
)
}));
}
return
Utils
.
_
.
template
(
query
)({
tableName
:
tableName
,
attributes
:
attrString
.
join
(
', '
)
});
return
Utils
.
_
.
template
(
query
)({
table
:
this
.
quoteTable
(
table
),
attribute
:
attribute
});
},
},
removeColumnQuery
:
function
(
tableName
,
attributeName
)
{
removeColumnQuery
:
function
(
tableName
,
attributeName
)
{
...
@@ -278,10 +277,6 @@ module.exports = (function() {
...
@@ -278,10 +277,6 @@ module.exports = (function() {
template
+=
' PRIMARY KEY'
;
template
+=
' PRIMARY KEY'
;
}
}
if
(
attribute
.
comment
&&
Utils
.
_
.
isString
(
attribute
.
comment
)
&&
attribute
.
comment
.
length
)
{
template
+=
' COMMENT '
+
this
.
escape
(
attribute
.
comment
);
}
if
(
attribute
.
references
)
{
if
(
attribute
.
references
)
{
template
+=
' REFERENCES '
+
this
.
quoteTable
(
attribute
.
references
);
template
+=
' REFERENCES '
+
this
.
quoteTable
(
attribute
.
references
);
...
...
lib/dialects/postgres/query-generator.js
View file @
0c1ec1c
...
@@ -119,26 +119,24 @@ module.exports = (function() {
...
@@ -119,26 +119,24 @@ module.exports = (function() {
}
}
},
},
addColumnQuery
:
function
(
table
Name
,
attributes
)
{
addColumnQuery
:
function
(
table
,
key
,
dataType
)
{
var
query
=
'ALTER TABLE <%= table
Name %> ADD COLUMN <%= attributes
%>;'
var
query
=
'ALTER TABLE <%= table
%> ADD COLUMN <%= attribute
%>;'
,
attr
String
=
[]
;
,
attr
ibute
;
for
(
var
attrName
in
attributes
)
{
if
(
dataType
.
toString
()
===
DataTypes
.
ENUM
.
toString
())
{
var
definition
=
attributes
[
attrName
];
query
=
this
.
pgEnum
(
table
,
key
,
dataType
)
+
query
;
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
;
}
}
}
attribute
=
Utils
.
_
.
template
(
'<%= key %> <%= definition %>'
)({
key
:
this
.
quoteIdentifier
(
key
),
definition
:
this
.
attributeToSQL
(
dataType
,
{
context
:
'addColumn'
})
});
return
Utils
.
_
.
template
(
query
)({
return
Utils
.
_
.
template
(
query
)({
table
Name
:
this
.
quoteTable
(
tableNam
e
),
table
:
this
.
quoteTable
(
tabl
e
),
attribute
s
:
attrString
.
join
(
', '
)
attribute
:
attribute
});
});
},
},
...
@@ -386,20 +384,19 @@ module.exports = (function() {
...
@@ -386,20 +384,19 @@ module.exports = (function() {
return
query
;
return
query
;
},
},
attribute
sToSQL
:
function
(
attribute
s
)
{
attribute
ToSQL
:
function
(
attribute
,
option
s
)
{
var
result
=
{};
if
(
!
Utils
.
_
.
isPlainObject
(
attribute
))
{
attribute
=
{
for
(
var
name
in
attributes
)
{
type
:
attribute
var
dataType
=
attributes
[
name
]
;
}
;
var
fieldName
=
dataType
.
field
||
name
;
}
if
(
Utils
.
_
.
isObject
(
dataType
))
{
var
template
=
'<%= type %>'
var
template
=
'<%= type %>'
,
replacements
=
{
type
:
dataTyp
e
.
type
};
,
replacements
=
{
type
:
attribut
e
.
type
};
if
(
dataTyp
e
.
type
.
toString
()
===
DataTypes
.
ENUM
.
toString
())
{
if
(
attribut
e
.
type
.
toString
()
===
DataTypes
.
ENUM
.
toString
())
{
if
(
Array
.
isArray
(
dataType
.
values
)
&&
(
dataTyp
e
.
values
.
length
>
0
))
{
if
(
Array
.
isArray
(
attribute
.
values
)
&&
(
attribut
e
.
values
.
length
>
0
))
{
replacements
.
type
=
'ENUM('
+
Utils
.
_
.
map
(
dataTyp
e
.
values
,
function
(
value
)
{
replacements
.
type
=
'ENUM('
+
Utils
.
_
.
map
(
attribut
e
.
values
,
function
(
value
)
{
return
this
.
escape
(
value
);
return
this
.
escape
(
value
);
}.
bind
(
this
)).
join
(
', '
)
+
')'
;
}.
bind
(
this
)).
join
(
', '
)
+
')'
;
}
else
{
}
else
{
...
@@ -407,71 +404,68 @@ module.exports = (function() {
...
@@ -407,71 +404,68 @@ module.exports = (function() {
}
}
}
}
if
(
dataType
.
type
===
DataTypes
.
BOOLEAN
)
{
if
(
attribute
.
type
===
attribute
.
BOOLEAN
)
{
dataTyp
e
.
type
=
'BOOLEAN'
;
attribut
e
.
type
=
'BOOLEAN'
;
}
}
if
(
dataType
.
type
===
DataTypes
.
DATE
)
{
if
(
attribute
.
type
===
attribute
.
DATE
)
{
dataTyp
e
.
_typeName
=
'DATETIME'
;
attribut
e
.
_typeName
=
'DATETIME'
;
dataTyp
e
.
type
=
'TIMESTAMP WITH TIME ZONE'
;
attribut
e
.
type
=
'TIMESTAMP WITH TIME ZONE'
;
}
}
if
(
dataType
.
hasOwnProperty
(
'allowNull'
)
&&
(
!
dataTyp
e
.
allowNull
))
{
if
(
attribute
.
hasOwnProperty
(
'allowNull'
)
&&
(
!
attribut
e
.
allowNull
))
{
template
+=
' NOT NULL'
;
template
+=
' NOT NULL'
;
}
}
if
(
dataTyp
e
.
autoIncrement
)
{
if
(
attribut
e
.
autoIncrement
)
{
template
+=
' SERIAL'
;
template
+=
' SERIAL'
;
}
}
if
(
Utils
.
defaultValueSchemable
(
dataType
.
defaultValue
))
{
if
(
Utils
.
defaultValueSchemable
(
attribute
.
defaultValue
))
{
// TODO thoroughly check that DataTypes.NOW will properly
// get populated on all databases as DEFAULT value
// i.e. mysql requires: DEFAULT CURRENT_TIMESTAMP
template
+=
' DEFAULT <%= 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'
;
template
+=
' UNIQUE'
;
}
}
if
(
dataTyp
e
.
primaryKey
)
{
if
(
attribut
e
.
primaryKey
)
{
template
+=
' PRIMARY KEY'
;
template
+=
' PRIMARY KEY'
;
}
}
if
(
dataTyp
e
.
references
)
{
if
(
attribut
e
.
references
)
{
template
+=
' REFERENCES <%= referencesTable %> (<%= referencesKey %>)'
;
template
+=
' REFERENCES <%= referencesTable %> (<%= referencesKey %>)'
;
replacements
.
referencesTable
=
this
.
quoteTable
(
dataTyp
e
.
references
);
replacements
.
referencesTable
=
this
.
quoteTable
(
attribut
e
.
references
);
if
(
dataTyp
e
.
referencesKey
)
{
if
(
attribut
e
.
referencesKey
)
{
replacements
.
referencesKey
=
this
.
quoteIdentifiers
(
dataTyp
e
.
referencesKey
);
replacements
.
referencesKey
=
this
.
quoteIdentifiers
(
attribut
e
.
referencesKey
);
}
else
{
}
else
{
replacements
.
referencesKey
=
this
.
quoteIdentifier
(
'id'
);
replacements
.
referencesKey
=
this
.
quoteIdentifier
(
'id'
);
}
}
if
(
dataTyp
e
.
onDelete
)
{
if
(
attribut
e
.
onDelete
)
{
template
+=
' ON DELETE <%= onDeleteAction %>'
;
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 %>'
;
template
+=
' ON UPDATE <%= onUpdateAction %>'
;
replacements
.
onUpdateAction
=
dataTyp
e
.
onUpdate
.
toUpperCase
();
replacements
.
onUpdateAction
=
attribut
e
.
onUpdate
.
toUpperCase
();
}
}
}
}
if
(
dataType
.
comment
&&
Utils
.
_
.
isString
(
dataType
.
comment
))
{
return
Utils
.
_
.
template
(
template
)(
replacements
);
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
);
}
result
[
fieldName
]
=
Utils
.
_
.
template
(
template
)(
replacements
);
attributesToSQL
:
function
(
attributes
,
options
)
{
}
else
{
var
result
=
{}
result
[
fieldName
]
=
dataType
;
,
key
}
,
attribute
;
for
(
key
in
attributes
)
{
attribute
=
attributes
[
key
];
result
[
attribute
.
field
||
key
]
=
this
.
attributeToSQL
(
attribute
,
options
);
}
}
return
result
;
return
result
;
...
...
lib/query-interface.js
View file @
0c1ec1c
...
@@ -300,19 +300,8 @@ module.exports = (function() {
...
@@ -300,19 +300,8 @@ module.exports = (function() {
});
});
};
};
QueryInterface
.
prototype
.
addColumn
=
function
(
tableName
,
attributeName
,
dataTypeOrOptions
)
{
QueryInterface
.
prototype
.
addColumn
=
function
(
table
,
key
,
dataType
)
{
var
attributes
=
{};
return
this
.
sequelize
.
query
(
this
.
QueryGenerator
.
addColumnQuery
(
table
,
key
,
dataType
),
null
,
{
logging
:
this
.
sequelize
.
options
.
logging
});
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
.
removeColumn
=
function
(
tableName
,
attributeName
)
{
QueryInterface
.
prototype
.
removeColumn
=
function
(
tableName
,
attributeName
)
{
...
...
test/dao-factory.test.js
View file @
0c1ec1c
...
@@ -2377,4 +2377,29 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
...
@@ -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 () {
...
@@ -218,5 +218,35 @@ describe(Support.getTestDialectTeaser("QueryInterface"), function () {
return
self
.
queryInterface
.
renameColumn
(
'Fruit'
,
'fruitId'
,
'fruit_id'
)
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