Skip to content

Commit 084eca2

Browse files
authored
Extract domain fields (#7075)
Signed-off-by: Denis Bykhov <[email protected]>
1 parent 3da16bf commit 084eca2

File tree

93 files changed

+200
-41
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+200
-41
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
DO $$
2+
DECLARE
3+
tbl_name text;
4+
hash_col_not_exists boolean;
5+
data_col_exists boolean;
6+
BEGIN
7+
FOR tbl_name IN
8+
SELECT table_name
9+
FROM information_schema.tables
10+
WHERE table_schema = 'public'
11+
AND table_type = 'BASE TABLE'
12+
LOOP
13+
EXECUTE format('
14+
SELECT EXISTS (
15+
SELECT 1
16+
FROM information_schema.columns
17+
WHERE table_name = %L
18+
AND column_name = ''data''
19+
AND data_type = ''jsonb''
20+
);', tbl_name) INTO data_col_exists;
21+
22+
EXECUTE format('
23+
SELECT NOT EXISTS (
24+
SELECT 1
25+
FROM information_schema.columns
26+
WHERE table_name = %L
27+
AND column_name = ''"%%hash%%"''
28+
);', tbl_name) INTO hash_col_not_exists;
29+
30+
IF data_col_exists AND hash_col_not_exists THEN
31+
EXECUTE format('
32+
ALTER TABLE %I ADD COLUMN "%%hash%%" text;', tbl_name);
33+
EXECUTE format('
34+
UPDATE %I SET "%%hash%%" = data->>''%%data%%'';', tbl_name);
35+
END IF;
36+
END LOOP;
37+
END $$;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
ALTER TABLE notification_dnc
2+
ADD "objectId" text,
3+
ADD "objectClass" text,
4+
ADD "user" text;
5+
6+
ALTER TABLE notification_dnc
7+
DROP COLUMN "attachedTo";
8+
9+
UPDATE notification_dnc
10+
SET "objectId" = (data->>'objectId');
11+
12+
UPDATE notification_dnc
13+
SET "objectClass" = (data->>'objectClass');
14+
15+
UPDATE notification_dnc
16+
SET "user" = (data->>'user');
17+
18+
ALTER TABLE notification_dnc
19+
ALTER COLUMN "objectId" SET NOT NULL;
20+
21+
ALTER TABLE notification_dnc
22+
ALTER COLUMN "objectClass" SET NOT NULL;
23+
24+
ALTER TABLE notification_dnc
25+
ALTER COLUMN "user" SET NOT NULL;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
ALTER TABLE doc_index_state
2+
ADD "needIndex" bool;
3+
4+
ALTER TABLE doc_index_state
5+
DROP COLUMN "attachedTo";
6+
7+
UPDATE doc_index_state
8+
SET "needIndex" = (data->>'needIndex')::boolean;
9+
10+
ALTER TABLE doc_index_state
11+
ALTER COLUMN "needIndex" SET NOT NULL;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
ALTER TABLE notification
2+
ADD "isViewed" bool,
3+
ADD archived bool,
4+
ADD "user" text;
5+
6+
ALTER TABLE notification
7+
DROP COLUMN "attachedTo";
8+
9+
UPDATE notification
10+
SET "isViewed" = (data->>'isViewed')::boolean;
11+
12+
UPDATE notification
13+
SET "archived" = (data->>'archived')::boolean;
14+
15+
UPDATE notification
16+
SET "user" = (data->>'user');
17+
18+
ALTER TABLE notification
19+
ALTER COLUMN "isViewed" SET NOT NULL;
20+
21+
ALTER TABLE notification
22+
ALTER COLUMN archived SET NOT NULL;
23+
24+
ALTER TABLE notification
25+
ALTER COLUMN "user" SET NOT NULL;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
ALTER TABLE tx
2+
ADD "objectSpace" text,
3+
ADD "objectId" text;
4+
5+
ALTER TABLE tx
6+
DROP COLUMN "attachedTo";
7+
8+
UPDATE tx
9+
SET "objectId" = (data->>'objectId');
10+
11+
UPDATE tx
12+
SET "objectSpace" = (data->>'objectSpace');
13+
14+
ALTER TABLE tx
15+
ALTER COLUMN "objectSpace" SET NOT NULL;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
ALTER TABLE notification_user
2+
ADD "user" text;
3+
4+
ALTER TABLE notification_user
5+
DROP COLUMN "attachedTo";
6+
7+
UPDATE notification_user
8+
SET "user" = (data->>'user');
9+
10+
ALTER TABLE notification_user
11+
ALTER COLUMN "user" SET NOT NULL;

server/postgres/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@
1414
//
1515

1616
export * from './storage'
17-
export { getDBClient, convertDoc, createTable, retryTxn, translateDomain, getDocFieldsByDomains } from './utils'
17+
export { getDBClient, convertDoc, createTable, retryTxn } from './utils'
18+
export { getDocFieldsByDomains, translateDomain } from './schemas'

server/postgres/src/schemas.ts

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,79 @@
1-
import { DOMAIN_SPACE } from '@hcengineering/core'
1+
import { DOMAIN_DOC_INDEX_STATE, DOMAIN_SPACE, DOMAIN_TX } from '@hcengineering/core'
22

33
type DataType = 'bigint' | 'bool' | 'text' | 'text[]'
44

55
type Schema = Record<string, [DataType, boolean]>
66

7-
export const defaultSchema: Schema = {
7+
const baseSchema: Schema = {
88
_id: ['text', true],
99
_class: ['text', true],
1010
space: ['text', true],
1111
modifiedBy: ['text', true],
1212
createdBy: ['text', false],
1313
modifiedOn: ['bigint', true],
1414
createdOn: ['bigint', false],
15+
'%hash%': ['text', false]
16+
}
17+
18+
const defaultSchema: Schema = {
19+
...baseSchema,
1520
attachedTo: ['text', false]
1621
}
1722

18-
export const spaceSchema: Schema = {
19-
_id: ['text', true],
20-
_class: ['text', true],
21-
space: ['text', true],
22-
modifiedBy: ['text', true],
23-
createdBy: ['text', false],
24-
modifiedOn: ['bigint', true],
25-
createdOn: ['bigint', false],
23+
const spaceSchema: Schema = {
24+
...baseSchema,
2625
private: ['bool', true],
2726
members: ['text[]', true]
2827
}
2928

29+
const txSchema: Schema = {
30+
...baseSchema,
31+
objectSpace: ['text', true],
32+
objectId: ['text', false]
33+
}
34+
35+
const notificationSchema: Schema = {
36+
...baseSchema,
37+
isViewed: ['bool', true],
38+
archived: ['bool', true],
39+
user: ['text', true]
40+
}
41+
42+
const dncSchema: Schema = {
43+
...baseSchema,
44+
objectId: ['text', true],
45+
objectClass: ['text', true],
46+
user: ['text', true]
47+
}
48+
49+
const userNotificationSchema: Schema = {
50+
...baseSchema,
51+
user: ['text', true]
52+
}
53+
54+
const docIndexStateSchema: Schema = {
55+
...baseSchema,
56+
needIndex: ['bool', true]
57+
}
58+
59+
export function translateDomain (domain: string): string {
60+
return domain.replaceAll('-', '_')
61+
}
62+
3063
export const domainSchemas: Record<string, Schema> = {
31-
[DOMAIN_SPACE]: spaceSchema
64+
[DOMAIN_SPACE]: spaceSchema,
65+
[DOMAIN_TX]: txSchema,
66+
[translateDomain(DOMAIN_DOC_INDEX_STATE)]: docIndexStateSchema,
67+
notification: notificationSchema,
68+
[translateDomain('notification-dnc')]: dncSchema,
69+
[translateDomain('notification-user')]: userNotificationSchema
3270
}
3371

3472
export function getSchema (domain: string): Schema {
35-
return domainSchemas[domain] ?? defaultSchema
73+
return domainSchemas[translateDomain(domain)] ?? defaultSchema
74+
}
75+
76+
export function getDocFieldsByDomains (domain: string): string[] {
77+
const schema = domainSchemas[translateDomain(domain)] ?? defaultSchema
78+
return Object.keys(schema)
3679
}

server/postgres/src/storage.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,16 @@ import {
7474
type DBDoc,
7575
escapeBackticks,
7676
getDBClient,
77-
getDocFieldsByDomains,
7877
inferType,
7978
isDataField,
8079
isOwner,
8180
type JoinProps,
8281
parseDoc,
8382
parseDocWithProjection,
8483
parseUpdate,
85-
type PostgresClientReference,
86-
translateDomain
84+
type PostgresClientReference
8785
} from './utils'
86+
import { getDocFieldsByDomains, translateDomain } from './schemas'
8887

8988
abstract class PostgresAdapterBase implements DbAdapter {
9089
protected readonly _helper: DBCollectionHelper
@@ -1550,7 +1549,7 @@ class PostgresTxAdapter extends PostgresAdapterBase implements TxAdapter {
15501549

15511550
async getModel (ctx: MeasureContext): Promise<Tx[]> {
15521551
const res = await this
1553-
.client`SELECT * FROM ${this.client(translateDomain(DOMAIN_TX))} WHERE "workspaceId" = ${this.workspaceId.name} AND data->>'objectSpace' = ${core.space.Model} ORDER BY _id ASC, "modifiedOn" ASC`
1552+
.client`SELECT * FROM ${this.client(translateDomain(DOMAIN_TX))} WHERE "workspaceId" = ${this.workspaceId.name} AND "objectSpace" = ${core.space.Model} ORDER BY _id ASC, "modifiedOn" ASC`
15541553

15551554
const model = res.map((p) => parseDoc<Tx>(p as any))
15561555
// We need to put all core.account.System transactions first

server/postgres/src/utils.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import core, {
3030
import { PlatformError, unknownStatus } from '@hcengineering/platform'
3131
import { type DomainHelperOperations } from '@hcengineering/server-core'
3232
import postgres from 'postgres'
33-
import { defaultSchema, domainSchemas, getSchema } from './schemas'
33+
import { getDocFieldsByDomains, getSchema, translateDomain } from './schemas'
3434

3535
const connections = new Map<string, PostgresClientReferenceImpl>()
3636

@@ -329,10 +329,6 @@ export class DBCollectionHelper implements DomainHelperOperations {
329329
}
330330
}
331331

332-
export function translateDomain (domain: string): string {
333-
return domain.replaceAll('-', '_')
334-
}
335-
336332
export function parseDocWithProjection<T extends Doc> (doc: DBDoc, projection: Projection<T> | undefined): T {
337333
const { workspaceId, data, ...rest } = doc
338334
for (const key in rest) {
@@ -397,11 +393,6 @@ export function isDataField (domain: string, field: string): boolean {
397393
return !getDocFieldsByDomains(domain).includes(field)
398394
}
399395

400-
export function getDocFieldsByDomains (domain: string): string[] {
401-
const schema = domainSchemas[domain] ?? defaultSchema
402-
return Object.keys(schema)
403-
}
404-
405396
export interface JoinProps {
406397
table: string // table to join
407398
path: string // _id.roles, attachedTo.attachedTo, space...

0 commit comments

Comments
 (0)