forked from Helicone/helicone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathch_local_hcone.sh
executable file
·125 lines (98 loc) · 3.98 KB
/
ch_local_hcone.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/bin/bash
set -euo pipefail
# Directory where your migration files are stored
MIGRATIONS_DIR="./migrations"
# Default ClickHouse configuration
DEFAULT_CLICKHOUSE_DB="default"
DEFAULT_CLICKHOUSE_HOST="localhost"
DEFAULT_CLICKHOUSE_PORT="9000"
DEFAULT_CLICKHOUSE_USER="default"
DEFAULT_CLICKHOUSE_PASSWORD="" # Leave blank if no password
DEFAULT_CLICKHOUSE_DISK_SETTINGS="" # Leave blank if want to use default disk (local storage)
# Override with command line arguments if provided
CLICKHOUSE_DB="${1:-$DEFAULT_CLICKHOUSE_DB}"
CLICKHOUSE_HOST="${2:-$DEFAULT_CLICKHOUSE_HOST}"
CLICKHOUSE_PORT="${3:-$DEFAULT_CLICKHOUSE_PORT}"
CLICKHOUSE_USER="${4:-$DEFAULT_CLICKHOUSE_USER}"
CLICKHOUSE_PASSWORD="${5:-$DEFAULT_CLICKHOUSE_PASSWORD}"
CLICKHOUSE_DISK_SETTINGS="${6:-$DEFAULT_CLICKHOUSE_DISK_SETTINGS}"
# Utility function to create connection string
create_clickhouse_string_connection() {
local ch_conn_str="--host $CLICKHOUSE_HOST --port $CLICKHOUSE_PORT --user $CLICKHOUSE_USER --database $CLICKHOUSE_DB"
[[ -n "$CLICKHOUSE_PASSWORD" ]] && ch_conn_str+=" --password $CLICKHOUSE_PASSWORD"
echo $ch_conn_str
}
# Function to create/verify the migration tracking table
create_migration_table() {
local ch_conn_str=$(create_clickhouse_string_connection)
local query="CREATE TABLE IF NOT EXISTS helicone_migrations (migration_name String, applied_date DateTime DEFAULT now()) ENGINE = MergeTree() ORDER BY migration_name"
[[ -n "$CLICKHOUSE_DISK_SETTINGS" ]] && query+=" SETTINGS $CLICKHOUSE_DISK_SETTINGS"
query+=";"
clickhouse-client $ch_conn_str --query "$query"
if [ $? -eq 0 ]; then
echo "Migration table created/verified successfully."
else
echo "Failed to create/verify migration table."
exit 1
fi
}
# Function to check if a migration was already applied
is_migration_applied() {
local ch_conn_str=$(create_clickhouse_string_connection)
local migration_name=$1
local query="SELECT count(*) FROM helicone_migrations WHERE migration_name = '${migration_name}';"
local result=$(clickhouse-client $ch_conn_str --query "$query")
if [[ -z $result ]]; then
echo "There was a failure retrieving the migrations table rows."
exit 1
elif [[ ${result} -gt 0 ]]; then
echo "1"
else
echo "0" # Migration not applied
fi
}
# Mark a migration as applied
mark_migration_as_applied() {
local ch_conn_str=$(create_clickhouse_string_connection)
local migration_name=$1
local query="INSERT INTO helicone_migrations (migration_name) VALUES ('${migration_name}');"
clickhouse-client $ch_conn_str --query "$query"
if [ $? -eq 0 ]; then
echo "Migration $migration_name applied successfully."
else
echo "Failed to mark $migration_name as applied."
exit 1
fi
}
# Function to apply migrations in sequence
run_migrations() {
local ch_conn_str=$(create_clickhouse_string_connection)
for file in `ls $MIGRATIONS_DIR | sort -V`; do
migration_applied=$(is_migration_applied $file)
if [[ "${migration_applied}" -eq "0" ]]; then
echo "Applying migration from file: $file"
local query="$(cat $MIGRATIONS_DIR/$file)"
if [[ "$query" == *"CREATE TABLE"* ]]; then # Disk configuration only will be applied in 'CREATE TABLE' queries
query="${query%;}"
[[ -n "$CLICKHOUSE_DISK_SETTINGS" ]] && query+=" SETTINGS $CLICKHOUSE_DISK_SETTINGS"
query+=";"
fi
clickhouse-client $ch_conn_str --query "$(echo "$query" | tr '\n' ' ')"
if [ $? -eq 0 ]; then
mark_migration_as_applied $file
else
echo "Failed to apply migration from $file"
exit 1
fi
else
echo "Migration $file was applied previously."
fi
done
echo "All migrations applied successfully."
}
# Main function to orchestrate migration steps
main() {
create_migration_table
run_migrations
}
main