Skip to content

Commit

Permalink
readd js script
Browse files Browse the repository at this point in the history
  • Loading branch information
Matlefebvre1234 committed Aug 14, 2024
1 parent 2cea9da commit fab8c26
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 61 deletions.
48 changes: 48 additions & 0 deletions create-schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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 and Replicated 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\s+\w+/g, "");
modifiedData = modifiedData.replace(/Replicated/g, "");
}

// 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!");
});
});
87 changes: 26 additions & 61 deletions create_schema.sh → schema.sql.example
Original file line number Diff line number Diff line change
@@ -1,47 +1,8 @@
#!/usr/bin/env bash

# Helper script for generating the `schema.sql` ClickHouse tables definition
# Specify a cluster name to add `ON CLUSTER` directives

show_usage() {
printf 'Usage: %s [(-o|--outfile) file (default: "schema.sql")] [(-c|--cluster) name (default: none)] [(-h|--help)]\n' "$(basename "$0")"
exit 0
}

SCHEMA_FILE="./schema.sql"
CLUSTER_NAME=""
while [[ "$#" -gt 0 ]]; do
case $1 in
-o|--outfile) SCHEMA_FILE="$2"; shift ;;
-c|--cluster) CLUSTER_NAME="$2"; shift ;;
-h|--help) show_usage ;;
*) echo "Unknown parameter passed: $1"; show_usage; exit 1 ;;
esac
shift
done

ON_CLUSTER_DIRECTIVE=""
if [ -n "$CLUSTER_NAME" ]; then
ON_CLUSTER_DIRECTIVE="ON CLUSTER $CLUSTER_NAME"
fi

cat > $SCHEMA_FILE <<- EOM
--------------------------------------
-- AUTO-GENERATED FILE, DO NOT EDIT --
--------------------------------------
-- This SQL file creates the required tables for a single Antelope chain.
-- You can use the ClickHouse client command to execute it:
-- $ cat schema.sql | clickhouse client -h <host> --port 9000 -d <database> -u <user> --password <password>
-------------------------------------------------
-- Meta tables to store Substreams information --
-------------------------------------------------
-------------------------------------------------
-- Meta tables to store Substreams information --
-------------------------------------------------

CREATE TABLE IF NOT EXISTS cursors
CREATE TABLE IF NOT EXISTS cursors ON CLUSTER cluster_name
(
id String,
cursor String,
Expand All @@ -58,7 +19,7 @@ CREATE TABLE IF NOT EXISTS cursors
-- -- Table for all balance changes event --
-------------------------------------------------

CREATE TABLE IF NOT EXISTS balance_changes (
CREATE TABLE IF NOT EXISTS balance_changes ON CLUSTER cluster_name (
"id" String,
timestamp DateTime64(3, 'UTC'),
contract FixedString(40),
Expand All @@ -78,7 +39,7 @@ ORDER BY (id,timestamp, block_num);
-- -- MV for historical balance changes event order by contract address --
------------------------------------------------------------------------------

CREATE MATERIALIZED VIEW balance_changes_contract_historical_mv
CREATE MATERIALIZED VIEW balance_changes_contract_historical_mv ON CLUSTER cluster_name
ENGINE = ReplicatedReplacingMergeTree()
ORDER BY (contract, owner,block_num)
POPULATE
Expand All @@ -87,7 +48,7 @@ AS SELECT * FROM balance_changes;
------------------------------------------------------------------------------
-- -- MV for historical balance changes event order by account address --
------------------------------------------------------------------------------
CREATE MATERIALIZED VIEW balance_changes_account_historical_mv
CREATE MATERIALIZED VIEW balance_changes_account_historical_mv ON CLUSTER cluster_name
ENGINE = ReplicatedReplacingMergeTree()
ORDER BY (owner, contract,block_num)
POPULATE
Expand All @@ -97,7 +58,7 @@ AS SELECT * FROM balance_changes;
-------------------------------------------
-- -- MV for latest token_holders --
-------------------------------------------
CREATE TABLE IF NOT EXISTS token_holders
CREATE TABLE IF NOT EXISTS token_holders ON CLUSTER cluster_name
(
account FixedString(40),
contract String,
Expand All @@ -110,7 +71,7 @@ CREATE TABLE IF NOT EXISTS token_holders
PRIMARY KEY (contract,account)
ORDER BY (contract, account);

CREATE MATERIALIZED VIEW token_holders_mv
CREATE MATERIALIZED VIEW token_holders_mv ON CLUSTER cluster_name
TO token_holders
AS
SELECT owner as account,
Expand All @@ -126,7 +87,7 @@ FROM balance_changes;
-------------------------------------------
-- MV for account balances --
-------------------------------------------
CREATE TABLE IF NOT EXISTS account_balances
CREATE TABLE IF NOT EXISTS account_balances ON CLUSTER cluster_name
(
account FixedString(40),
contract String,
Expand All @@ -139,7 +100,7 @@ CREATE TABLE IF NOT EXISTS account_balances
PRIMARY KEY (account,contract)
ORDER BY (account,contract);

CREATE MATERIALIZED VIEW account_balances_mv
CREATE MATERIALIZED VIEW account_balances_mv ON CLUSTER cluster_name
TO account_balances
AS
SELECT owner as account,
Expand All @@ -154,7 +115,7 @@ FROM balance_changes;
-------------------------------------------------
-- Table for all token information --
-------------------------------------------------
CREATE TABLE IF NOT EXISTS contracts (
CREATE TABLE IF NOT EXISTS contracts ON CLUSTER cluster_name (
contract FixedString(40),
name String,
symbol String,
Expand All @@ -171,7 +132,7 @@ ORDER BY (contract);
-------------------------------------------------
-- Table for token supply --
-------------------------------------------------
CREATE TABLE IF NOT EXISTS supply (
CREATE TABLE IF NOT EXISTS supply ON CLUSTER cluster_name (
contract FixedString(40),
supply UInt256,
block_num UInt32(),
Expand All @@ -181,10 +142,21 @@ ENGINE = ReplicatedReplacingMergeTree()
ORDER BY (contract,block_num);



-------------------------------------------------
-- 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;


-------------------------------------------------
-- table for all transfers events --
-------------------------------------------------
CREATE TABLE IF NOT EXISTS transfers (
CREATE TABLE IF NOT EXISTS transfers ON CLUSTER cluster_name (
id String,
contract FixedString(40),
`from` String,
Expand All @@ -203,7 +175,7 @@ ORDER BY (id, tx_id, block_num, timestamp);
-------------------------------------------------
-- MV for historical transfers events by contract address --
-------------------------------------------------
CREATE MATERIALIZED VIEW transfers_contract_historical_mv
CREATE MATERIALIZED VIEW transfers_contract_historical_mv ON CLUSTER cluster_name
ENGINE = ReplicatedReplacingMergeTree()
ORDER BY (contract, `from`,`to`,block_num)
POPULATE
Expand All @@ -212,7 +184,7 @@ AS SELECT * FROM transfers;
-------------------------------------------------
-- MV for historical transfers events by from address --
-------------------------------------------------
CREATE MATERIALIZED VIEW transfers_from_historical_mv
CREATE MATERIALIZED VIEW transfers_from_historical_mv ON CLUSTER cluster_name
ENGINE = ReplicatedReplacingMergeTree()
ORDER BY (`from`, contract,block_num)
POPULATE
Expand All @@ -221,15 +193,8 @@ AS SELECT * FROM transfers;
-------------------------------------------------
-- MV for historical transfers events by to address --
-------------------------------------------------
CREATE MATERIALIZED VIEW transfers_to_historical_mv
CREATE MATERIALIZED VIEW transfers_to_historical_mv ON CLUSTER cluster_name
ENGINE = ReplicatedReplacingMergeTree()
ORDER BY (`to`, contract,block_num)
POPULATE
AS SELECT * FROM transfers;
EOM

echo "[+] Created '$SCHEMA_FILE'"
echo "[*] Run the following command to apply:"
echo "cat $SCHEMA_FILE | clickhouse client -h <host> --port 9000 -d <database> -u <user> --password <password>"
AS SELECT * FROM transfers;

0 comments on commit fab8c26

Please sign in to comment.