Skip to content
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

Feat/migration service #1814

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 0 additions & 155 deletions .idems_app/deployments/default.json

This file was deleted.

5 changes: 5 additions & 0 deletions .idems_app/deployments/plh_debug/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@ config.app_config.APP_SKINS.defaultSkinName = SKINS.debug.name;
// Limit available skins to only include debug skin, to force this skin to be applied on init
config.app_config.APP_SKINS.available = [SKINS.debug];

config.app_config.MIGRATION_ACTIONS={
enabled:true,
dataListName:'app_version_actions'
}

export default config;
4 changes: 2 additions & 2 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ android {
applicationId "international.idems.plh_teens"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 16011
versionName "0.16.11"
versionCode 16012
versionName "0.16.12"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
36 changes: 36 additions & 0 deletions documentation/docs/authors/migrations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Migrations

`_app_version` and `_app_version_code`

`_app_version_previous` and `_app_version_code_previous`


A specific data list can be enabled
```ts
config.app_config.MIGRATION_ACTIONS={
enabled:true,
dataListName:'app_version_actions'
}
```

By default the migrations will be processed in order of ID, therefore it is recommended to name systematically, e.g.

| id | action_list | condition_list| comment |
|--- | --- | --- | --- |
|001 | `trigger \| set_field : my_field : value_1` | | |
|002 | `trigger \| set_field : my_field : value_2` | | |

Each action will run exactly once if not previously run. This includes new app users, who will run all migrations on first load.

If this behaviour is not desirable additional conditions can be used to limit migrations applied to new users, or users upgrading from a specific version number

| id | action_list | condition_list| comment |
|--- | --- | --- | --- |
|002 | `trigger \| set_field : my_field : value_3` | `+@fields._app_version_code_previous < 0.16.12` | |

| id | action_list | condition_list| comment |
|--- | --- | --- | --- |
|002 | `trigger \| set_field : my_field : value_3` | `+@fields._app_version_code_previous < 0.16.12` | |

:::tip
If using fields like `_app_version_code` and `_app_version_code_previous` you should still force interpretation as a number as their default string representation would assume `"120" > "1100"` (per-character comparison)
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "frontend",
"version": "0.16.11",
"version": "0.16.12",
"author": "IDEMS International",
"license": "See LICENSE",
"homepage": "https://idems.international/",
Expand Down
11 changes: 10 additions & 1 deletion packages/data-models/appConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ const APP_FIELDS = {
SERVER_SYNC_LATEST: `${FIELD_PREFIX}._server_sync_latest`,
APP_LANGUAGE: `${FIELD_PREFIX}._app_language`,
DEPLOYMENT_NAME: `${FIELD_PREFIX}._deployment_name`,
APP_VERSION: `${FIELD_PREFIX}._app_version`,
/** Name given to app version, e.g 0.16.10 */
APP_VERSION_NAME: `${FIELD_PREFIX}._app_version`,
/** Number given to app version, e.g 16010 */
APP_VERSION_CODE: `${FIELD_PREFIX}._app_version_code`,
APP_AUTH_USER: `${FIELD_PREFIX}._app_auth_user`,
APP_SKIN: `${FIELD_PREFIX}._app_skin`,
APP_THEME: `${FIELD_PREFIX}._app_theme`,
Expand Down Expand Up @@ -176,6 +179,11 @@ const FEEDBACK_MODULE_DEFAULTS = {
selected_text_field: "_feedback_selected_text",
};

const MIGRATION_ACTIONS = {
enabled: false,
dataListName: "app_version_actions",
};

const APP_CONFIG = {
APP_FIELDS,
APP_HEADER_DEFAULTS,
Expand All @@ -191,6 +199,7 @@ const APP_CONFIG = {
DYNAMIC_PREFIXES,
FEEDBACK_MODULE_DEFAULTS,
FIELD_PREFIX,
MIGRATION_ACTIONS,
NOTIFICATIONS_SYNC_FREQUENCY_MS,
NOTIFICATION_DEFAULTS,
SERVER_SYNC_FREQUENCY_MS,
Expand Down
2 changes: 1 addition & 1 deletion packages/scripts/src/commands/deployment/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { DEPLOYMENTS_PATH } from "../../paths";
import { loadDeploymentJson } from "./utils";

/** Adjust config version to force new config set */
export const DEPLOYMENT_CONFIG_VERSION = 2.2;
export const DEPLOYMENT_CONFIG_VERSION = 20230228.0;

export interface IDeploymentConfigJson extends IDeploymentConfig {
_workspace_path: string;
Expand Down
2 changes: 1 addition & 1 deletion packages/scripts/src/commands/deployment/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fs, { readdirSync, statSync } from "fs-extra";
import fs, { statSync } from "fs-extra";
import path from "path";
import { Logger } from "../../utils";
import chalk from "chalk";
Expand Down
29 changes: 19 additions & 10 deletions packages/scripts/src/commands/version.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import * as fs from "fs-extra";
import { Command } from "commander";
import inquirer from "inquirer";
import { APP_BUILD_GRADLE_PATH, MAIN_PACKAGE_PATH } from "../paths";
import { APP_BUILD_GRADLE_PATH, MAIN_PACKAGE_PATH, SRC_ENV_DIR } from "../paths";
import { resolve } from "path";

/***************************************************************************************
* CLI
Expand All @@ -22,21 +23,22 @@ export default program.description("Set app version").action(async (options: IPr
* package.json version and also assigning to android version codes
*/
async function version(options: IProgramOptions) {
const oldVersion = fs.readJSONSync(MAIN_PACKAGE_PATH).version;
const newVersion = await promptNewVersion(oldVersion);
updatePackageJson(newVersion);
updateGradleBuild(newVersion);
const oldVersionName = fs.readJSONSync(MAIN_PACKAGE_PATH).version;
const newVersionName = await promptNewVersion(oldVersionName);
const newVersionCode = _generateVersionCode(newVersionName);
updatePackageJson(newVersionName);
updateGradleBuild(newVersionName, newVersionCode);
updateEnvVersion(newVersionName, newVersionCode);
}

function updateGradleBuild(newVersionName: string) {
function updateGradleBuild(versionName: string, versionCode: number) {
let gradleBuildFile = fs.readFileSync(APP_BUILD_GRADLE_PATH, {
encoding: "utf-8",
});
const newVersionCode = _generateVersionCode(newVersionName);
gradleBuildFile = gradleBuildFile.replace(/versionCode [0-9]+/g, `versionCode ${newVersionCode}`);
gradleBuildFile = gradleBuildFile.replace(/versionCode [0-9]+/g, `versionCode ${versionCode}`);
gradleBuildFile = gradleBuildFile.replace(
/versionName "[0-9]+\.[0-9]+\.[0-9]+"/g,
`versionName "${newVersionName}"`
`versionName "${versionName}"`
);
fs.writeFileSync(APP_BUILD_GRADLE_PATH, gradleBuildFile, { encoding: "utf-8" });
}
Expand All @@ -62,9 +64,16 @@ async function promptNewVersion(currentVersion: string) {
return version;
}

/** Write version name and code as export to src/environments/version.ts */
function updateEnvVersion(name: string, code: number) {
const versionTSPath = resolve(SRC_ENV_DIR, "version.ts");
const versionTSContents = `export const APP_VERSION = { name: "${name}", code: ${code} }\n`;
fs.writeFileSync(versionTSPath, versionTSContents);
}

// 2.4.1 => 2004001
// 2.40.1 => 2040001
function _generateVersionCode(versionName: string) {
const v = versionName.split(".");
return `${Number(v[0]) * 1000000 + Number(v[1]) * 1000 + Number(v[2])}`;
return Number(v[0]) * 1000000 + Number(v[1]) * 1000 + Number(v[2]);
}
1 change: 1 addition & 0 deletions packages/scripts/src/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const SCRIPTS_LOGS_DIR = path.join(SCRIPTS_WORKSPACE_PATH, "logs");
export const DATA_MODELS_WORKSPACE_PATH = path.join(ROOT_DIR, "packages/data-models");

export const SRC_ASSETS_PATH = path.join(ROOT_DIR, "src", "assets");
export const SRC_ENV_DIR = path.join(ROOT_DIR, "src", "environments");

export const CONFIG_FOLDER_PATH = path.join(SCRIPTS_WORKSPACE_PATH, "config");
export const PRIVATE_KEY_PATH = path.join(CONFIG_FOLDER_PATH, "private.key");
Expand Down
2 changes: 1 addition & 1 deletion src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<ion-toolbar color="primary">
<ion-title>{{ sideMenuDefaults.title }}</ion-title>
<div class="app-version" slot="end">
<span *ngIf="sideMenuDefaults.should_show_version">{{ APP_VERSION }} </span>
<span *ngIf="sideMenuDefaults.should_show_version">{{ APP_VERSION.name }} </span>
<span *ngIf="sideMenuDefaults.should_show_deployment_name" style="margin-left: 16px"
>({{ DEPLOYMENT_NAME }})</span
>
Expand Down
Loading