FEATURE: Theme settings migrations (#24071)

This commit introduces a new feature that allows theme developers to manage the transformation of theme settings over time. Similar to Rails migrations, the theme settings migration system enables developers to write and execute migrations for theme settings, ensuring a smooth transition when changes are required in the format or structure of setting values.

Example use cases for the theme settings migration system:

1. Renaming a theme setting.

2. Changing the data type of a theme setting (e.g., transforming a string setting containing comma-separated values into a proper list setting).

3. Altering the format of data stored in a theme setting.

All of these use cases and more are now possible while preserving theme setting values for sites that have already modified their theme settings.

Usage:

1. Create a top-level directory called `migrations` in your theme/component, and then within the `migrations` directory create another directory called `settings`.

2. Inside the `migrations/settings` directory, create a JavaScript file using the format `XXXX-some-name.js`, where `XXXX` is a unique 4-digit number, and `some-name` is a descriptor of your choice that describes the migration.

3. Within the JavaScript file, define and export (as the default) a function called `migrate`. This function will receive a `Map` object and must also return a `Map` object (it's acceptable to return the same `Map` object that the function received).

4. The `Map` object received by the `migrate` function will include settings that have been overridden or changed by site administrators. Settings that have never been changed from the default will not be included.

5. The keys and values contained in the `Map` object that the `migrate` function returns will replace all the currently changed settings of the theme.

6. Migrations are executed in numerical order based on the XXXX segment in the migration filenames. For instance, `0001-some-migration.js` will be executed before `0002-another-migration.js`.

Here's a complete example migration script that renames a setting from `setting_with_old_name` to `setting_with_new_name`:

```js
// File name: 0001-rename-setting.js

export default function migrate(settings) {
  if (settings.has("setting_with_old_name")) {
    settings.set("setting_with_new_name", settings.get("setting_with_old_name"));
  }
  return settings;
}
```

Internal topic: t/109980
This commit is contained in:
Osama Sayegh
2023-11-02 08:10:15 +03:00
committed by GitHub
parent d50fccfcaf
commit 3cadd6769e
21 changed files with 1292 additions and 187 deletions

View File

@ -160,6 +160,8 @@ class Admin::ThemesController < Admin::AdminController
render_json_error I18n.t("themes.import_error.unknown_file_type"),
status: :unprocessable_entity
end
rescue Theme::SettingsMigrationError => err
render_json_error err.message
end
def index
@ -226,10 +228,14 @@ class Admin::ThemesController < Admin::AdminController
@theme.remote_theme.update_remote_version if params[:theme][:remote_check]
@theme.remote_theme.update_from_remote if params[:theme][:remote_update]
if params[:theme][:remote_update]
@theme.remote_theme.update_from_remote(raise_if_theme_save_fails: false)
else
@theme.save
end
respond_to do |format|
if @theme.save
if @theme.errors.blank?
update_default_theme
@theme = Theme.include_relations.find(@theme.id)
@ -253,6 +259,8 @@ class Admin::ThemesController < Admin::AdminController
end
rescue RemoteTheme::ImportError => e
render_json_error e.message
rescue Theme::SettingsMigrationError => e
render_json_error e.message
end
def destroy