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 1bd8ae1e
authored
Sep 07, 2019
by
Sushant
Committed by
GitHub
Sep 07, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix(postgres): syncing indexes with schema (#11399)
1 parent
f07da3df
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
87 additions
and
49 deletions
lib/dialects/abstract/query-generator.js
lib/dialects/postgres/query-generator.js
test/integration/dialects/postgres/associations.test.js
test/integration/model/schema.test.js
lib/dialects/abstract/query-generator.js
View file @
1bd8ae1
...
@@ -569,13 +569,6 @@ class QueryGenerator {
...
@@ -569,13 +569,6 @@ class QueryGenerator {
options
.
where
=
this
.
whereQuery
(
options
.
where
);
options
.
where
=
this
.
whereQuery
(
options
.
where
);
}
}
if
(
options
.
schema
)
{
tableName
=
{
tableName
,
schema
:
options
.
schema
};
}
if
(
typeof
tableName
===
'string'
)
{
if
(
typeof
tableName
===
'string'
)
{
tableName
=
this
.
quoteIdentifiers
(
tableName
);
tableName
=
this
.
quoteIdentifiers
(
tableName
);
}
else
{
}
else
{
...
@@ -1690,7 +1683,7 @@ class QueryGenerator {
...
@@ -1690,7 +1683,7 @@ class QueryGenerator {
const joinSource = `
$
{
asLeft
.
replace
(
/->/g
,
'.'
)}.
$
{
attrLeft
}
`;
const joinSource = `
$
{
asLeft
.
replace
(
/->/g
,
'.'
)}.
$
{
attrLeft
}
`;
// Check for potential aliased JOIN condition
// Check for potential aliased JOIN condition
joinOn = this._getAliasForField(asLeft, joinSource, topLevelInfo.options) || this.quoteIdentifier(joinSource);
joinOn = this._getAliasForField(asLeft, joinSource, topLevelInfo.options) || this.quoteIdentifier(joinSource);
}
}
}
}
...
@@ -1965,7 +1958,7 @@ class QueryGenerator {
...
@@ -1965,7 +1958,7 @@ class QueryGenerator {
if (Array.isArray(options.order)) {
if (Array.isArray(options.order)) {
for (let order of options.order) {
for (let order of options.order) {
// wrap if not array
// wrap if not array
if (!Array.isArray(order)) {
if (!Array.isArray(order)) {
order = [order];
order = [order];
...
@@ -1989,7 +1982,7 @@ class QueryGenerator {
...
@@ -1989,7 +1982,7 @@ class QueryGenerator {
const subQueryAttribute = options.attributes.find(a => Array.isArray(a) && a[0] === order[0] && a[1]);
const subQueryAttribute = options.attributes.find(a => Array.isArray(a) && a[0] === order[0] && a[1]);
if (subQueryAttribute) {
if (subQueryAttribute) {
const modelName = this.quoteIdentifier(model.name);
const modelName = this.quoteIdentifier(model.name);
order[0] = new Utils.Col(this._getAliasForField(modelName, subQueryAttribute[1], options) || subQueryAttribute[1]);
order[0] = new Utils.Col(this._getAliasForField(modelName, subQueryAttribute[1], options) || subQueryAttribute[1]);
}
}
}
}
...
...
lib/dialects/postgres/query-generator.js
View file @
1bd8ae1
...
@@ -515,7 +515,6 @@ class PostgresQueryGenerator extends AbstractQueryGenerator {
...
@@ -515,7 +515,6 @@ class PostgresQueryGenerator extends AbstractQueryGenerator {
if
(
attribute
.
references
)
{
if
(
attribute
.
references
)
{
let
referencesTable
=
this
.
quoteTable
(
attribute
.
references
.
model
);
let
referencesTable
=
this
.
quoteTable
(
attribute
.
references
.
model
);
const
tableDetails
=
this
.
extractTableDetails
(
referencesTable
,
options
);
let
schema
;
let
schema
;
if
(
options
.
schema
)
{
if
(
options
.
schema
)
{
...
@@ -529,7 +528,10 @@ class PostgresQueryGenerator extends AbstractQueryGenerator {
...
@@ -529,7 +528,10 @@ class PostgresQueryGenerator extends AbstractQueryGenerator {
}
}
if
(
schema
)
{
if
(
schema
)
{
referencesTable
=
schema
+
tableDetails
.
delimiter
+
referencesTable
;
referencesTable
=
this
.
quoteTable
(
this
.
addSchema
({
tableName
:
referencesTable
,
_schema
:
schema
}));
}
}
let
referencesKey
;
let
referencesKey
;
...
...
test/integration/dialects/postgres/associations.test.js
View file @
1bd8ae1
...
@@ -140,39 +140,6 @@ if (dialect.match(/^postgres/)) {
...
@@ -140,39 +140,6 @@ if (dialect.match(/^postgres/)) {
});
});
});
});
});
});
it
(
'defaults to schema provided to sync() for references #11276'
,
function
()
{
const
User
=
this
.
sequelize
.
define
(
'UserXYZ'
,
{
uid
:
{
type
:
DataTypes
.
INTEGER
,
primaryKey
:
true
,
autoIncrement
:
true
,
allowNull
:
false
}
}),
Task
=
this
.
sequelize
.
define
(
'TaskXYZ'
,
{
});
Task
.
belongsTo
(
User
);
return
Support
.
dropTestSchemas
(
this
.
sequelize
).
then
(()
=>
{
return
this
.
sequelize
.
createSchema
(
'archive'
);
}).
then
(()
=>
{
return
User
.
sync
({
force
:
true
,
schema
:
'archive'
});
}).
then
(()
=>
{
return
Task
.
sync
({
force
:
true
,
schema
:
'archive'
});
}).
then
(()
=>
{
return
User
.
schema
(
'archive'
).
create
({});
}).
then
(
user
=>
{
return
Task
.
schema
(
'archive'
).
create
({}).
then
(
task
=>
{
return
task
.
setUserXYZ
(
user
).
then
(()
=>
{
return
task
.
getUserXYZ
({
schema
:
'archive'
});
});
});
}).
then
(
user
=>
{
expect
(
user
).
to
.
be
.
ok
;
return
this
.
sequelize
.
dropSchema
(
'archive'
);
});
});
});
});
});
});
});
});
...
...
test/integration/model/schema.test.js
View file @
1bd8ae1
...
@@ -3,6 +3,7 @@
...
@@ -3,6 +3,7 @@
const
chai
=
require
(
'chai'
),
const
chai
=
require
(
'chai'
),
expect
=
chai
.
expect
,
expect
=
chai
.
expect
,
Support
=
require
(
'../support'
),
Support
=
require
(
'../support'
),
dialect
=
Support
.
getTestDialect
(),
DataTypes
=
require
(
'../../../lib/data-types'
),
DataTypes
=
require
(
'../../../lib/data-types'
),
current
=
Support
.
sequelize
,
current
=
Support
.
sequelize
,
Op
=
Support
.
Sequelize
.
Op
,
Op
=
Support
.
Sequelize
.
Op
,
...
@@ -184,8 +185,8 @@ describe(Support.getTestDialectTeaser('Model'), () => {
...
@@ -184,8 +185,8 @@ describe(Support.getTestDialectTeaser('Model'), () => {
beforeEach
(
'build restaurant tables'
,
function
()
{
beforeEach
(
'build restaurant tables'
,
function
()
{
return
Promise
.
all
([
return
Promise
.
all
([
current
.
createSchema
(
'schema_one'
),
current
.
createSchema
(
SCHEMA_ONE
),
current
.
createSchema
(
'schema_two'
)
current
.
createSchema
(
SCHEMA_TWO
)
]).
then
(()
=>
{
]).
then
(()
=>
{
return
Promise
.
all
([
return
Promise
.
all
([
this
.
RestaurantOne
.
sync
({
force
:
true
}),
this
.
RestaurantOne
.
sync
({
force
:
true
}),
...
@@ -196,8 +197,8 @@ describe(Support.getTestDialectTeaser('Model'), () => {
...
@@ -196,8 +197,8 @@ describe(Support.getTestDialectTeaser('Model'), () => {
afterEach
(
'drop schemas'
,
()
=>
{
afterEach
(
'drop schemas'
,
()
=>
{
return
Promise
.
all
([
return
Promise
.
all
([
current
.
dropSchema
(
'schema_one'
),
current
.
dropSchema
(
SCHEMA_ONE
),
current
.
dropSchema
(
'schema_two'
)
current
.
dropSchema
(
SCHEMA_TWO
)
]);
]);
});
});
...
@@ -510,6 +511,81 @@ describe(Support.getTestDialectTeaser('Model'), () => {
...
@@ -510,6 +511,81 @@ describe(Support.getTestDialectTeaser('Model'), () => {
});
});
});
});
});
});
describe
(
'regressions'
,
()
=>
{
it
(
'should be able to sync model with schema'
,
function
()
{
const
User
=
this
.
sequelize
.
define
(
'User1'
,
{
name
:
DataTypes
.
STRING
,
value
:
DataTypes
.
INTEGER
},
{
schema
:
SCHEMA_ONE
,
indexes
:
[
{
name
:
'test_slug_idx'
,
fields
:
[
'name'
]
}
]
});
const
Task
=
this
.
sequelize
.
define
(
'Task2'
,
{
name
:
DataTypes
.
STRING
,
value
:
DataTypes
.
INTEGER
},
{
schema
:
SCHEMA_TWO
,
indexes
:
[
{
name
:
'test_slug_idx'
,
fields
:
[
'name'
]
}
]
});
return
User
.
sync
({
force
:
true
}).
then
(()
=>
{
return
Task
.
sync
({
force
:
true
});
}).
then
(()
=>
{
return
Promise
.
all
([
this
.
sequelize
.
queryInterface
.
describeTable
(
User
.
tableName
,
SCHEMA_ONE
),
this
.
sequelize
.
queryInterface
.
describeTable
(
Task
.
tableName
,
SCHEMA_TWO
)
]);
}).
then
(([
user
,
task
])
=>
{
expect
(
user
).
to
.
be
.
ok
;
expect
(
task
).
to
.
be
.
ok
;
});
});
// TODO: this should work with MSSQL / MariaDB too
// Need to fix addSchema return type
if
(
dialect
.
match
(
/^postgres/
))
{
it
(
'defaults to schema provided to sync() for references #11276'
,
function
()
{
const
User
=
this
.
sequelize
.
define
(
'UserXYZ'
,
{
uid
:
{
type
:
DataTypes
.
INTEGER
,
primaryKey
:
true
,
autoIncrement
:
true
,
allowNull
:
false
}
}),
Task
=
this
.
sequelize
.
define
(
'TaskXYZ'
,
{
});
Task
.
belongsTo
(
User
);
return
User
.
sync
({
force
:
true
,
schema
:
SCHEMA_ONE
}).
then
(()
=>
{
return
Task
.
sync
({
force
:
true
,
schema
:
SCHEMA_ONE
});
}).
then
(()
=>
{
return
User
.
schema
(
SCHEMA_ONE
).
create
({});
}).
then
(
user
=>
{
return
Task
.
schema
(
SCHEMA_ONE
).
create
({}).
then
(
task
=>
{
return
task
.
setUserXYZ
(
user
).
then
(()
=>
{
return
task
.
getUserXYZ
({
schema
:
SCHEMA_ONE
});
});
});
}).
then
(
user
=>
{
expect
(
user
).
to
.
be
.
ok
;
});
});
}
});
});
});
}
}
});
});
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