From 3646b0b825556efcd0cb681b0eb07d481013a387 Mon Sep 17 00:00:00 2001 From: Hanzla Mateen Date: Fri, 16 Aug 2024 13:40:28 +0500 Subject: [PATCH] Changes to pass environment in metabase params (#10982) * Changes to pass environment in metabase params * Added migration --- .../metabase/metabase-setting.schema.ts | 2 + .../metabase-setting/metabase-setting.seed.ts | 1 + ...40816095000_metabase-environment-column.ts | 74 +++++++++++++++++++ .../metabase-url/metabase-url.hooks.ts | 5 +- 4 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 packages/server-core/src/integrations/metabase/metabase-setting/migrations/20240816095000_metabase-environment-column.ts diff --git a/packages/common/src/schemas/integrations/metabase/metabase-setting.schema.ts b/packages/common/src/schemas/integrations/metabase/metabase-setting.schema.ts index 4877d8a324..cf3f8063c1 100644 --- a/packages/common/src/schemas/integrations/metabase/metabase-setting.schema.ts +++ b/packages/common/src/schemas/integrations/metabase/metabase-setting.schema.ts @@ -40,6 +40,7 @@ export const metabaseSettingSchema = Type.Object( }), siteUrl: Type.String(), secretKey: Type.String(), + environment: Type.String(), crashDashboardId: Type.Optional(Type.String()), expiration: Type.Number(), createdAt: Type.String({ format: 'date-time' }), @@ -70,6 +71,7 @@ export const metabaseSettingQueryProperties = Type.Pick(metabaseSettingSchema, [ 'id', 'siteUrl', 'secretKey', + 'environment', 'crashDashboardId' ]) diff --git a/packages/server-core/src/integrations/metabase/metabase-setting/metabase-setting.seed.ts b/packages/server-core/src/integrations/metabase/metabase-setting/metabase-setting.seed.ts index bb830a8893..814be9d4c3 100644 --- a/packages/server-core/src/integrations/metabase/metabase-setting/metabase-setting.seed.ts +++ b/packages/server-core/src/integrations/metabase/metabase-setting/metabase-setting.seed.ts @@ -42,6 +42,7 @@ export async function seed(knex: Knex): Promise { { siteUrl: process.env.METABASE_SITE_URL!, secretKey: process.env.METABASE_SECRET_KEY!, + environment: process.env.METABASE_ENVIRONMENT!, crashDashboardId: process.env.METABASE_CRASH_DASHBOARD_ID!, expiration: isNaN(parseInt(process.env.METABASE_EXPIRATION!)) ? 10 : parseInt(process.env.METABASE_EXPIRATION!) } diff --git a/packages/server-core/src/integrations/metabase/metabase-setting/migrations/20240816095000_metabase-environment-column.ts b/packages/server-core/src/integrations/metabase/metabase-setting/migrations/20240816095000_metabase-environment-column.ts new file mode 100644 index 0000000000..391b52006f --- /dev/null +++ b/packages/server-core/src/integrations/metabase/metabase-setting/migrations/20240816095000_metabase-environment-column.ts @@ -0,0 +1,74 @@ +/* +CPAL-1.0 License + +The contents of this file are subject to the Common Public Attribution License +Version 1.0. (the "License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at +https://github.com/EtherealEngine/etherealengine/blob/dev/LICENSE. +The License is based on the Mozilla Public License Version 1.1, but Sections 14 +and 15 have been added to cover use of software over a computer network and +provide for limited attribution for the Original Developer. In addition, +Exhibit A has been modified to be consistent with Exhibit B. + +Software distributed under the License is distributed on an "AS IS" basis, +WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the +specific language governing rights and limitations under the License. + +The Original Code is Ethereal Engine. + +The Original Developer is the Initial Developer. The Initial Developer of the +Original Code is the Ethereal Engine team. + +All portions of the code written by the Ethereal Engine team are Copyright © 2021-2023 +Ethereal Engine. All Rights Reserved. +*/ + +import { metabaseSettingPath } from '@etherealengine/common/src/schemas/integrations/metabase/metabase-setting.schema' +import type { Knex } from 'knex' + +/** + * @param { import("knex").Knex } knex + * @returns { Promise } + */ +export async function up(knex: Knex): Promise { + await knex.raw('SET FOREIGN_KEY_CHECKS=0') + + const tableExists = await knex.schema.hasTable(metabaseSettingPath) + if (tableExists) { + const environmentExists = await knex.schema.hasColumn(metabaseSettingPath, 'environment') + if (environmentExists === false) { + await knex.schema.alterTable(metabaseSettingPath, async (table) => { + table.string('environment').nullable() + }) + + const metabaseSettings = await knex.table(metabaseSettingPath).first() + + if (metabaseSettings) { + await knex.table(metabaseSettingPath).update({ + environment: process.env.METABASE_ENVIRONMENT + }) + } + } + } + await knex.raw('SET FOREIGN_KEY_CHECKS=1') +} + +/** + * @param { import("knex").Knex } knex + * @returns { Promise } + */ +export async function down(knex: Knex): Promise { + await knex.raw('SET FOREIGN_KEY_CHECKS=0') + + const tableExists = await knex.schema.hasTable(metabaseSettingPath) + if (tableExists) { + const environmentExists = await knex.schema.hasColumn(metabaseSettingPath, 'environment') + if (environmentExists) { + await knex.schema.alterTable(metabaseSettingPath, async (table) => { + table.dropColumn('environment') + }) + } + } + + await knex.raw('SET FOREIGN_KEY_CHECKS=1') +} diff --git a/packages/server-core/src/integrations/metabase/metabase-url/metabase-url.hooks.ts b/packages/server-core/src/integrations/metabase/metabase-url/metabase-url.hooks.ts index 4f5c1f0333..69e1ea8800 100644 --- a/packages/server-core/src/integrations/metabase/metabase-url/metabase-url.hooks.ts +++ b/packages/server-core/src/integrations/metabase/metabase-url/metabase-url.hooks.ts @@ -47,6 +47,7 @@ export const metabaseCrashDashboard = async (context: HookContext