Skip to content

Forced update db schema #8173

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

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
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
110 changes: 97 additions & 13 deletions server/postgres/src/schemas.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { DOMAIN_DOC_INDEX_STATE, DOMAIN_MODEL_TX, DOMAIN_RELATION, DOMAIN_SPACE, DOMAIN_TX } from '@hcengineering/core'

export type DataType = 'bigint' | 'bool' | 'text' | 'text[]'
import { type SchemaDiff, type FieldSchema, type Schema } from './types'

export function getIndex (field: FieldSchema): string {
if (field.indexType === undefined || field.indexType === 'btree') {
Expand All @@ -9,15 +8,6 @@ export function getIndex (field: FieldSchema): string {
return ` USING ${field.indexType}`
}

export interface FieldSchema {
type: DataType
notNull: boolean
index: boolean
indexType?: 'btree' | 'gin' | 'gist' | 'brin' | 'hash'
}

export type Schema = Record<string, FieldSchema>

const baseSchema: Schema = {
_id: {
type: 'text',
Expand Down Expand Up @@ -139,6 +129,11 @@ const notificationSchema: Schema = {
type: 'text',
notNull: true,
index: true
},
docNotifyContext: {
type: 'text',
notNull: false,
index: false
}
}

Expand Down Expand Up @@ -185,7 +180,7 @@ const docIndexStateSchema: Schema = {
}

const timeSchema: Schema = {
...baseSchema,
...defaultSchema,
workslots: {
type: 'bigint',
notNull: false,
Expand Down Expand Up @@ -289,11 +284,98 @@ const githubLogin: Schema = {
}
}

export function addSchema (domain: string, schema: Schema): void {
function addSchema (domain: string, schema: Schema): void {
domainSchemas[translateDomain(domain)] = schema
domainSchemaFields.set(domain, createSchemaFields(schema))
}

// add schema if not forced and return migrate script if have differences
export function setSchema (domain: string, schema: Schema): string | undefined {
const translated = translateDomain(domain)
if (forcedSchemas.includes(translated)) {
const diff = getSchemaDiff(translated, schema)
if (diff !== undefined) {
return migrateSchema(translated, diff)
}
}
addSchema(translated, schema)
}

function migrateSchema (domain: string, diff: SchemaDiff): string {
const queries: string[] = []
if (diff.remove !== undefined) {
for (const key in diff.remove) {
const field = diff.remove[key]
switch (field.type) {
case 'text':
queries.push(`UPDATE ${domain} SET data = jsonb_set(data, '{${key}}', to_jsonb("${key}"), true);`)
break
case 'text[]':
queries.push(`UPDATE ${domain} SET data = jsonb_set(data, '{${key}}', to_jsonb("${key}::text[]"), true);`)
break
case 'bigint':
queries.push(`UPDATE ${domain} SET data = jsonb_set(data, '{${key}}', to_jsonb("${key}"::bigint), true);`)
break
case 'bool':
queries.push(`UPDATE ${domain} SET data = jsonb_set(data, '{${key}}', to_jsonb("${key}"::boolean), true);`)
break
}
queries.push(`ALTER TABLE ${domain} DROP COLUMN "${key}"`)
}
}
if (diff.add !== undefined) {
for (const key in diff.add) {
const field = diff.add[key]
queries.push(`ALTER TABLE ${domain} ADD COLUMN "${key}" ${field.type}`)
queries.push('COMMIT')
switch (field.type) {
case 'text':
queries.push(`UPDATE ${domain} SET "${key}" = (data->>'${key}');`)
break
case 'text[]':
queries.push(`UPDATE ${domain} SET "${key}" = array(
SELECT jsonb_array_elements_text(data->'${key}')
)`)
break
case 'bigint':
queries.push(`UPDATE ${domain} SET "${key}" = (data->>'${key}')::bigint;`)
break
case 'bool':
queries.push(`UPDATE ${domain} SET "${key}" = (data->>'${key}')::boolean;`)
break
}
if (field.notNull) {
queries.push(`ALTER TABLE ${domain} ALTER COLUMN "${key}" SET NOT NULL`)
}
}
}
return queries.join(';')
}

function getSchemaDiff (domain: string, dbSchema: Schema): SchemaDiff | undefined {
const domainSchema = getSchema(domain)
const res: SchemaDiff = {}
const add: Schema = {}
const remove: Schema = {}
for (const key in domainSchema) {
if (dbSchema[key] === undefined) {
add[key] = domainSchema[key]
}
}
for (const key in dbSchema) {
if (domainSchema[key] === undefined) {
remove[key] = dbSchema[key]
}
}
if (Object.keys(add).length > 0) {
res.add = add
}
if (Object.keys(remove).length > 0) {
res.remove = remove
}
return Object.keys(res).length > 0 ? res : undefined
}

export function translateDomain (domain: string): string {
return domain.replaceAll('-', '_')
}
Expand All @@ -315,6 +397,8 @@ export const domainSchemas: Record<string, Schema> = {
kanban: defaultSchema
}

const forcedSchemas: string[] = Object.keys(domainSchemas)

export function getSchema (domain: string): Schema {
return domainSchemas[translateDomain(domain)] ?? defaultSchema
}
Expand Down
11 changes: 2 additions & 9 deletions server/postgres/src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,8 @@ import {
} from '@hcengineering/server-core'
import type postgres from 'postgres'
import { createDBClient, createGreenDBClient, type DBClient } from './client'
import {
getDocFieldsByDomains,
getSchema,
getSchemaAndFields,
type Schema,
type SchemaAndFields,
translateDomain
} from './schemas'
import { type ValueType } from './types'
import { getDocFieldsByDomains, getSchema, getSchemaAndFields, type SchemaAndFields, translateDomain } from './schemas'
import { type Schema, type ValueType } from './types'
import {
convertArrayParams,
convertDoc,
Expand Down
16 changes: 16 additions & 0 deletions server/postgres/src/types.ts
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
export type ValueType = 'common' | 'array' | 'dataArray'

export type DataType = 'bigint' | 'bool' | 'text' | 'text[]'

export interface FieldSchema {
type: DataType
notNull: boolean
index: boolean
indexType?: 'btree' | 'gin' | 'gist' | 'brin' | 'hash'
}

export type Schema = Record<string, FieldSchema>

export interface SchemaDiff {
remove?: Schema
add?: Schema
}
10 changes: 6 additions & 4 deletions server/postgres/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,15 @@ import { type DomainHelperOperations } from '@hcengineering/server-core'
import postgres, { type Options, type ParameterOrJSON } from 'postgres'
import type { DBClient } from './client'
import {
addSchema,
type DataType,
getDocFieldsByDomains,
getIndex,
getSchema,
getSchemaAndFields,
type Schema,
type SchemaAndFields,
setSchema,
translateDomain
} from './schemas'
import { type Schema, type DataType } from './types'

const clientRefs = new Map<string, ClientRef>()
const loadedDomains = new Set<string>()
Expand Down Expand Up @@ -145,7 +144,10 @@ async function getTableSchema (client: postgres.Sql, domains: string[]): Promise
}
}
for (const [domain, schema] of Object.entries(schemas)) {
addSchema(domain, schema)
const schemaMigration = setSchema(domain, schema)
if (schemaMigration !== undefined) {
await client.unsafe(schemaMigration)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need a tool for it and execute before deployment, since it could be many clients, and migrations could go bad.

}
}
}

Expand Down