-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy-table.sh
74 lines (59 loc) · 1.55 KB
/
copy-table.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
#!/bin/bash
set -e
# The script assumes:
# 1. You have created an Aito database schema
# 2. Your read-write environment API key is defined as API_KEY environment variable
# 3. "jq" is installed https://stedolan.github.io/jq/.
if [[ -z "$1" ]]; then
echo "Example usage: ./copy-table.sh your-env-name products" >&2
exit 1
fi
if [[ -z "$API_KEY" ]]; then
echo "API_KEY environment variable is not defined. Exiting .." >&2
exit 1
fi
if ! [ -x "$(command -v jq)" ]; then
echo "Error: jq is not installed." >&2
exit 1
fi
AITO_ENV="$1"
AITO_TABLE="$2"
if [ -d ".aitotmp" ]; then
echo ".aitotmp directory already exists, refusing to run."
echo "You can manually delete the directory and try running again."
echo "Exiting .."
exit 1
fi
echo "Creating .aitotmp working directory .."
mkdir .aitotmp
function cleanup {
echo "Removing .aitotmp working directory .."
rm -r .aitotmp
exit 0
}
trap cleanup EXIT
curl -X POST \
https://$AITO_ENV.api.aito.ai/api/v1/_search \
-H 'content-type: application/json' \
-H "x-api-key: $API_KEY" \
-d "
{
\"from\": \"$AITO_TABLE\",
\"limit\": 1
}" | jq -r '.total' > .aitotmp/totalrows
TOTAL_ROWS=$(cat .aitotmp/totalrows)
BATCH_SIZE=500
COUNTER=0
while [ $COUNTER -lt $(($TOTAL_ROWS)) ]; do
curl -X POST \
https://$AITO_ENV.api.aito.ai/api/v1/_search \
-H 'content-type: application/json' \
-H "x-api-key: $API_KEY" \
-d "
{
\"from\": \"$AITO_TABLE\",
\"limit\": $BATCH_SIZE,
\"offset\": $COUNTER
}" | jq -c '.hits[]'
let COUNTER=COUNTER+BATCH_SIZE
done