Skip to content

Commit a1657e9

Browse files
Merge pull request #1244 from merico-dev/1243-add-dashboard-content-migration-upon-creation-and-update
update content schema on create/update
2 parents 3cee1bd + d276b56 commit a1657e9

File tree

9 files changed

+239
-220
lines changed

9 files changed

+239
-220
lines changed

api/src/dashboard_migration/index.ts

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import { dashboardDataSource } from '../data_sources/dashboard';
22
import logger from 'npmlog';
33
import _ from 'lodash';
4-
import { Repository } from 'typeorm';
54
import DashboardContent from '../models/dashboard_content';
65
import DashboardContentChangelog from '../models/dashboard_content_changelog';
76
import { DashboardContentChangelogService } from '../services/dashboard_content_changelog.service';
87

98
// NOTE: Keep versions in order
10-
const versions = [
9+
export const versions = [
1110
'2.0.0',
1211
'2.1.0',
1312
'4.5.0',
@@ -49,11 +48,7 @@ async function findHandler(currentVersion: string | undefined) {
4948
return import(`./handlers/${nextVersion}`);
5049
}
5150

52-
async function migrateOneDashboardContent(
53-
dashboardContent: DashboardContent,
54-
dashboardContentChangelogRepo: Repository<DashboardContentChangelog>,
55-
dashboardContentRepo: Repository<DashboardContent>,
56-
) {
51+
export async function migrateOneDashboardContent(dashboardContent: DashboardContent): Promise<boolean> {
5752
try {
5853
const version = dashboardContent.content.version as string;
5954
if (version && !versions.includes(version)) {
@@ -63,20 +58,7 @@ async function migrateOneDashboardContent(
6358
}
6459
let handler = await findHandler(version);
6560
while (handler) {
66-
const originalDashboardContent = _.cloneDeep(dashboardContent);
6761
dashboardContent.content = handler.main(dashboardContent.content);
68-
const updatedDashboardContent = await dashboardContentRepo.save(dashboardContent);
69-
const diff = await DashboardContentChangelogService.createChangelog(
70-
originalDashboardContent,
71-
_.cloneDeep(updatedDashboardContent),
72-
);
73-
if (diff) {
74-
const changelog = new DashboardContentChangelog();
75-
changelog.dashboard_content_id = dashboardContent.id;
76-
changelog.diff = diff;
77-
await dashboardContentChangelogRepo.save(changelog);
78-
}
79-
logger.info(`MIGRATED ${dashboardContent.id} TO VERSION ${dashboardContent.content.version}`);
8062
handler = await findHandler(dashboardContent.content.version as string);
8163
}
8264
} catch (error) {
@@ -86,10 +68,12 @@ async function migrateOneDashboardContent(
8668
*/
8769
logger.error(`error migrating dashboard content. ID: ${dashboardContent.id} name: ${dashboardContent.name}`);
8870
logger.error(error.message);
71+
return false;
8972
}
73+
return true;
9074
}
9175

92-
async function main() {
76+
export async function migrateDashboardContents() {
9377
logger.info('STARTING MIGRATION OF DASHBOARD CONTENTS');
9478
try {
9579
if (!dashboardDataSource.isInitialized) {
@@ -100,7 +84,20 @@ async function main() {
10084
const dashboardContents = await dashboardContentRepo.find();
10185

10286
for (const dashboardContent of dashboardContents) {
103-
migrateOneDashboardContent(dashboardContent, dashboardContentChangelogRepo, dashboardContentRepo);
87+
const originalDashboardContent = _.cloneDeep(dashboardContent);
88+
await migrateOneDashboardContent(dashboardContent);
89+
const updatedDashboardContent = await dashboardContentRepo.save(dashboardContent);
90+
const diff = await DashboardContentChangelogService.createChangelog(
91+
originalDashboardContent,
92+
_.cloneDeep(updatedDashboardContent),
93+
);
94+
if (diff) {
95+
const changelog = new DashboardContentChangelog();
96+
changelog.dashboard_content_id = dashboardContent.id;
97+
changelog.diff = diff;
98+
await dashboardContentChangelogRepo.save(changelog);
99+
}
100+
logger.info(`MIGRATED ${dashboardContent.id} TO VERSION ${dashboardContent.content.version}`);
104101
}
105102
} catch (error) {
106103
logger.error('error migrating dashboard contents');
@@ -109,5 +106,3 @@ async function main() {
109106
}
110107
logger.info('MIGRATION OF DASHBOARD CONTENTS FINISHED');
111108
}
112-
113-
main();

api/src/locales/en.json

Lines changed: 81 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,82 @@
11
{
2-
"ACCOUNT_NO_DELETE_SELF": "Can not delete own account",
3-
"ACCOUNT_NO_EDIT_SELF": "Can not edit own account. Use /account/update instead",
4-
"ACCOUNT_NO_ADD_SIMILAR_OR_HIGHER_PRIVILEGES": "Can not add account with similar or higher privileges",
5-
"ACCOUNT_INVALID_CREDENTIALS": "Invalid credentials",
6-
"ACCOUNT_NO_EDIT_SUPERADMIN": "Can not edit superadmin details",
7-
"ACCOUNT_NO_EDIT_SIMILAR_OR_HIGHER_PRIVILEGES": "Can not edit account with similar or higher privileges than own account",
8-
"ACCOUNT_NO_CHANGE_SIMILAR_OR_HIGHER_PRIVILEGES": "Can not change account privileges to similar or higher than own account",
9-
"ACCOUNT_NO_EMPTY_RESET_PASSWORD": "Must provide new_password when reset_password is true",
10-
"ACCOUNT_NO_DELETE_SIMILAR_OR_HIGHER_PRIVILEGES": "Can not delete account with similar or higher privileges than own account",
11-
"ACCOUNT_NAME_EMAIL_ALREADY_EXISTS": "An account with that name or email already exists",
12-
"ACCOUNT_PWD_LENGTH_SHOULD_BE_GRATER_THAN_8": "Password must be at least 8 characters long",
13-
"ACCOUNT_PWD_MISMATCH": "Password is incorrect",
14-
"ACCOUNT_NOT_FOUND": "Account not found",
15-
"ACCOUNT_NO_DELETE_SUPERADMIN": "Can not delete superadmin account",
16-
"DATASOURCE_HTTP_REQUIRED_FIELDS": "Pre processing and post processing are both required for HTTP datasource",
17-
"DATASOURCE_DB_REQUIRED_FIELDS": "Mysql|Postgresql config must contain [port, username, password, database]",
18-
"DATASOURCE_RENAME_SAME_KEY": "New key is the same as the old one",
19-
"DATASOURCE_NO_DELETE_PRESET": "Can not delete preset datasources",
20-
"DATASOURCE_CONNECTION_TEST_FAILED": "Testing datasource connection failed",
21-
"DATASOURCE_TYPE_KEY_ALREADY_EXISTS": "A data source with that type and key already exists",
22-
"DATASOURCE_ONLY_HTTP_IS_EDITABLE": "Only HTTP data source is editable",
23-
"DATASOURCE_MISSING": "Several datasources were not found",
24-
"AUTH_NOT_ENABLED": "Authentication system is not enabled",
25-
"AUTH_MUST_BEARER": "Must authenticate with bearer token",
26-
"APIKEY_NO_DELETE_PRESET": "Preset apikey can not be deleted",
27-
"APIKEY_NAME_ALREADY_EXISTS": "An ApiKey with that name already exists",
28-
"CONFIG_REQUIRES_AUTHENTICATION": "Must be authenticated for this config",
29-
"CONFIG_INCORRECT_VALUE": "Incorrect config value",
30-
"CONFIG_INSUFFICIENT_PRIVILEGES": "Insufficient privileges for this config",
31-
"DASHBOARD_EDIT_REQUIRES_SUPERADMIN": "Only superadmin can edit preset dashboards",
32-
"DASHBOARD_DELETE_PRESET_REQUIRES_SUPERADMIN": "Only superadmin can delete preset dashboards",
33-
"DASHBOARD_NAME_ALREADY_EXISTS": "A dashboard with that name already exists",
34-
"UNAUTHORIZED": "Not authenticated",
35-
"FORBIDDEN": "Access denied",
36-
"DASHBOARD_PERMISSION_NO_OWNER": "Dashboard has no owner. Please assign a new owner before modifying permissions",
37-
"DASHBOARD_PERMISSION_FORBIDDEN": "Insufficient privileges for this dashboard",
38-
"DASHBOARD_OWNER_INSUFFICIENT_PRIVILEGES": "Resource has insufficient privileges to take dashboard ownership",
39-
"DASHBOARD_PERMISSION_MISSING_APIKEY": "Several apikeys were not found",
40-
"DASHBOARD_PERMISSION_MISSING_ACCOUNT": "Several accounts were not found",
41-
"DASHBOARD_CONTENT_NAME_ALREADY_EXISTS": "A dashboard content with that name already exists",
42-
"DASHBOARD_CONTENT_EDIT_REQUIRES_SUPERADMIN": "Only superadmin can edit preset dashboard contents",
43-
"DASHBOARD_CONTENT_DELETE_REQUIRES_SUPERADMIN": "Only superadmin can delete preset dashboard contents",
44-
"DASHBOARD_CONTENT_DOES_NOT_EXIST": "That dashboard content does not exist",
45-
"CUSTOM_FUNCTION_ALREADY_EXISTS": "A custom function with that id already exists",
46-
"CUSTOM_FUNCTION_NO_EDIT_PRESET": "Preset custom functions can not be edited",
47-
"CUSTOM_FUNCTION_NO_DELETE_PRESET": "Preset custom functions can not be deleted",
48-
"SQL_SNIPPET_ALREADY_EXISTS": "A sql snippet with that id already exists",
49-
"SQL_SNIPPET_NO_EDIT_PRESET": "Preset sql snippets can not be edited",
50-
"SQL_SNIPPET_NO_DELETE_PRESET": "Preset sql snippets can not be deleted",
51-
"ROLE_NOT_FOUND": "Role not found",
52-
"ROLE_ALREADY_EXISTS": "Role already exists",
53-
"CONFIG_DESCRIPTION_LANG": "Configure the locality of server responses",
54-
"CONFIG_DESCRIPTION_WEBSITE_SETTINGS": "Configurations for the website logo and favicon",
55-
"CONFIG_DESCRIPTION_QUERY_CACHE_ENABLED": "Configure whether to cache query results",
56-
"CONFIG_DESCRIPTION_QUERY_CACHE_EXPIRE_TIME": "Configure the time in seconds before query cache expires",
57-
"ROLE_PERMISSION_DATASOURCE_VIEW": "Allows viewing datasources",
58-
"ROLE_PERMISSION_DATASOURCE_MANAGE": "Allows creating, editing, deleting datasources",
59-
"ROLE_PERMISSION_DASHBOARD_VIEW": "Allows viewing dashboards (including contents)",
60-
"ROLE_PERMISSION_DASHBOARD_MANAGE": "Allows creating, editing, deleting dashboards, and viewing changelogs",
61-
"ROLE_PERMISSION_ACCOUNT_LIST": "Allows viewing of accounts",
62-
"ROLE_PERMISSION_ACCOUNT_LOGIN": "Allows logging in",
63-
"ROLE_PERMISSION_ACCOUNT_UPDATE": "Allows updating own account",
64-
"ROLE_PERMISSION_ACCOUNT_CHANGEPASSWORD": "Allows changing account password",
65-
"ROLE_PERMISSION_ACCOUNT_MANAGE": "Allows creating, editing, deleting accounts",
66-
"ROLE_PERMISSION_APIKEY_LIST": "Allows viewing of ApiKeys",
67-
"ROLE_PERMISSION_APIKEY_MANAGE": "Allows creating and deleting ApiKeys",
68-
"ROLE_PERMISSION_ROLE_MANAGE": "Allows creating, editing, deleting roles",
69-
"ROLE_PERMISSION_CONFIG_SET_LANG": "Allows updating lang",
70-
"ROLE_PERMISSION_CONFIG_GET_WEBSITE_SETTINGS": "Allows retrieving website settings",
71-
"ROLE_PERMISSION_CONFIG_SET_WEBSITE_SETTINGS": "Allows updating website settings",
72-
"ROLE_PERMISSION_CONFIG_SET_QUERY_CACHE_ENABLED": "Allows updating the query cache enabled status",
73-
"ROLE_PERMISSION_CONFIG_SET_QUERY_CACHE_EXPIRE_TIME": "Allows updating the query cache expire time",
74-
"ROLE_PERMISSION_CUSTOM_FUNCTION_VIEW": "Allows viewing custom functions",
75-
"ROLE_PERMISSION_CUSTOM_FUNCTION_MANAGE": "Allows creating, editing, deleting custom functions",
76-
"ROLE_PERMISSION_SQL_SNIPPET_VIEW": "Allows viewing sql snippets",
77-
"ROLE_PERMISSION_SQL_SNIPPET_MANAGE": "Allows creating, editing, deleting sql snippets",
78-
"ROLE_PERMISSION_PRESET": "Allows modification of preset assets",
79-
"QUERY_ID_NOT_FOUND": "A Query with that ID does not exist",
80-
"QUERY_PARSING_ERROR": "Error parsing query"
81-
}
2+
"ACCOUNT_NO_DELETE_SELF": "Can not delete own account",
3+
"ACCOUNT_NO_EDIT_SELF": "Can not edit own account. Use /account/update instead",
4+
"ACCOUNT_NO_ADD_SIMILAR_OR_HIGHER_PRIVILEGES": "Can not add account with similar or higher privileges",
5+
"ACCOUNT_INVALID_CREDENTIALS": "Invalid credentials",
6+
"ACCOUNT_NO_EDIT_SUPERADMIN": "Can not edit superadmin details",
7+
"ACCOUNT_NO_EDIT_SIMILAR_OR_HIGHER_PRIVILEGES": "Can not edit account with similar or higher privileges than own account",
8+
"ACCOUNT_NO_CHANGE_SIMILAR_OR_HIGHER_PRIVILEGES": "Can not change account privileges to similar or higher than own account",
9+
"ACCOUNT_NO_EMPTY_RESET_PASSWORD": "Must provide new_password when reset_password is true",
10+
"ACCOUNT_NO_DELETE_SIMILAR_OR_HIGHER_PRIVILEGES": "Can not delete account with similar or higher privileges than own account",
11+
"ACCOUNT_NAME_EMAIL_ALREADY_EXISTS": "An account with that name or email already exists",
12+
"ACCOUNT_PWD_LENGTH_SHOULD_BE_GRATER_THAN_8": "Password must be at least 8 characters long",
13+
"ACCOUNT_PWD_MISMATCH": "Password is incorrect",
14+
"ACCOUNT_NOT_FOUND": "Account not found",
15+
"ACCOUNT_NO_DELETE_SUPERADMIN": "Can not delete superadmin account",
16+
"DATASOURCE_HTTP_REQUIRED_FIELDS": "Pre processing and post processing are both required for HTTP datasource",
17+
"DATASOURCE_DB_REQUIRED_FIELDS": "Mysql|Postgresql config must contain [port, username, password, database]",
18+
"DATASOURCE_RENAME_SAME_KEY": "New key is the same as the old one",
19+
"DATASOURCE_NO_DELETE_PRESET": "Can not delete preset datasources",
20+
"DATASOURCE_CONNECTION_TEST_FAILED": "Testing datasource connection failed",
21+
"DATASOURCE_TYPE_KEY_ALREADY_EXISTS": "A data source with that type and key already exists",
22+
"DATASOURCE_ONLY_HTTP_IS_EDITABLE": "Only HTTP data source is editable",
23+
"DATASOURCE_MISSING": "Several datasources were not found",
24+
"AUTH_NOT_ENABLED": "Authentication system is not enabled",
25+
"AUTH_MUST_BEARER": "Must authenticate with bearer token",
26+
"APIKEY_NO_DELETE_PRESET": "Preset apikey can not be deleted",
27+
"APIKEY_NAME_ALREADY_EXISTS": "An ApiKey with that name already exists",
28+
"CONFIG_REQUIRES_AUTHENTICATION": "Must be authenticated for this config",
29+
"CONFIG_INCORRECT_VALUE": "Incorrect config value",
30+
"CONFIG_INSUFFICIENT_PRIVILEGES": "Insufficient privileges for this config",
31+
"DASHBOARD_EDIT_REQUIRES_SUPERADMIN": "Only superadmin can edit preset dashboards",
32+
"DASHBOARD_DELETE_PRESET_REQUIRES_SUPERADMIN": "Only superadmin can delete preset dashboards",
33+
"DASHBOARD_NAME_ALREADY_EXISTS": "A dashboard with that name already exists",
34+
"UNAUTHORIZED": "Not authenticated",
35+
"FORBIDDEN": "Access denied",
36+
"DASHBOARD_PERMISSION_NO_OWNER": "Dashboard has no owner. Please assign a new owner before modifying permissions",
37+
"DASHBOARD_PERMISSION_FORBIDDEN": "Insufficient privileges for this dashboard",
38+
"DASHBOARD_OWNER_INSUFFICIENT_PRIVILEGES": "Resource has insufficient privileges to take dashboard ownership",
39+
"DASHBOARD_PERMISSION_MISSING_APIKEY": "Several apikeys were not found",
40+
"DASHBOARD_PERMISSION_MISSING_ACCOUNT": "Several accounts were not found",
41+
"DASHBOARD_CONTENT_NAME_ALREADY_EXISTS": "A dashboard content with that name already exists",
42+
"DASHBOARD_CONTENT_EDIT_REQUIRES_SUPERADMIN": "Only superadmin can edit preset dashboard contents",
43+
"DASHBOARD_CONTENT_DELETE_REQUIRES_SUPERADMIN": "Only superadmin can delete preset dashboard contents",
44+
"DASHBOARD_CONTENT_DOES_NOT_EXIST": "That dashboard content does not exist",
45+
"DASHBOARD_CONTENT_MIGRATION_FAILED": "Content could not be migrated to latest version",
46+
"CUSTOM_FUNCTION_ALREADY_EXISTS": "A custom function with that id already exists",
47+
"CUSTOM_FUNCTION_NO_EDIT_PRESET": "Preset custom functions can not be edited",
48+
"CUSTOM_FUNCTION_NO_DELETE_PRESET": "Preset custom functions can not be deleted",
49+
"SQL_SNIPPET_ALREADY_EXISTS": "A sql snippet with that id already exists",
50+
"SQL_SNIPPET_NO_EDIT_PRESET": "Preset sql snippets can not be edited",
51+
"SQL_SNIPPET_NO_DELETE_PRESET": "Preset sql snippets can not be deleted",
52+
"ROLE_NOT_FOUND": "Role not found",
53+
"ROLE_ALREADY_EXISTS": "Role already exists",
54+
"CONFIG_DESCRIPTION_LANG": "Configure the locality of server responses",
55+
"CONFIG_DESCRIPTION_WEBSITE_SETTINGS": "Configurations for the website logo and favicon",
56+
"CONFIG_DESCRIPTION_QUERY_CACHE_ENABLED": "Configure whether to cache query results",
57+
"CONFIG_DESCRIPTION_QUERY_CACHE_EXPIRE_TIME": "Configure the time in seconds before query cache expires",
58+
"ROLE_PERMISSION_DATASOURCE_VIEW": "Allows viewing datasources",
59+
"ROLE_PERMISSION_DATASOURCE_MANAGE": "Allows creating, editing, deleting datasources",
60+
"ROLE_PERMISSION_DASHBOARD_VIEW": "Allows viewing dashboards (including contents)",
61+
"ROLE_PERMISSION_DASHBOARD_MANAGE": "Allows creating, editing, deleting dashboards, and viewing changelogs",
62+
"ROLE_PERMISSION_ACCOUNT_LIST": "Allows viewing of accounts",
63+
"ROLE_PERMISSION_ACCOUNT_LOGIN": "Allows logging in",
64+
"ROLE_PERMISSION_ACCOUNT_UPDATE": "Allows updating own account",
65+
"ROLE_PERMISSION_ACCOUNT_CHANGEPASSWORD": "Allows changing account password",
66+
"ROLE_PERMISSION_ACCOUNT_MANAGE": "Allows creating, editing, deleting accounts",
67+
"ROLE_PERMISSION_APIKEY_LIST": "Allows viewing of ApiKeys",
68+
"ROLE_PERMISSION_APIKEY_MANAGE": "Allows creating and deleting ApiKeys",
69+
"ROLE_PERMISSION_ROLE_MANAGE": "Allows creating, editing, deleting roles",
70+
"ROLE_PERMISSION_CONFIG_SET_LANG": "Allows updating lang",
71+
"ROLE_PERMISSION_CONFIG_GET_WEBSITE_SETTINGS": "Allows retrieving website settings",
72+
"ROLE_PERMISSION_CONFIG_SET_WEBSITE_SETTINGS": "Allows updating website settings",
73+
"ROLE_PERMISSION_CONFIG_SET_QUERY_CACHE_ENABLED": "Allows updating the query cache enabled status",
74+
"ROLE_PERMISSION_CONFIG_SET_QUERY_CACHE_EXPIRE_TIME": "Allows updating the query cache expire time",
75+
"ROLE_PERMISSION_CUSTOM_FUNCTION_VIEW": "Allows viewing custom functions",
76+
"ROLE_PERMISSION_CUSTOM_FUNCTION_MANAGE": "Allows creating, editing, deleting custom functions",
77+
"ROLE_PERMISSION_SQL_SNIPPET_VIEW": "Allows viewing sql snippets",
78+
"ROLE_PERMISSION_SQL_SNIPPET_MANAGE": "Allows creating, editing, deleting sql snippets",
79+
"ROLE_PERMISSION_PRESET": "Allows modification of preset assets",
80+
"QUERY_ID_NOT_FOUND": "A Query with that ID does not exist",
81+
"QUERY_PARSING_ERROR": "Error parsing query"
82+
}

0 commit comments

Comments
 (0)