From 776116e971b01496682926a0feb36c9fad588bd9 Mon Sep 17 00:00:00 2001 From: Denis Carriere Date: Fri, 16 Feb 2024 19:51:37 -0500 Subject: [PATCH] Update table import in table-initialization.ts --- src/clickhouse/table-initialization.ts | 2 +- src/clickhouse/tables/blocks.sql | 3 ++- src/clickhouse/tables/blocks_mv.sql | 22 ++++++++++++++++++++++ src/clickhouse/tables/index.ts | 19 ------------------- src/clickhouse/tables/tables.ts | 14 ++++++++++++++ 5 files changed, 39 insertions(+), 21 deletions(-) create mode 100644 src/clickhouse/tables/blocks_mv.sql delete mode 100644 src/clickhouse/tables/index.ts create mode 100644 src/clickhouse/tables/tables.ts diff --git a/src/clickhouse/table-initialization.ts b/src/clickhouse/table-initialization.ts index 1c80d98..4dec1cb 100644 --- a/src/clickhouse/table-initialization.ts +++ b/src/clickhouse/table-initialization.ts @@ -2,7 +2,7 @@ import { logger } from "../logger.js"; import { Err, Ok, Result } from "../result.js"; import client from "./createClient.js"; import { augmentCreateTableStatement, getTableName, isCreateTableStatement } from "./table-utils.js"; -import tables from "./tables/index.js"; +import tables from "./tables/tables.js"; export async function initializeDefaultTables(): Promise { const promiseResults = await Promise.allSettled( diff --git a/src/clickhouse/tables/blocks.sql b/src/clickhouse/tables/blocks.sql index c0403aa..2bdb7d5 100644 --- a/src/clickhouse/tables/blocks.sql +++ b/src/clickhouse/tables/blocks.sql @@ -1,3 +1,4 @@ +-- blocks -- CREATE TABLE IF NOT EXISTS blocks ( block_id FixedString(64), block_number UInt32(), @@ -6,4 +7,4 @@ CREATE TABLE IF NOT EXISTS blocks ( ) ENGINE = ReplacingMergeTree PRIMARY KEY (block_id) -ORDER BY (block_id, chain, block_number, timestamp); \ No newline at end of file +ORDER BY (block_id, chain, block_number, timestamp); diff --git a/src/clickhouse/tables/blocks_mv.sql b/src/clickhouse/tables/blocks_mv.sql new file mode 100644 index 0000000..9d1cac7 --- /dev/null +++ b/src/clickhouse/tables/blocks_mv.sql @@ -0,0 +1,22 @@ +-- view -- +CREATE MATERIALIZED VIEW IF NOT EXISTS blocks_mv +ENGINE = MergeTree +ORDER BY (chain, timestamp, block_number) +AS SELECT + block_id, + block_number, + chain, + timestamp +FROM blocks; + +-- DROP TABLE IF EXISTS blocks_mv; + +-- OPTIMIZE TABLE blocks_mv FINAL; + +-- -- insert -- +-- INSERT INTO blocks_mv SELECT +-- block_id, +-- block_number, +-- chain, +-- timestamp +-- FROM blocks; \ No newline at end of file diff --git a/src/clickhouse/tables/index.ts b/src/clickhouse/tables/index.ts deleted file mode 100644 index b9b50b1..0000000 --- a/src/clickhouse/tables/index.ts +++ /dev/null @@ -1,19 +0,0 @@ -import blocks_sql from "./blocks.sql"; -import deleted_entity_changes_sql from "./deleted_entity_changes.sql"; -import final_blocks_sql from "./final_blocks.sql"; -import module_hashes_sql from "./module_hashes.sql"; -import unparsed_json_sql from "./unparsed_json.sql"; - -export const blocks = await Bun.file(blocks_sql).text(); -export const final_blocks = await Bun.file(final_blocks_sql).text(); -export const module_hashes = await Bun.file(module_hashes_sql).text(); -export const unparsed_json = await Bun.file(unparsed_json_sql).text(); -export const deleted_entity_changes = await Bun.file(deleted_entity_changes_sql).text(); - -export default [ - ["blocks", blocks], - ["final_blocks", final_blocks], - ["module_hashes", module_hashes], - ["unparsed_json", unparsed_json], - ["deleted_entity_changes", deleted_entity_changes], -]; diff --git a/src/clickhouse/tables/tables.ts b/src/clickhouse/tables/tables.ts new file mode 100644 index 0000000..29024e5 --- /dev/null +++ b/src/clickhouse/tables/tables.ts @@ -0,0 +1,14 @@ +import { join } from "path"; + +function file(path: string) { + return Bun.file(join(import.meta.dirname, path)).text(); +} + +const glob = new Bun.Glob("**/*.sql"); +const tables: [string, string][] = []; + +for (const path of glob.scanSync(import.meta.dirname)) { + tables.push([path.replace(".sql", ""), await file(path)]) +} + +export default tables; \ No newline at end of file