-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreusable-ruby-publish-migrated-db.yaml
More file actions
244 lines (219 loc) · 8.87 KB
/
reusable-ruby-publish-migrated-db.yaml
File metadata and controls
244 lines (219 loc) · 8.87 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
name: Reusable Ruby publish migrated DB
on:
workflow_call:
secrets:
appSecret:
description: GitHub App private key (PEM) for generating a token
required: false
inputs:
appId:
description: GitHub App ID for generating a token
required: false
type: string
appInstallationId:
description: Optional GitHub App installation ID
required: false
type: string
workingDirectory:
description: Working direction for action
default: ./
type: string
required: false
rubyVersion:
description: Ruby version to use
default: '2.7.5'
type: string
required: false
nodeVersion:
description: Node version to use
default: '20'
type: string
required: false
jobs:
run:
runs-on: ubuntu-latest
defaults:
run:
working-directory: ${{ inputs.workingDirectory }}
steps:
- name: Start postgres Docker
run: |
sudo systemctl stop postgresql.service
docker run -d --name postgres \
-e POSTGRES_PASSWORD=postgres -e POSTGRES_USER=postgres -e PGDATA=/data \
-p 5432:5432 postgres:alpine;
until docker exec postgres pg_isready -U postgres; do
sleep 1
done
- name: Create postgres users
run: |
docker exec postgres psql -U postgres -c "CREATE USER rails WITH SUPERUSER PASSWORD 'rails';"
docker exec postgres psql -U postgres -c "CREATE USER rees46 WITH SUPERUSER PASSWORD 'rees46';"
docker exec postgres rm -rf /docker-entrypoint-initdb.d/*
- id: app-token
if: ${{ inputs.appId }}
uses: actions/create-github-app-token@v1
with:
app-id: ${{ inputs.appId }}
private-key: ${{ secrets.appSecret }}
owner: ${{ github.repository_owner }}
- uses: actions/checkout@v5
with:
submodules: 'true'
token: ${{ steps.app-token.outputs.token || github.token }}
- name: Start clickhouse server
run: |
docker run -d \
--name clickhouse_server \
-p 8123:8123 \
-p 9000:9000 \
-p 9009:9009 \
-p 9004:9004 \
-p 9005:9005 \
--ulimit nofile=262144:262144 \
yandex/clickhouse-server:22-alpine;
docker cp ./config/clickhouse-config.xml clickhouse_server:/etc/clickhouse-server/config.xml
- uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ inputs.rubyVersion }}
bundler-cache: true
- name: Prepare configs
run: |
mv ./config/database.yml.example ./config/database.yml
mv ./config/secrets.yml.example ./config/secrets.yml
file="./config/database.yml"
awk '
{
gsub(/<<: \*default_clickhouse_pg/, "<<: *default_clickhouse")
if ($0 ~ /default_clickhouse:/) {
in_default_clickhouse = 1
}
if ($0 ~ /development_clickhouse:/) {
in_development_clickhouse = 1
}
if (in_development_clickhouse && $0 ~ /<<: \*default_clickhouse_pg/) {
$0 = " <<: *default_clickhouse"
in_development_clickhouse = 0
}
print
}' "$file" > tmp && mv tmp "$file"
echo "$(cat $file)"
- name: Run clickhouse migrations
run: |
bundle exec rake clickhouse:create
bundle exec rake clickhouse:schema:load
docker exec clickhouse_server clickhouse-client --query="rename database rees46 to rees46_test"
bundle exec rake clickhouse:create
bundle exec rake clickhouse:schema:load
docker exec clickhouse_server clickhouse-client --query="show databases"
docker exec clickhouse_server clickhouse-client --query="show tables from rees46_test"
docker exec clickhouse_server clickhouse-client --query="show tables from rees46"
- name: Publish clickhouse server docker image
env:
REPOSITORY_OWNER: ${{ github.repository_owner }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
docker exec clickhouse_server cp -r /var/lib/clickhouse/. /data/
docker exec clickhouse_server sed -i 's|<path>/var/lib/clickhouse</path>|<path>/data</path>|g' /etc/clickhouse-server/config.xml
docker stop clickhouse_server
echo $GITHUB_TOKEN | docker login ghcr.io -u $REPOSITORY_OWNER --password-stdin
docker commit clickhouse_server ghcr.io/rees46/clickhouse-server-migrated:latest
docker push ghcr.io/rees46/clickhouse-server-migrated:latest
- name: Run postgres migrations test
env:
RAILS_ENV: test
DATABASE_TEST_URL: postgres://rees46:rees46@localhost:5432/rees46_test
DATABASE_CLICK_TEST_URL: postgres://rees46:rees46@localhost:5432/rees46_clickhouse_test
run: |
bundle exec rake db:create
bundle exec rake db:functions:install || true
bundle exec rake db:schema:load
bundle exec rake clickhouse:create
bundle exec rake clickhouse:schema:load -- --simple
- name: Run postgres migrations development
env:
PG_PASSWORD: rees46
PG_USER: rees46
RAILS_ENV: development
run: |
bundle exec rake db:create
bundle exec rake db:functions:install || true
bundle exec rake db:schema:load
- name: Run seeds dev
env:
RAILS_ENV: development
PG_PASSWORD: rees46
PG_USER: rees46
run: |
bundle exec rake db:seed
- name: Run seeds test
env:
RAILS_ENV: test
DATABASE_TEST_URL: postgres://rees46:rees46@localhost:5432/rees46_test
DATABASE_CLICK_TEST_URL: postgres://rees46:rees46@localhost:5432/rees46_clickhouse_test
run: |
bundle exec rake db:seed
- name: Publish postgres docker image
env:
REPOSITORY_OWNER: ${{ github.repository_owner }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
docker exec postgres psql -U postgres -c "SELECT datname FROM pg_database WHERE datistemplate = false;"
echo $(docker exec postgres psql -U rees46 -W rees46 -d rees46_test -c "\dt;")
echo $GITHUB_TOKEN | docker login ghcr.io -u $REPOSITORY_OWNER --password-stdin
docker commit --change "ENV PGDATA=/data" postgres ghcr.io/rees46/postgres-migrated:latest
docker push ghcr.io/rees46/postgres-migrated:latest
- name: Start clickhouse
run: |
docker stop clickhouse_server
docker run -d \
--name clickhouse \
-e CLICKHOUSE_SKIP_USER_SETUP=1 \
-p 8123:8123 \
-p 9000:9000 \
-p 9009:9009 \
-p 9004:9004 \
-p 9005:9005 \
--ulimit nofile=262144:262144 \
clickhouse/clickhouse-server
timeout 20s bash -c '
until curl -fsS http://localhost:8123/ping | grep -qx "Ok."; do
sleep 1
done
'
cat <<'EOF' > /tmp/99-data-path.xml
<clickhouse>
<path>/data/</path>
<tmp_path>/data/tmp/</tmp_path>
<user_files_path>/data/user_files/</user_files_path>
<format_schema_path>/data/format_schemas/</format_schema_path>
</clickhouse>
EOF
docker cp /tmp/99-data-path.xml clickhouse:/etc/clickhouse-server/config.d/99-data-path.xml
docker exec clickhouse mkdir -p /data/tmp /data/user_files /data/format_schemas
docker exec clickhouse chown -R clickhouse:clickhouse /data
docker restart clickhouse
timeout 20s bash -c '
until curl -fsS http://localhost:8123/ping | grep -qx "Ok."; do
sleep 1
done
'
- name: Run clickhouse migrations
run: |
bundle exec rake clickhouse:create
bundle exec rake clickhouse:schema:load
docker exec clickhouse clickhouse-client --query="rename database rees46 to rees46_test"
bundle exec rake clickhouse:create
bundle exec rake clickhouse:schema:load
docker exec clickhouse clickhouse-client --query="show databases"
docker exec clickhouse clickhouse-client --query="show tables from rees46_test"
docker exec clickhouse clickhouse-client --query="show tables from rees46"
- name: Publish clickhouse docker image
env:
REPOSITORY_OWNER: ${{ github.repository_owner }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
docker stop clickhouse
echo $GITHUB_TOKEN | docker login ghcr.io -u $REPOSITORY_OWNER --password-stdin
docker commit clickhouse ghcr.io/rees46/clickhouse-migrated:latest
docker push ghcr.io/rees46/clickhouse-migrated:latest