Skip to content

Commit e199ee4

Browse files
committed
fix conflicts
1 parent 41ea656 commit e199ee4

File tree

3 files changed

+31
-49
lines changed

3 files changed

+31
-49
lines changed

src/Adapters/Storage/Postgres/PostgresStorageAdapter.js

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,43 +1096,26 @@ export class PostgresStorageAdapter implements StorageAdapter {
10961096
}
10971097
} else {
10981098
await t.none(
1099-
'ALTER TABLE $<className:name> ADD COLUMN IF NOT EXISTS $<fieldName:name> $<postgresType:raw>',
1100-
{
1101-
className,
1102-
fieldName,
1103-
postgresType: parseTypeToPostgresType(type),
1104-
}
1099+
'CREATE TABLE IF NOT EXISTS $<joinTable:name> ("relatedId" varChar(120), "owningId" varChar(120), PRIMARY KEY("relatedId", "owningId") )',
1100+
{ joinTable: `_Join:${fieldName}:${className}` }
11051101
);
1106-
} catch (error) {
1107-
if (error.code === PostgresRelationDoesNotExistError) {
1108-
return self.createClass(className, { fields: { [fieldName]: type } }, t);
1109-
}
1110-
if (error.code !== PostgresDuplicateColumnError) {
1111-
throw error;
1112-
}
1113-
// Column already exists, created by other request. Carry on to see if it's the right type.
11141102
}
1115-
} else {
1116-
await t.none(
1117-
'CREATE TABLE IF NOT EXISTS $<joinTable:name> ("relatedId" varChar(120), "owningId" varChar(120), PRIMARY KEY("relatedId", "owningId") )',
1118-
{ joinTable: `_Join:${fieldName}:${className}` }
1119-
);
1120-
}
11211103

1122-
const result = await t.any(
1123-
'SELECT "schema" FROM "_SCHEMA" WHERE "className" = $<className> and ("schema"::json->\'fields\'->$<fieldName>) is not null',
1124-
{ className, fieldName }
1125-
);
1126-
1127-
if (result[0]) {
1128-
throw 'Attempted to add a field that already exists';
1129-
} else {
1130-
const path = `{fields,${fieldName}}`;
1131-
await t.none(
1132-
'UPDATE "_SCHEMA" SET "schema"=jsonb_set("schema", $<path>, $<type>) WHERE "className"=$<className>',
1133-
{ path, type, className }
1104+
const result = await t.any(
1105+
'SELECT "schema" FROM "_SCHEMA" WHERE "className" = $<className> and ("schema"::json->\'fields\'->$<fieldName>) is not null',
1106+
{ className, fieldName }
11341107
);
1135-
}
1108+
1109+
if (result[0]) {
1110+
throw 'Attempted to add a field that already exists';
1111+
} else {
1112+
const path = `{fields,${fieldName}}`;
1113+
await t.none(
1114+
'UPDATE "_SCHEMA" SET "schema"=jsonb_set("schema", $<path>, $<type>) WHERE "className"=$<className>',
1115+
{ path, type, className }
1116+
);
1117+
}
1118+
});
11361119
this._notifySchemaChange();
11371120
}
11381121

src/Controllers/DatabaseController.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ class DatabaseController {
484484
return this.canAddField(schema, className, object, aclGroup, runOptions);
485485
})
486486
.then(() => {
487-
return schema.validateObject(className, object, query, this._transactionalSession);
487+
return schema.validateObject(className, object, query);
488488
});
489489
}
490490

src/Controllers/SchemaController.js

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -807,9 +807,8 @@ export default class SchemaController {
807807
className,
808808
})
809809
);
810-
// TODO: Remove by updating schema cache directly
811-
await this.reloadData({ clearCache: true });
812810
const parseSchema = convertAdapterSchemaToParseSchema(adapterSchema);
811+
SchemaCache.set(className, parseSchema);
813812
return parseSchema;
814813
} catch (error) {
815814
if (error && error.code === Parse.Error.DUPLICATE_VALUE) {
@@ -935,6 +934,7 @@ export default class SchemaController {
935934
return (
936935
// The schema update succeeded. Reload the schema
937936
this.addClassIfNotExists(className)
937+
.then(() => this.reloadData())
938938
.catch(() => {
939939
// The schema update failed. This can be okay - it might
940940
// have failed because there's a race condition and a different
@@ -1060,12 +1060,7 @@ export default class SchemaController {
10601060
// object if the provided className-fieldName-type tuple is valid.
10611061
// The className must already be validated.
10621062
// If 'freeze' is true, refuse to update the schema for this field.
1063-
enforceFieldExists(
1064-
className: string,
1065-
fieldName: string,
1066-
type: string | SchemaField,
1067-
transactionalSession: ?any
1068-
) {
1063+
enforceFieldExists(className: string, fieldName: string, type: string | SchemaField) {
10691064
if (fieldName.indexOf('.') > 0) {
10701065
// subdocument key (x.y) => ok if x is of type 'object'
10711066
fieldName = fieldName.split('.')[0];
@@ -1113,7 +1108,7 @@ export default class SchemaController {
11131108
}
11141109

11151110
return this._dbAdapter
1116-
.addFieldIfNotExists(className, fieldName, type, transactionalSession)
1111+
.addFieldIfNotExists(className, fieldName, type)
11171112
.catch(error => {
11181113
if (error.code == Parse.Error.INCORRECT_TYPE) {
11191114
// Make sure that we throw errors when it is appropriate to do so.
@@ -1125,6 +1120,13 @@ export default class SchemaController {
11251120
return Promise.resolve();
11261121
})
11271122
.then(() => {
1123+
const cached = SchemaCache.get(className);
1124+
if (cached) {
1125+
if (cached && !cached.fields[fieldName]) {
1126+
cached.fields[fieldName] = type;
1127+
SchemaCache.set(className, cached);
1128+
}
1129+
}
11281130
return {
11291131
className,
11301132
fieldName,
@@ -1214,7 +1216,7 @@ export default class SchemaController {
12141216
// Validates an object provided in REST format.
12151217
// Returns a promise that resolves to the new schema if this object is
12161218
// valid.
1217-
async validateObject(className: string, object: any, query: any, transactionalSession: ?any) {
1219+
async validateObject(className: string, object: any, query: any) {
12181220
let geocount = 0;
12191221
const schema = await this.enforceClassExists(className);
12201222
const results = [];
@@ -1244,15 +1246,12 @@ export default class SchemaController {
12441246
// Every object has ACL implicitly.
12451247
continue;
12461248
}
1247-
results.push(
1248-
await schema.enforceFieldExists(className, fieldName, expected, transactionalSession)
1249-
);
1249+
results.push(await schema.enforceFieldExists(className, fieldName, expected));
12501250
}
12511251
const enforceFields = results.filter(result => !!result);
12521252

12531253
if (enforceFields.length !== 0) {
1254-
// TODO: Remove by updating schema cache directly
1255-
await this.reloadData({ clearCache: true });
1254+
await this.reloadData();
12561255
}
12571256
this.ensureFields(enforceFields);
12581257

0 commit comments

Comments
 (0)