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
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
151 additions
and
118 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,92 +384,88 @@ module.exports = (function() {
...
@@ -386,92 +384,88 @@ module.exports = (function() {
return
query
;
return
query
;
},
},
attributesToSQL
:
function
(
attributes
)
{
attributeToSQL
:
function
(
attribute
,
options
)
{
var
result
=
{};
if
(
!
Utils
.
_
.
isPlainObject
(
attribute
))
{
attribute
=
{
type
:
attribute
};
}
for
(
var
name
in
attributes
)
{
var
template
=
'<%= type %>'
var
dataType
=
attributes
[
name
];
,
replacements
=
{
type
:
attribute
.
type
};
var
fieldName
=
dataType
.
field
||
name
;
if
(
Utils
.
_
.
isObject
(
dataType
))
{
if
(
attribute
.
type
.
toString
()
===
DataTypes
.
ENUM
.
toString
())
{
var
template
=
'<%= type %>'
if
(
Array
.
isArray
(
attribute
.
values
)
&&
(
attribute
.
values
.
length
>
0
))
{
,
replacements
=
{
type
:
dataType
.
type
};
replacements
.
type
=
'ENUM('
+
Utils
.
_
.
map
(
attribute
.
values
,
function
(
value
)
{
return
this
.
escape
(
value
);
}.
bind
(
this
)).
join
(
', '
)
+
')'
;
}
else
{
throw
new
Error
(
'Values for ENUM haven\'t been defined.'
);
}
}
if
(
dataType
.
type
.
toString
()
===
DataTypes
.
ENUM
.
toString
())
{
if
(
attribute
.
type
===
attribute
.
BOOLEAN
)
{
if
(
Array
.
isArray
(
dataType
.
values
)
&&
(
dataType
.
values
.
length
>
0
))
{
attribute
.
type
=
'BOOLEAN'
;
replacements
.
type
=
'ENUM('
+
Utils
.
_
.
map
(
dataType
.
values
,
function
(
value
)
{
}
return
this
.
escape
(
value
);
}.
bind
(
this
)).
join
(
', '
)
+
')'
;
}
else
{
throw
new
Error
(
'Values for ENUM haven\'t been defined.'
);
}
}
if
(
dataType
.
type
===
DataTypes
.
BOOLEAN
)
{
if
(
attribute
.
type
===
attribute
.
DATE
)
{
dataType
.
type
=
'BOOLEAN'
;
attribute
.
_typeName
=
'DATETIME'
;
}
attribute
.
type
=
'TIMESTAMP WITH TIME ZONE'
;
}
if
(
dataType
.
type
===
DataTypes
.
DATE
)
{
if
(
attribute
.
hasOwnProperty
(
'allowNull'
)
&&
(
!
attribute
.
allowNull
))
{
dataType
.
_typeName
=
'DATETIME'
;
template
+=
' NOT NULL'
;
dataType
.
type
=
'TIMESTAMP WITH TIME ZONE'
;
}
}
if
(
dataType
.
hasOwnProperty
(
'allowNull'
)
&&
(
!
dataType
.
allowNull
)
)
{
if
(
attribute
.
autoIncrement
)
{
template
+=
' NOT NUL
L'
;
template
+=
' SERIA
L'
;
}
}
if
(
dataType
.
autoIncrement
)
{
if
(
Utils
.
defaultValueSchemable
(
attribute
.
defaultValue
))
{
template
+=
' SERIAL'
;
template
+=
' DEFAULT <%= defaultValue %>'
;
}
replacements
.
defaultValue
=
this
.
escape
(
attribute
.
defaultValue
,
attribute
);
}
if
(
Utils
.
defaultValueSchemable
(
dataType
.
defaultValue
))
{
if
(
attribute
.
unique
===
true
)
{
// TODO thoroughly check that DataTypes.NOW will properly
template
+=
' UNIQUE'
;
// get populated on all databases as DEFAULT value
}
// i.e. mysql requires: DEFAULT CURRENT_TIMESTAMP
template
+=
' DEFAULT <%= defaultValue %>'
;
replacements
.
defaultValue
=
this
.
escape
(
dataType
.
defaultValue
,
dataType
);
}
if
(
dataType
.
unique
===
true
)
{
if
(
attribute
.
primaryKey
)
{
template
+=
' UNIQUE
'
;
template
+=
' PRIMARY KEY
'
;
}
}
if
(
dataType
.
primaryKey
)
{
if
(
attribute
.
references
)
{
template
+=
' PRIMARY KEY
'
;
template
+=
' REFERENCES <%= referencesTable %> (<%= referencesKey %>)
'
;
}
replacements
.
referencesTable
=
this
.
quoteTable
(
attribute
.
references
);
if
(
dataType
.
references
)
{
if
(
attribute
.
referencesKey
)
{
template
+=
' REFERENCES <%= referencesTable %> (<%= referencesKey %>)'
;
replacements
.
referencesKey
=
this
.
quoteIdentifiers
(
attribute
.
referencesKey
);
replacements
.
referencesTable
=
this
.
quoteTable
(
dataType
.
references
);
}
else
{
replacements
.
referencesKey
=
this
.
quoteIdentifier
(
'id'
);
}
if
(
dataType
.
referencesKey
)
{
if
(
attribute
.
onDelete
)
{
replacements
.
referencesKey
=
this
.
quoteIdentifiers
(
dataType
.
referencesKey
);
template
+=
' ON DELETE <%= onDeleteAction %>'
;
}
else
{
replacements
.
onDeleteAction
=
attribute
.
onDelete
.
toUpperCase
();
replacements
.
referencesKey
=
this
.
quoteIdentifier
(
'id'
);
}
}
if
(
dataType
.
onDelete
)
{
if
(
attribute
.
onUpdate
)
{
template
+=
' ON DELETE <%= onDeleteAction %>'
;
template
+=
' ON UPDATE <%= onUpdateAction %>'
;
replacements
.
onDeleteAction
=
dataType
.
onDelete
.
toUpperCase
();
replacements
.
onUpdateAction
=
attribute
.
onUpdate
.
toUpperCase
();
}
}
}
if
(
dataType
.
onUpdate
)
{
return
Utils
.
_
.
template
(
template
)(
replacements
);
template
+=
' ON UPDATE <%= onUpdateAction %>'
;
},
replacements
.
onUpdateAction
=
dataType
.
onUpdate
.
toUpperCase
();
}
}
if
(
dataType
.
comment
&&
Utils
.
_
.
isString
(
dataType
.
comment
))
{
attributesToSQL
:
function
(
attributes
,
options
)
{
template
+=
' COMMENT ON COLUMN <%= tableName %>.<%= columnName %> IS <%= comment %>'
;
var
result
=
{}
replacements
.
columnName
=
this
.
quoteIdentifier
(
name
);
,
key
replacements
.
tableName
=
'<%= table %>'
;
// Hacky, table name will be inserted by create table
,
attribute
;
replacements
.
comment
=
this
.
escape
(
dataType
.
comment
);
}
result
[
fieldName
]
=
Utils
.
_
.
template
(
template
)(
replacements
);
for
(
key
in
attributes
)
{
}
else
{
attribute
=
attributes
[
key
];
result
[
fieldName
]
=
dataType
;
result
[
attribute
.
field
||
key
]
=
this
.
attributeToSQL
(
attribute
,
options
);
}
}
}
return
result
;
return
result
;
...
@@ -733,7 +727,7 @@ module.exports = (function() {
...
@@ -733,7 +727,7 @@ module.exports = (function() {
pgDataTypeMapping
:
function
(
tableName
,
attr
,
dataType
)
{
pgDataTypeMapping
:
function
(
tableName
,
attr
,
dataType
)
{
return
this
.
dataTypeMapping
(
tableName
,
attr
,
dataType
);
return
this
.
dataTypeMapping
(
tableName
,
attr
,
dataType
);
},
},
dataTypeMapping
:
function
(
tableName
,
attr
,
dataType
)
{
dataTypeMapping
:
function
(
tableName
,
attr
,
dataType
)
{
if
(
Utils
.
_
.
includes
(
dataType
,
'PRIMARY KEY'
))
{
if
(
Utils
.
_
.
includes
(
dataType
,
'PRIMARY KEY'
))
{
primaryKeys
[
tableName
].
push
(
attr
);
primaryKeys
[
tableName
].
push
(
attr
);
...
...
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