-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactoring method createTable #4456
Changes from all commits
2146a57
e07da41
78c63c4
d2fdc64
c2f00ce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -684,6 +684,7 @@ export class PostgresStorageAdapter { | |
// Just create a table, do not insert in schema | ||
createTable(className, schema, conn) { | ||
conn = conn || this._client; | ||
const self = this; | ||
debug('createTable', className, schema); | ||
const valuesArray = []; | ||
const patternsArray = []; | ||
|
@@ -721,24 +722,23 @@ export class PostgresStorageAdapter { | |
}); | ||
const qs = `CREATE TABLE IF NOT EXISTS $1:name (${patternsArray.join(',')})`; | ||
const values = [className, ...valuesArray]; | ||
return conn.task(t => { | ||
return this._ensureSchemaCollectionExists(t) | ||
.then(() => t.none(qs, values)) | ||
.catch(error => { | ||
if (error.code === PostgresDuplicateRelationError) { | ||
// Table already exists, must have been created by a different request. Ignore error. | ||
} else { | ||
|
||
return conn.task('create-table', function * (t) { | ||
try { | ||
yield self._ensureSchemaCollectionExists(t); | ||
yield t.none(qs, values); | ||
} catch(error) { | ||
if (error.code !== PostgresDuplicateRelationError) { | ||
throw error; | ||
}}) | ||
}) | ||
.then(() => { | ||
return conn.tx('create-relation-tables', t => { | ||
const queries = relations.map((fieldName) => { | ||
return t.none('CREATE TABLE IF NOT EXISTS $<joinTable:name> ("relatedId" varChar(120), "owningId" varChar(120), PRIMARY KEY("relatedId", "owningId") )', {joinTable: `_Join:${fieldName}:${className}`}); | ||
}); | ||
return t.batch(queries); | ||
}); | ||
} | ||
// ELSE: Table already exists, must have been created by a different request. Ignore the error. | ||
} | ||
yield t.tx('create-table-tx', tx => { | ||
return tx.batch(relations.map(fieldName => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @vitaly-t Merry Christmas. Quick question. I've notice you got rid of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dplewis as per that PR, it is a performance optimization, to execute all queries via a single IO operation, as opposed to executing them separately. It is much faster that way ;) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks good to me 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return tx.none('CREATE TABLE IF NOT EXISTS $<joinTable:name> ("relatedId" varChar(120), "owningId" varChar(120), PRIMARY KEY("relatedId", "owningId") )', {joinTable: `_Join:${fieldName}:${className}`}); | ||
})); | ||
}); | ||
}); | ||
} | ||
|
||
addFieldIfNotExists(className, fieldName, type) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
quick question, shouldn't we use
.bind(this)
instead ofself
capturing?