Skip to content
Merged
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
37 changes: 26 additions & 11 deletions databases/elasticsearch_db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import assert, {equal} from 'assert';

import {Buffer} from 'buffer';
import {createHash} from 'crypto';
import {Client} from 'elasticsearch8';
import {Client} from '@elastic/elasticsearch';
import type {estypes} from '@elastic/elasticsearch';
import {BulkObject} from './cassandra_db';
import {MappingTypeMapping} from "elasticsearch8/lib/api/types";

const schema = '2';

Expand All @@ -30,7 +30,7 @@ const keyToId = (key:string) => {
return keyBuf.length > 512 ? createHash('sha512').update(keyBuf).digest('hex') : key;
};

const mappings: MappingTypeMapping = {
const mappings: estypes.MappingTypeMapping = {
// _id is expected to equal key, unless the UTF-8 encoded key is > 512 bytes, in which case it is
// the hex-encoded sha512 hash of the UTF-8 encoded key.
properties: {
Expand All @@ -39,6 +39,28 @@ const mappings: MappingTypeMapping = {
},
};

const legacyDocToSchema2Key = (index: string, id: string, type: unknown, v1BaseIndex?: string) => {
const legacyType = typeof type === 'string' && type !== '' && type !== '_doc' ? type : null;
if (v1BaseIndex && index !== v1BaseIndex) {
const parts = index.slice(v1BaseIndex.length + 1).split('-');
if (parts.length !== 2) {
throw new Error(`unable to migrate records from index ${index} due to data ambiguity`);
}
if (legacyType != null) return `${parts[0]}:${decodeURIComponent(legacyType)}:${parts[1]}:${id}`;
const idParts = id.split(':');
if (idParts.length !== 2) {
throw new Error(`unable to migrate records from index ${index} due to data ambiguity`);
}
return `${parts[0]}:${idParts[0]}:${parts[1]}:${idParts[1]}`;
}
if (legacyType != null) return `${legacyType}:${id}`;
const idParts = id.split(':');
if (idParts.length !== 2) {
throw new Error(`unable to migrate records from index ${index} due to missing legacy type metadata`);
}
return `${idParts[0]}:${idParts[1]}`;
};

const migrateToSchema2 = async (client: Client, v1BaseIndex: string | undefined, v2Index: string, logger: any) => {
let recordsMigratedLastLogged = 0;
let recordsMigrated = 0;
Expand All @@ -60,14 +82,7 @@ const migrateToSchema2 = async (client: Client, v1BaseIndex: string | undefined,
totals.set(index, total);
const body = [];
for (const {_id, _type, _source: {val}} of hits) {
let key = `${_type}:${_id}`;
if (v1BaseIndex && index !== v1BaseIndex) {
const parts = index.slice(v1BaseIndex.length + 1).split('-');
if (parts.length !== 2) {
throw new Error(`unable to migrate records from index ${index} due to data ambiguity`);
}
key = `${parts[0]}:${decodeURIComponent(_type)}:${parts[1]}:${_id}`;
}
const key = legacyDocToSchema2Key(index, _id, _type, v1BaseIndex);
body.push({index: {_id: keyToId(key)}}, {key, value: JSON.parse(val)});
}
await client.bulk({index: v2Index, body});
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ services:
COUCHDB_USER: ueberdb
COUCHDB_PASSWORD: ueberdb
elasticsearch:
image: elasticsearch:7.17.3
image: elasticsearch:9.3.3
ports:
- 9200:9200
environment:
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"cassandra-driver": "^4.8.0",
"cli-table3": "^0.6.5",
"dirty-ts": "^1.1.8",
"elasticsearch8": "npm:@elastic/elasticsearch@^8.19.1",
"@elastic/elasticsearch": "^9.3.4",
Comment thread
qodo-code-review[bot] marked this conversation as resolved.
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thought this is better. Idk why this was renamed like this before

"eslint": "^9.35.0",
"eslint-config-etherpad": "^4.0.4",
"eslint-plugin-import": "^2.32.0",
Expand Down Expand Up @@ -95,6 +95,6 @@
}
],
"engines": {
"node": ">=16.20.1"
"node": ">=22.0.0"
}
}
67 changes: 43 additions & 24 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading