-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
WIP: Locked Pre defined Schemas / auto migration #6379
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,20 @@ import type { | |
SchemaField, | ||
LoadSchemaOptions, | ||
} from './types'; | ||
import logger from '../logger'; | ||
|
||
const defaultIndexes = Object.freeze({ | ||
_Default: { | ||
objectId: true, | ||
}, | ||
_User: { | ||
username: true, | ||
email: true, | ||
}, | ||
_Role: { | ||
name: true, | ||
}, | ||
}); | ||
|
||
const defaultColumns: { [string]: SchemaFields } = Object.freeze({ | ||
// Contain the default columns for every parse object type (except _Join collection) | ||
|
@@ -592,34 +606,26 @@ const _GraphQLConfigSchema = { | |
className: '_GraphQLConfig', | ||
fields: defaultColumns._GraphQLConfig, | ||
}; | ||
const _PushStatusSchema = convertSchemaToAdapterSchema( | ||
injectDefaultSchema({ | ||
className: '_PushStatus', | ||
fields: {}, | ||
classLevelPermissions: {}, | ||
}) | ||
); | ||
const _JobStatusSchema = convertSchemaToAdapterSchema( | ||
injectDefaultSchema({ | ||
className: '_JobStatus', | ||
fields: {}, | ||
classLevelPermissions: {}, | ||
}) | ||
); | ||
const _JobScheduleSchema = convertSchemaToAdapterSchema( | ||
injectDefaultSchema({ | ||
className: '_JobSchedule', | ||
fields: {}, | ||
classLevelPermissions: {}, | ||
}) | ||
); | ||
const _AudienceSchema = convertSchemaToAdapterSchema( | ||
injectDefaultSchema({ | ||
className: '_Audience', | ||
fields: defaultColumns._Audience, | ||
classLevelPermissions: {}, | ||
}) | ||
); | ||
const _PushStatusSchema = convertSchemaToAdapterSchema({ | ||
className: '_PushStatus', | ||
fields: {}, | ||
classLevelPermissions: {}, | ||
}); | ||
const _JobStatusSchema = convertSchemaToAdapterSchema({ | ||
className: '_JobStatus', | ||
fields: {}, | ||
classLevelPermissions: {}, | ||
}); | ||
const _JobScheduleSchema = convertSchemaToAdapterSchema({ | ||
className: '_JobSchedule', | ||
fields: {}, | ||
classLevelPermissions: {}, | ||
}); | ||
const _AudienceSchema = convertSchemaToAdapterSchema({ | ||
className: '_Audience', | ||
fields: defaultColumns._Audience, | ||
classLevelPermissions: {}, | ||
}); | ||
const VolatileClassesSchemas = [ | ||
_HooksSchema, | ||
_JobStatusSchema, | ||
|
@@ -651,6 +657,13 @@ const typeToString = (type: SchemaField | string): string => { | |
return `${type.type}`; | ||
}; | ||
|
||
const paramsAreEquals = (indexA, indexB) => { | ||
const keysIndexA = Object.keys(indexA); | ||
const keysIndexB = Object.keys(indexB); | ||
if (keysIndexA.length !== keysIndexB.length) return false; | ||
return keysIndexA.every(k => indexA[k] === indexB[k]); | ||
}; | ||
|
||
// Stores the entire schema of the app in a weird hybrid format somewhere between | ||
// the mongo format and the Parse format. Soon, this will all be Parse format. | ||
export default class SchemaController { | ||
|
@@ -813,6 +826,146 @@ export default class SchemaController { | |
}); | ||
} | ||
|
||
async loadSchemas(localSchemas, database: DatabaseController) { | ||
const cloudSchemas = await this.getAllClasses({ clearCache: true }); | ||
// We do not check classes to delete, developer need to delete manually classes | ||
// to avoid data loss during auto migration | ||
await Promise.all( | ||
localSchemas.map(async localSchema => { | ||
const cloudSchema = cloudSchemas.find( | ||
({ className }) => className === localSchema.className | ||
); | ||
if (cloudSchema) { | ||
await this.migrateClass(localSchema, cloudSchema, database); | ||
} else { | ||
if (!localSchema.className) | ||
throw new Parse.Error( | ||
500, | ||
`className is undefined on a schema. Check your schemas objects.` | ||
); | ||
await this.addClassIfNotExists( | ||
localSchema.className, | ||
localSchema.fields, | ||
localSchema.classLevelPermissions, | ||
localSchema.indexes | ||
); | ||
} | ||
}) | ||
); | ||
} | ||
|
||
async migrateClass(localSchema, cloudSchema, database: DatabaseController) { | ||
localSchema.fields = localSchema.fields || {}; | ||
localSchema.indexes = localSchema.indexes || {}; | ||
localSchema.classLevelPermissions = localSchema.classLevelPermissions || {}; | ||
|
||
const fieldsToAdd = {}; | ||
const fieldsToDelete = {}; | ||
const indexesToAdd = {}; | ||
const indexesToDelete = {}; | ||
|
||
const isNotDefaultField = fieldName => | ||
!defaultColumns['_Default'][fieldName] || | ||
(defaultColumns[localSchema.className] && | ||
!defaultColumns[localSchema.className][fieldName]); | ||
|
||
Object.keys(localSchema.fields) | ||
.filter(fieldName => isNotDefaultField(fieldName)) | ||
.forEach(fieldName => { | ||
if (!cloudSchema.fields[fieldName]) | ||
fieldsToAdd[fieldName] = localSchema.fields[fieldName]; | ||
}); | ||
|
||
Object.keys(cloudSchema.fields) | ||
.filter(fieldName => isNotDefaultField(fieldName)) | ||
.map(async 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. assuming accidental use of |
||
if (!localSchema.fields[fieldName]) { | ||
fieldsToDelete[fieldName] = { __op: 'Delete' }; | ||
return; | ||
} | ||
if ( | ||
!paramsAreEquals( | ||
cloudSchema.fields[fieldName], | ||
localSchema.fields[fieldName] | ||
) | ||
) { | ||
fieldsToDelete[fieldName] = { __op: 'Delete' }; | ||
fieldsToAdd[fieldName] = localSchema.fields[fieldName]; | ||
} | ||
}); | ||
|
||
const isNotDefaultIndex = indexName => | ||
!defaultIndexes['_Default'][indexName] || | ||
(defaultIndexes[localSchema.className] && | ||
!defaultIndexes[localSchema.className][indexName]); | ||
|
||
Object.keys(localSchema.indexes) | ||
.filter(indexName => isNotDefaultIndex(indexName)) | ||
.forEach(indexName => { | ||
if (!cloudSchema.indexes[indexName]) | ||
indexesToAdd[indexName] = localSchema.indexes[indexName]; | ||
}); | ||
|
||
Object.keys(cloudSchema.indexes) | ||
.filter(indexName => isNotDefaultIndex(indexName)) | ||
.map(async indexName => { | ||
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. Same as previous comment on |
||
if (!localSchema.indexes[indexName]) { | ||
indexesToDelete[indexName] = { __op: 'Delete' }; | ||
return; | ||
} | ||
if ( | ||
!paramsAreEquals( | ||
cloudSchema.indexes[indexName], | ||
localSchema.indexes[indexName] | ||
) | ||
) { | ||
indexesToDelete[indexName] = { __op: 'Delete' }; | ||
indexesToAdd[indexName] = localSchema.indexes[indexName]; | ||
} | ||
}); | ||
const CLPs = { | ||
...localSchema.classLevelPermissions, | ||
// Block add field feature when developers use parse with schemas | ||
// to avoid auto migration issues across SDKs | ||
addField: {}, | ||
}; | ||
// Concurrent migrations can lead to errors | ||
try { | ||
await this.updateClass( | ||
localSchema.className, | ||
fieldsToDelete, | ||
CLPs, | ||
indexesToDelete, | ||
database | ||
); | ||
} catch (e) { | ||
setTimeout(async () => { | ||
try { | ||
await this.migrateClass(localSchema, cloudSchema, database); | ||
} catch (e) { | ||
logger.error('Error occured during migration deletion time.'); | ||
} | ||
}, 15000); | ||
} | ||
try { | ||
await this.updateClass( | ||
localSchema.className, | ||
fieldsToAdd, | ||
CLPs, | ||
indexesToAdd, | ||
database | ||
); | ||
} catch (e) { | ||
setTimeout(async () => { | ||
try { | ||
await this.migrateClass(localSchema, cloudSchema, database); | ||
} catch (e) { | ||
logger.error('Error occured during migration addition.'); | ||
} | ||
}, 15000); | ||
} | ||
} | ||
|
||
updateClass( | ||
className: string, | ||
submittedFields: SchemaFields, | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I think you should return this and lose the async.
i.e.
return Promise.all(...