Guideline for handling schema and schema migration

I am using Parse Server to build our server, however, we are facing issues with the schema auto migration. Do we have any guidelines or recommendations when changing schema?

Currently whenever we change the schema, starting the server will auto running the migration. Is there a way that we can stop this auto migration and make the migration explicit?

I using my own migration scripts. I’m not using a parse server migration help code.

Hi,

Do you mind to share about your use case? I have a few questions for those cases

  1. I can stop the auto schema migration by throwing the error in beforeMigration. However, I am not sure if this is appropriate
  2. From the Parse’s perspective, I wonder why the schema migration is done automatically each time we start the server instead of making it an explicit step?
  3. Can you share how you use your own script to do schema migration?

Hi,
If you prepare a script for _SCHEMA collection’s that script should include a parse server scheme example.

for ex:

db.getCollection("_SCHEMA").updateOne({_id:"Location"},
{"$set":{"cities":"array","counties":"array"}})

If you run those basic scripts, when your parse server wakes up, it automatically migrates these changes. Then your Location collections’ cities and counties fields will be an array type.

So you can define your own schema scripts but should be proper with parse schema definition.

I predefine schema by myself.

export const Counter = {
    className: "Counter",
    fields: {
        field_Name: {type: 'String'},
        value: {type: 'Number'},
        class_Name: {type: 'String'},
    },

    classLevelPermissions: {
        find: {},
        count: {},
        get: {},
        update: {},
        create: {},
        delete: {},
        protectedFields: {},
    },
}

and use the parse.schema

var schemas = [Counter]
for (let i = 0; i < schemas.length; i++) {
        const className = schemas[i].className;
        const fileds = schemas[i].fields;
        const keys = Object.keys(fileds);
        logInfo("className: " + className);
        const schema = new Parse.Schema(className);

        let needCreate = true;
        try {
            const oldSchema = await schema.get();
            needCreate = false;
        } catch (e) {
            needCreate = true;
        }

        for (let j = 0; j < keys.length; j++) {
            const key = keys[j];
            const value = fileds[key];
            const type = value.type;
            const defaultValue = value.defaultValue;
            const targetClass = value.targetClass;
            if (type === "Number") {
                if (defaultValue) {
                    schema.addNumber(key, {defaultValue: defaultValue});
                } else {
                    schema.addNumber(key);
                }
            } else if (type === "Boolean") {
                if (defaultValue) {
                    schema.addBoolean(key, {defaultValue: defaultValue});
                } else {
                    schema.addBoolean(key);
                }
            } else if (type === "String") {
                if (defaultValue) {
                    schema.addString(key, {defaultValue: defaultValue});
                } else {
                    schema.addString(key);
                }
            } else if (type === "Date") {
                if (defaultValue) {
                    schema.addDate(key, {defaultValue: defaultValue});
                } else {
                    schema.addDate(key);
                }
            } else if (type === "Array") {
                if (defaultValue) {
                    schema.addArray(key, {defaultValue: defaultValue});
                } else {
                    schema.addArray(key);
                }
            } else if (type === "Pointer") {
                if (defaultValue) {
                    schema.addPointer(key, targetClass, {defaultValue: defaultValue});
                } else {
                    schema.addPointer(key, targetClass);
                }
            }
        }

        if (needCreate) {
            logInfo("create " + className)
            await schema.save();
            logInfo("create " + className + " success");
        } else {
            logInfo("update " + className);
            await schema.update();
            logInfo("update " + className + " success");
        }
    }