Skip to content

Commit

Permalink
update table sql
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisCarriere committed Oct 23, 2023
1 parent d58cabd commit 3f6eb2f
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 43 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ dist/
substreams-sink-clickhouse
.env
replay.log
*.sql
package-lock.json
.DS_Store
5 changes: 2 additions & 3 deletions src/clickhouse/createDatabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { client } from "../config.js";
import { logger } from "../logger.js";

export async function createDatabase(database: string) {
logger.info(`Creating database '${database}'`);
if (!database) throw new Error("The database name must be specified");
if (!database) throw new Error("[database] is required");
await client.exec({query: `CREATE DATABASE IF NOT EXISTS "${database}"`});
logger.info(`Database [${database}] created`);
logger.info(`CREATE DATABASE [${database}]`);
}
44 changes: 5 additions & 39 deletions src/clickhouse/table-initialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,13 @@ import { client } from "../config.js";
import { logger } from "../logger.js";
import { TableInitSchema } from "../schemas.js";
import { splitSchemaByTableCreation } from "./table-utils.js";

const queries = [
`
CREATE TABLE IF NOT EXISTS manifest (
module_hash FixedString(40),
module_name String(),
chain LowCardinality(String),
type String(),
)
ENGINE = ReplacingMergeTree
ORDER BY (module_hash);
`,
`
CREATE TABLE IF NOT EXISTS block (
block_id FixedString(64),
block_number UInt32(),
chain LowCardinality(String),
timestamp DateTime64(3, 'UTC'),
final_block Bool,
)
ENGINE = ReplacingMergeTree
PRIMARY KEY (block_id)
ORDER BY (block_id, block_number, timestamp);
`,
`CREATE TABLE IF NOT EXISTS unparsed_json (
raw_data JSON,
source LowCardinality(String),
id String,
block_id FixedString(64),
module_hash FixedString(40),
chain LowCardinality(String)
)
ENGINE = MergeTree
ORDER BY (source, chain, module_hash, block_id)`,
];
import tables from "./tables/index.js";

export function initializeManifest(): Promise<unknown> {
logger.info("Initializing 'manifest' table.");
logger.info("Initializing 'clock' table.");
logger.info("Initializing 'unparsed_json' table.");
return Promise.all(queries.map((query) => client.command({ query })));
return Promise.all(tables.map(([table, query]) => {
logger.info(`CREATE TABLE [${table}]`);
return client.command({ query })
}));
}

const metadataQueries = (tableName: string) => [
Expand Down
9 changes: 9 additions & 0 deletions src/clickhouse/tables/blocks.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CREATE TABLE IF NOT EXISTS blocks (
block_id FixedString(64),
block_number UInt32(),
chain LowCardinality(String),
timestamp DateTime64(3, 'UTC'),
)
ENGINE = ReplacingMergeTree
PRIMARY KEY (block_id)
ORDER BY (block_id, block_number, chain, timestamp);
5 changes: 5 additions & 0 deletions src/clickhouse/tables/final_blocks.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE IF NOT EXISTS final_blocks (
block_id FixedString(64),
)
ENGINE = ReplacingMergeTree
ORDER BY (block_id);
16 changes: 16 additions & 0 deletions src/clickhouse/tables/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import blocks_sql from "./blocks.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 default [
["blocks", blocks],
["final_blocks", final_blocks],
["module_hashes", module_hashes],
["unparsed_json", unparsed_json],
]
8 changes: 8 additions & 0 deletions src/clickhouse/tables/module_hashes.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CREATE TABLE IF NOT EXISTS module_hashes (
module_hash FixedString(40),
module_name String(),
chain LowCardinality(String),
type String(),
)
ENGINE = ReplacingMergeTree
ORDER BY (module_hash);
10 changes: 10 additions & 0 deletions src/clickhouse/tables/unparsed_json.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CREATE TABLE IF NOT EXISTS unparsed_json (
raw_data JSON,
source LowCardinality(String),
id String,
block_id FixedString(64),
module_hash FixedString(40),
chain LowCardinality(String)
)
ENGINE = MergeTree
ORDER BY (source, chain, module_hash, block_id)
5 changes: 5 additions & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ declare module "*.html" {
const content: string;
export default content;
}

declare module "*.sql" {
const content: string;
export default content;
}

0 comments on commit 3f6eb2f

Please sign in to comment.