forked from tsai/budibase
35 changed files with 463 additions and 90 deletions
@ -0,0 +1,26 @@ |
|||
import { publishEvent } from "../events" |
|||
import { Event, VersionCheckedEvent, VersionChangeEvent } from "@budibase/types" |
|||
|
|||
export async function checked(version: string) { |
|||
const properties: VersionCheckedEvent = { |
|||
currentVersion: version, |
|||
} |
|||
await publishEvent(Event.VERSION_CHECKED, properties) |
|||
} |
|||
|
|||
export async function upgraded(from: string, to: string) { |
|||
const properties: VersionChangeEvent = { |
|||
from, |
|||
to, |
|||
} |
|||
|
|||
await publishEvent(Event.VERSION_UPGRADED, properties) |
|||
} |
|||
|
|||
export async function downgraded(from: string, to: string) { |
|||
const properties: VersionChangeEvent = { |
|||
from, |
|||
to, |
|||
} |
|||
await publishEvent(Event.VERSION_DOWNGRADED, properties) |
|||
} |
|||
@ -0,0 +1,92 @@ |
|||
import { |
|||
db as dbUtils, |
|||
events, |
|||
utils, |
|||
context, |
|||
tenancy, |
|||
} from "@budibase/backend-core" |
|||
import { Installation } from "@budibase/types" |
|||
import semver from "semver" |
|||
|
|||
const pkg = require("../package.json") |
|||
|
|||
export const getInstall = async (): Promise<Installation> => { |
|||
return dbUtils.doWithDB( |
|||
dbUtils.StaticDatabases.PLATFORM_INFO.name, |
|||
async (platformDb: any) => { |
|||
let install: Installation |
|||
try { |
|||
install = await platformDb.get( |
|||
dbUtils.StaticDatabases.PLATFORM_INFO.docs.install |
|||
) |
|||
} catch (e: any) { |
|||
if (e.status === 404) { |
|||
install = { |
|||
_id: dbUtils.StaticDatabases.PLATFORM_INFO.docs.install, |
|||
installId: utils.newid(), |
|||
version: pkg.version, |
|||
} |
|||
const resp = await platformDb.put(install) |
|||
install._rev = resp.rev |
|||
} else { |
|||
throw e |
|||
} |
|||
} |
|||
return install |
|||
}, |
|||
{} |
|||
) |
|||
} |
|||
|
|||
const updateVersion = async (version: string): Promise<boolean> => { |
|||
try { |
|||
await dbUtils.doWithDB( |
|||
dbUtils.StaticDatabases.PLATFORM_INFO.name, |
|||
async (platformDb: any) => { |
|||
const install = await getInstall() |
|||
install.version = version |
|||
await platformDb.put(install) |
|||
}, |
|||
{} |
|||
) |
|||
} catch (e: any) { |
|||
if (e.status === 409) { |
|||
// do nothing - version has already been updated
|
|||
// likely in clustered environment
|
|||
return false |
|||
} |
|||
throw e |
|||
} |
|||
return true |
|||
} |
|||
|
|||
export const checkInstallVersion = async (): Promise<void> => { |
|||
const install = await getInstall() |
|||
|
|||
const currentVersion = install.version |
|||
const newVersion = pkg.version |
|||
|
|||
if (currentVersion !== newVersion) { |
|||
const isUpgrade = semver.gt(newVersion, currentVersion) |
|||
const isDowngrade = semver.lt(newVersion, currentVersion) |
|||
|
|||
const success = await updateVersion(newVersion) |
|||
|
|||
if (success) { |
|||
await context.doInUserContext( |
|||
{ |
|||
_id: install.installId, |
|||
isInstall: true, |
|||
tenantId: tenancy.DEFAULT_TENANT_ID, |
|||
}, |
|||
async () => { |
|||
if (isUpgrade) { |
|||
await events.version.upgraded(currentVersion, newVersion) |
|||
} else if (isDowngrade) { |
|||
await events.version.downgraded(currentVersion, newVersion) |
|||
} |
|||
} |
|||
) |
|||
} |
|||
} |
|||
} |
|||
@ -1,2 +1,3 @@ |
|||
export * as app from "./app" |
|||
export * as global from "./global" |
|||
export * as installation from "./installation" |
|||
|
|||
@ -0,0 +1,22 @@ |
|||
import { events, tenancy } from "@budibase/backend-core" |
|||
import { Installation } from "@budibase/types" |
|||
import * as installation from "../../../installation" |
|||
import * as global from "./global" |
|||
|
|||
/** |
|||
* Date: |
|||
* May 2022 |
|||
* |
|||
* Description: |
|||
* Backfill installation events. |
|||
*/ |
|||
|
|||
export const run = async () => { |
|||
// need to use the default tenant to try to get the installation time
|
|||
await tenancy.doInTenant(tenancy.DEFAULT_TENANT_ID, async () => { |
|||
const db = tenancy.getGlobalDB() |
|||
const installTimestamp = (await global.getInstallTimestamp(db)) as number |
|||
const install: Installation = await installation.getInstall() |
|||
await events.identification.identifyInstallation(install, installTimestamp) |
|||
}) |
|||
} |
|||
@ -1,3 +1,5 @@ |
|||
import { Document } from "../document" |
|||
|
|||
export interface Datasource extends Document {} |
|||
export interface Datasource extends Document { |
|||
source: string |
|||
} |
|||
|
|||
@ -1 +1,9 @@ |
|||
import { Document } from "../document" |
|||
|
|||
export interface GlobalInfo {} |
|||
|
|||
export interface Installation extends Document { |
|||
_id: string |
|||
installId: string |
|||
version: string |
|||
} |
|||
|
|||
@ -1,11 +1,45 @@ |
|||
export interface AutomationCreatedEvent {} |
|||
export interface AutomationCreatedEvent { |
|||
appId: string |
|||
automationId: string |
|||
triggerId: string |
|||
triggerType: string |
|||
} |
|||
|
|||
export interface AutomationDeletedEvent {} |
|||
export interface AutomationTriggerUpdatedEvent { |
|||
appId: string |
|||
automationId: string |
|||
triggerId: string |
|||
triggerType: string |
|||
} |
|||
|
|||
export interface AutomationTestedEvent {} |
|||
export interface AutomationDeletedEvent { |
|||
appId: string |
|||
automationId: string |
|||
triggerId: string |
|||
triggerType: string |
|||
} |
|||
|
|||
export interface AutomationStepCreatedEvent {} |
|||
export interface AutomationTestedEvent { |
|||
appId: string |
|||
automationId: string |
|||
triggerId: string |
|||
triggerType: string |
|||
} |
|||
|
|||
export interface AutomationStepDeletedEvent {} |
|||
export interface AutomationStepCreatedEvent { |
|||
appId: string |
|||
automationId: string |
|||
triggerId: string |
|||
triggerType: string |
|||
stepId: string |
|||
stepType: string |
|||
} |
|||
|
|||
export interface AutomationTriggerUpdatedEvent {} |
|||
export interface AutomationStepDeletedEvent { |
|||
appId: string |
|||
automationId: string |
|||
triggerId: string |
|||
triggerType: string |
|||
stepId: string |
|||
stepType: string |
|||
} |
|||
|
|||
@ -1,5 +1,14 @@ |
|||
export interface DatasourceCreatedEvent {} |
|||
export interface DatasourceCreatedEvent { |
|||
datasourceId: string |
|||
source: string |
|||
} |
|||
|
|||
export interface DatasourceUpdatedEvent {} |
|||
export interface DatasourceUpdatedEvent { |
|||
datasourceId: string |
|||
source: string |
|||
} |
|||
|
|||
export interface DatasourceDeletedEvent {} |
|||
export interface DatasourceDeletedEvent { |
|||
datasourceId: string |
|||
source: string |
|||
} |
|||
|
|||
@ -1,3 +1,7 @@ |
|||
export interface LayoutCreatedEvent {} |
|||
export interface LayoutCreatedEvent { |
|||
layoutId: string |
|||
} |
|||
|
|||
export interface LayoutDeletedEvent {} |
|||
export interface LayoutDeletedEvent { |
|||
layoutId: string |
|||
} |
|||
|
|||
@ -1 +0,0 @@ |
|||
export interface VersionCheckedEvent {} |
|||
@ -0,0 +1,8 @@ |
|||
export interface VersionCheckedEvent { |
|||
currentVersion: string |
|||
} |
|||
|
|||
export interface VersionChangeEvent { |
|||
from: string |
|||
to: string |
|||
} |
|||
Loading…
Reference in new issue