Skip to content
Open
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
2 changes: 2 additions & 0 deletions foundations/core/packages/model/src/migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ export interface MigrationClient {
create: <T extends Doc>(domain: Domain, doc: T | T[]) => Promise<void>
delete: <T extends Doc>(domain: Domain, _id: Ref<T>) => Promise<void>
deleteMany: <T extends Doc>(domain: Domain, query: DocumentQuery<T>) => Promise<void>
// Check the physical storage domain, including legacy domains no longer present in the active model.
domainExists: (domain: Domain) => Promise<boolean>

hierarchy: Hierarchy
model: ModelDb
Expand Down
33 changes: 30 additions & 3 deletions foundations/server/packages/postgres/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,12 +325,39 @@ export class DBCollectionHelper implements DomainHelperOperations {
async create (domain: Domain): Promise<void> {}

async exists (domain: Domain): Promise<boolean> {
// Always exists. We don't need to check for index existence
return true
// Migrations can reference legacy domains that were removed from the current model,
// so this must check the physical table instead of the initialized model-domain set.
const tableName = translateDomain(domain)
const res = await this.client.execute(
`
SELECT 1
FROM information_schema.tables
WHERE table_schema = 'public'
AND table_name = $1
LIMIT 1
`,
[tableName]
)
return res.length > 0
}

async listDomains (): Promise<Set<Domain>> {
return this.domains
const rows = await this.client.execute(`
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'public'
AND table_name NOT LIKE 'pg_%'
AND table_name NOT LIKE 'cluster_%'
AND table_name NOT LIKE 'kv_%'
AND table_name NOT LIKE 'node_%'
`)
const tableNames = new Set(rows.map((it) => it.table_name as Domain))
for (const domain of this.domains) {
if (tableNames.has(translateDomain(domain) as Domain)) {
tableNames.add(domain)
}
}
return tableNames
}

async createIndex (domain: Domain, value: string | FieldIndexConfig<Doc>, options?: { name: string }): Promise<void> {}
Expand Down
18 changes: 11 additions & 7 deletions models/chunter/src/migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,16 +156,20 @@ export async function createRandom (client: MigrationUpgradeClient, tx: TxOperat
}

async function convertCommentsToChatMessages (client: MigrationClient): Promise<void> {
await client.update(
DOMAIN_COMMENT,
{ _class: 'chunter:class:Comment' as Ref<Class<Doc>> },
{ _class: chunter.class.ChatMessage }
)
await client.move(DOMAIN_COMMENT, { _class: chunter.class.ChatMessage }, DOMAIN_ACTIVITY)
if (await client.domainExists(DOMAIN_COMMENT)) {
await client.update(
DOMAIN_COMMENT,
{ _class: 'chunter:class:Comment' as Ref<Class<Doc>> },
{ _class: chunter.class.ChatMessage }
)
await client.move(DOMAIN_COMMENT, { _class: chunter.class.ChatMessage }, DOMAIN_ACTIVITY)
}
}

async function removeBacklinks (client: MigrationClient): Promise<void> {
await client.deleteMany(DOMAIN_COMMENT, { _class: 'chunter:class:Backlink' as Ref<Class<Doc>> })
if (await client.domainExists(DOMAIN_COMMENT)) {
await client.deleteMany(DOMAIN_COMMENT, { _class: 'chunter:class:Backlink' as Ref<Class<Doc>> })
}
await client.deleteMany(DOMAIN_ACTIVITY, {
_class: activity.class.DocUpdateMessage,
objectClass: 'chunter:class:Backlink' as Ref<Class<Doc>>
Expand Down
6 changes: 6 additions & 0 deletions server/tool/src/upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ export class MigrateClientImpl implements MigrationClient {
await this.lowLevel.rawDeleteMany(domain, query)
}

async domainExists (domain: Domain): Promise<boolean> {
const adapter = this.pipeline.context.adapterManager?.getAdapter(domain, false)
const helper = adapter?.helper?.()
return helper !== undefined ? await helper.exists(domain) : true
}

async fullReindex (): Promise<void> {
await this.queue.send(this.ctx, this.wsIds.uuid, [workspaceEvents.fullReindex()])
}
Expand Down