[postgres] Improve performance when adding many new fields to the Schema (#3740)

This commit is contained in:
Paulo Vítor S Reis
2017-04-23 18:25:33 -03:00
committed by Florent Vilmart
parent 5e14147676
commit 69042fbf50
2 changed files with 81 additions and 6 deletions

View File

@@ -543,15 +543,15 @@ export class PostgresStorageAdapter {
promise = t.none('CREATE TABLE IF NOT EXISTS $<joinTable:name> ("relatedId" varChar(120), "owningId" varChar(120), PRIMARY KEY("relatedId", "owningId") )', {joinTable: `_Join:${fieldName}:${className}`})
}
return promise.then(() => {
return t.any('SELECT "schema" FROM "_SCHEMA" WHERE "className" = $<className>', {className});
return t.any('SELECT "schema" FROM "_SCHEMA" WHERE "className" = $<className> and ("schema"::json->\'fields\'->$<fieldName>) is not null', {className, fieldName});
}).then(result => {
if (fieldName in result[0].schema.fields) {
if (result[0]) {
throw "Attempted to add a field that already exists";
} else {
result[0].schema.fields[fieldName] = type;
const path = `{fields,${fieldName}}`;
return t.none(
'UPDATE "_SCHEMA" SET "schema"=$<schema> WHERE "className"=$<className>',
{schema: result[0].schema, className}
'UPDATE "_SCHEMA" SET "schema"=jsonb_set("schema", $<path>, $<type>) WHERE "className"=$<className>',
{ path, type, className }
);
}
});