Skip to content

Commit

Permalink
add cluster.js to generate schema.sql with cluster name
Browse files Browse the repository at this point in the history
  • Loading branch information
Matlefebvre1234 committed Jul 23, 2024
1 parent 1300a91 commit 17f72a8
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 23 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,6 @@ cursor.lock
antelope-token-api

*.db

#SCHEMA
schema.sql
47 changes: 47 additions & 0 deletions cluster.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const fs = require("fs");
const path = require("path");

// Replace with the actual path to your schema file
const inputFilePath = path.join(__dirname, "schema.sql.example");
const outputFilePath = path.join(__dirname, "schema.sql");

// Get the cluster name from the command-line arguments
const args = process.argv.slice(2);
const clusterName = args[0]; // First argument after the script name

if (!clusterName) {
console.error(
"Environment variable 'CLUSTER_NAME' is not set, Cluster will be remove",
);
}

// Read the schema file
fs.readFile(inputFilePath, "utf8", (err, data) => {
if (err) {
console.error(`Error reading file: ${err}`);
process.exit(1);
}

let modifiedData;

if (clusterName) {
// Replace the cluster name in the schema
modifiedData = data.replace(
/ON CLUSTER\s+\w+/g,
`ON CLUSTER ${clusterName}`,
);

console.log(`Cluster name updated to: ${clusterName}`);
} else {
modifiedData = data.replace(`/ON CLUSTER`, "");
}

// Write the modified schema to a new file
fs.writeFile(outputFilePath, modifiedData, "utf8", (err) => {
if (err) {
console.error(`Error writing file: ${err}`);
process.exit(1);
}
console.log("Schema updated successfully!");
});
});
95 changes: 72 additions & 23 deletions clickhouse schema/schema.sql → schema.sql.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
-- Table for balance changes --
CREATE TABLE IF NOT EXISTS balance_changes ON CLUSTER antelope (
-------------------------------------------------
-- Meta tables to store Substreams information --
-------------------------------------------------

CREATE TABLE IF NOT EXISTS cursors
(
id String,
cursor String,
block_num Int64,
block_id String
)
ENGINE = ReplacingMergeTree()
PRIMARY KEY (id)
ORDER BY (id);



-------------------------------------------------
-- -- Table for all balance changes event --
-------------------------------------------------

CREATE TABLE IF NOT EXISTS balance_changes ON CLUSTER cluster_name (
"id" String,
timestamp DateTime64(3, 'UTC'),
contract FixedString(40),
Expand All @@ -15,22 +35,30 @@ ENGINE = ReplicatedMergeTree PRIMARY KEY ("id")
ORDER BY (id,timestamp, block_num);


-- MV for contract --s
CREATE MATERIALIZED VIEW balance_changes_contract_historical_mv ON CLUSTER antelope
------------------------------------------------------------------------------
-- -- MV for historical balance changes event order by contract address --
------------------------------------------------------------------------------

CREATE MATERIALIZED VIEW balance_changes_contract_historical_mv ON CLUSTER cluster_name
ENGINE = ReplicatedReplacingMergeTree()
ORDER BY (contract, owner,block_num)
POPULATE
AS SELECT * FROM balance_changes;

-- MV for owner --
CREATE MATERIALIZED VIEW balance_changes_account_historical_mv ON CLUSTER antelope
------------------------------------------------------------------------------
-- -- MV for historical balance changes event order by account address --
------------------------------------------------------------------------------
CREATE MATERIALIZED VIEW balance_changes_account_historical_mv ON CLUSTER cluster_name
ENGINE = ReplicatedReplacingMergeTree()
ORDER BY (owner, contract,block_num)
POPULATE
AS SELECT * FROM balance_changes;


CREATE TABLE IF NOT EXISTS token_holders ON CLUSTER antelope
-------------------------------------------
-- -- MV for latest token_holders --
-------------------------------------------
CREATE TABLE IF NOT EXISTS token_holders ON CLUSTER cluster_name
(
account FixedString(40),
contract String,
Expand All @@ -43,7 +71,7 @@ CREATE TABLE IF NOT EXISTS token_holders ON CLUSTER antelope
PRIMARY KEY (contract,account)
ORDER BY (contract, account);

CREATE MATERIALIZED VIEW token_holders_mv ON CLUSTER antelope
CREATE MATERIALIZED VIEW token_holders_mv ON CLUSTER cluster_name
TO token_holders
AS
SELECT owner as account,
Expand All @@ -54,7 +82,12 @@ SELECT owner as account,
transaction_id as tx_id
FROM balance_changes;

CREATE TABLE IF NOT EXISTS account_balances ON CLUSTER antelope


-------------------------------------------
-- MV for account balances --
-------------------------------------------
CREATE TABLE IF NOT EXISTS account_balances ON CLUSTER cluster_name
(
account FixedString(40),
contract String,
Expand All @@ -67,7 +100,7 @@ CREATE TABLE IF NOT EXISTS account_balances ON CLUSTER antelope
PRIMARY KEY (account,contract)
ORDER BY (account,contract);

CREATE MATERIALIZED VIEW account_balances_mv ON CLUSTER antelope
CREATE MATERIALIZED VIEW account_balances_mv ON CLUSTER cluster_name
TO account_balances
AS
SELECT owner as account,
Expand All @@ -79,8 +112,10 @@ SELECT owner as account,
FROM balance_changes;



CREATE TABLE IF NOT EXISTS contracts ON CLUSTER antelope (
-------------------------------------------------
-- Table for all token information --
-------------------------------------------------
CREATE TABLE IF NOT EXISTS contracts ON CLUSTER cluster_name (
contract FixedString(40),
name String,
symbol String,
Expand All @@ -94,7 +129,10 @@ ORDER BY (contract);



CREATE TABLE IF NOT EXISTS supply ON CLUSTER antelope (
-------------------------------------------------
-- Table for token supply --
-------------------------------------------------
CREATE TABLE IF NOT EXISTS supply ON CLUSTER cluster_name (
contract FixedString(40),
supply UInt256,
block_num UInt32(),
Expand All @@ -105,16 +143,21 @@ ENGINE = ReplicatedReplacingMergeTree(version)
ORDER BY (contract,supply);


-- MV for contract --
CREATE MATERIALIZED VIEW mv_supply_contract ON CLUSTER antelope

-------------------------------------------------
-- MV for token supply order by contract address --
-------------------------------------------------
CREATE MATERIALIZED VIEW mv_supply_contract ON CLUSTER cluster_name
ENGINE = ReplicatedReplacingMergeTree()
ORDER BY (contract,block_num)
POPULATE
AS SELECT * FROM supply;



CREATE TABLE IF NOT EXISTS transfers ON CLUSTER antelope (
-------------------------------------------------
-- table for all transfers events --
-------------------------------------------------
CREATE TABLE IF NOT EXISTS transfers ON CLUSTER cluster_name (
id String,
contract FixedString(40),
`from` String,
Expand All @@ -130,22 +173,28 @@ PRIMARY KEY (id)
ORDER BY (id, tx_id, block_num, timestamp);


-- MV for contract --
CREATE MATERIALIZED VIEW transfers_contract_historical_mv ON CLUSTER antelope
-------------------------------------------------
-- MV for historical transfers events by contract address --
-------------------------------------------------
CREATE MATERIALIZED VIEW transfers_contract_historical_mv ON CLUSTER cluster_name
ENGINE = ReplicatedReplacingMergeTree()
ORDER BY (contract, `from`,`to`,block_num)
POPULATE
AS SELECT * FROM transfers;

-- MV for from --
CREATE MATERIALIZED VIEW transfers_from_historical_mv ON CLUSTER antelope
-------------------------------------------------
-- MV for historical transfers events by from address --
-------------------------------------------------
CREATE MATERIALIZED VIEW transfers_from_historical_mv ON CLUSTER cluster_name
ENGINE = ReplicatedReplacingMergeTree()
ORDER BY (`from`, contract,block_num)
POPULATE
AS SELECT * FROM transfers;

-- MV for from --
CREATE MATERIALIZED VIEW transfers_to_historical_mv ON CLUSTER antelope
-------------------------------------------------
-- MV for historical transfers events by to address --
-------------------------------------------------
CREATE MATERIALIZED VIEW transfers_to_historical_mv ON CLUSTER cluster_name
ENGINE = ReplicatedReplacingMergeTree()
ORDER BY (`to`, contract,block_num)
POPULATE
Expand Down

0 comments on commit 17f72a8

Please sign in to comment.