[IMPLEMENTED] Apply default values for new mandatory fields

We have had a problem with using the CLI for transferring schema, and for upgrading Squidex, because we have managed to get our content in to an invalid state. This invalid state is easy to reproduce and is likely to happen to multiple people. It could be resolved by applying defaults to mandatory fields when they are undefined. The steps to reproduce the issue:

  1. Create a schema
  2. Create content based on that schema
  3. Update the schema to have a new field that is both mandatory and has a default value.
  4. Note all existing content is now invalid as far as they system is concerned as the defaults are not applied and it is mandatory. So, now you move the site content via CLI, you cannot upgrade Squidex, etc. until it is resolved.

You can resolve this manually but when there is a large amount of historical content this can be difficult to find elements that contain the now broken schema, and then change all of them.

I know the idea of applying defaults to old content was rejected ( Fields Default Values do not work - #6 by Vitali_Nikolenko) and I understand that but I think in the case of mandatory fields it would make sense to change this as there is no reason a mandatory field with a default value should ever legitimately have a value of undefined. Also, it is only in the mandatory case that Squidex features like the upgrade migration break.

Could you use scripts for that?

I don’t know. I see this in documentation for scripting:

You can create scripts that run whenever a content item is created, updated, deleted, queried or when the status changes (e.g. from Draft to Published).

In the case we are discussing we have only changed the schema. We haven’t necessarily queried for the content item at all. The operations that are breaking for us are sq sync and the rebuilding of content and schemas during upgrade. Would those actions trigger a content query that would trigger the script?

If you have not queried the content at all you can solve it with a query script. But I would probably write a migration script to fix that.

With the migration script is that something built into Squidex that I haven’t seen yet that we could plug in to? Or, do you mean write something separate outside of Squidex to query and update the content directly in the database?

I would just use the SDK for that (depending on your programming language), it can probably be done in a few lines of code. Don’t touch the database please :wink:

Ok - good advice :slight_smile: Thanks - will look into doing it with the SDK.

1 Like

Thought this sounded familiar! We had similar issue a while ago: Adding new defaulted field to schema does not update the contents of the schema

So plan is to disable any rules, update content to force default values for the required field, and then enable rules again?

It is a costly operation therefore I don’t want to make it adhoc. But I could integrate that into the CLI. Should not be that complicated.

1 Like

I have added new endpoints to the API to enforce the defaults. There is also CLI support for that.

sq contents enrich-defaults <my-schema>

But you need to update Squidex for that. I have considered to implement it without that, but I did not want to duplicate the default value logic.

We already have a basic task system in the API for backups. I think I will generalize this next year to allow other tasks as well. Then we can integrate some tasks into the UI.

2 Likes