-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathMakefile
More file actions
324 lines (287 loc) · 12.5 KB
/
Copy pathMakefile
File metadata and controls
324 lines (287 loc) · 12.5 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
include .envrc
# ==================================================================================== #
# HELPERS
# ==================================================================================== #
## help: print this help message
.PHONY: help
help:
@echo 'Usage:'
@sed -n 's/^##//p' ${MAKEFILE_LIST} | column -t -s ':' | sed -e 's/^/ /'
.PHONY: confirm
confirm:
@echo -n 'Are you sure? [y/N] ' && read ans && [ $${ans:-N} = y ]
# ==================================================================================== #
# DEVELOPMENT
# ==================================================================================== #
## run/serve: build and run a sqlpipe server
.PHONY: run/serve
run/serve: build
./bin/sqlpipe serve \
--dsn=postgres://postgres:${SQLPIPE-PASSWORD}@localhost/sqlpipe?sslmode=disable \
--secret "${SECRET}" \
--max-concurrency 20
## run/init: create a new db, set it up, migrate it, then start a new sqlpipe server
.PHONY: run/init
run/init: db/init
go build -ldflags=${linker_flags} -o=./bin/sqlpipe ./cmd;
./bin/sqlpipe serve \
--dsn=postgres://postgres:${SQLPIPE-PASSWORD}@localhost/sqlpipe?sslmode=disable \
--admin-username=sqlpipe \
--admin-password=${SQLPIPE-PASSWORD} \
--secret "${SECRET}" \
--max-concurrency 20 \
--create-admin
## run/replicate: build and run a cli replication
.PHONY: run/replicate
run/replicate: build
./sqlpipe replicate \
--source-ds-type postgresql \
--source-hostname localhost \
--source-port 5432 \
--source-db-name testing \
--source-username postgres \
--source-password ${SQLPIPE-PASSWORD} \
--target-ds-type postgresql \
--target-hostname localhost \
--target-port 5432 \
--target-username postgres \
--target-schema public \
--target-password ${SQLPIPE-PASSWORD} \
--target-db-name postgres \
--tables="wide_table" \
--force
## db/init: Initialize a fresh instance of postgresql
.PHONY: db/init
db/init:
docker container rm -f sqlpipe-postgresql;
docker container run -d -p 5432:5432 --name sqlpipe-postgresql -e POSTGRES_PASSWORD=${SQLPIPE-PASSWORD} postgres:14.1
sleep 1
docker exec -it sqlpipe-postgresql psql postgres://postgres:${SQLPIPE-PASSWORD}@localhost/postgres?sslmode=disable -c 'CREATE DATABASE sqlpipe'
go run ./cmd initialize --dsn=postgres://postgres:${SQLPIPE-PASSWORD}@localhost/sqlpipe?sslmode=disable --force
## db/backend: connect to the backend database as postgres user
.PHONY: db/backend
db/backend:
docker exec -it sqlpipe-postgresql psql postgres://postgres:${SQLPIPE-PASSWORD}@localhost/sqlpipe?sslmode=disable
## docker/prune: Prune unused docker stuff
.PHONY: docker/prune
docker/prune:
@echo 'Pruning unused docker objects'
docker system prune -f --volumes
## env/insert: Insert a few record for testing
.PHONY: env/insert
env/insert:
@echo 'inserting a few records in each table'
# insert connections
curl -u sqlpipe:${SQLPIPE-PASSWORD} -k -i -d '{"name": "prod", "dsType": "postgresql", "hostname": "localhost", "port": 5432, "dbName": "sqlpipe", "username": "postgres", "password": "${SQLPIPE-PASSWORD}", "skipTest": true}' https://localhost:9000/api/v1/connections
curl -u sqlpipe:${SQLPIPE-PASSWORD} -k -i -d '{"name": "postgresql", "dsType": "postgresql", "hostname": "${postgresqlHostname}", "port": 5432, "dbName": "testing", "username": "sqlpipe", "password": "${SQLPIPE-PASSWORD}"}' https://localhost:9000/api/v1/connections;
curl -u sqlpipe:${SQLPIPE-PASSWORD} -k -i -d '{"name": "mysql", "dsType": "mysql", "hostname": "${mysqlHostname}", "port": 3306, "dbName": "testing", "username": "sqlpipe", "password": "${SQLPIPE-PASSWORD}"}' https://localhost:9000/api/v1/connections;
curl -u sqlpipe:${SQLPIPE-PASSWORD} -k -i -d '{"name": "mssql", "dsType": "mssql", "hostname": "${mssqlHostname}", "port": 1433, "dbName": "testing", "username": "sqlpipe", "password": "${SQLPIPE-PASSWORD}"}' https://localhost:9000/api/v1/connections;
curl -u sqlpipe:${SQLPIPE-PASSWORD} -k -i -d '{"name": "oracle", "dsType": "oracle", "hostname": "${oracleHostname}", "port": 1521, "dbName": "testing", "username": "sqlpipe", "password": "${SQLPIPE-PASSWORD}"}' https://localhost:9000/api/v1/connections;
curl -u sqlpipe:${SQLPIPE-PASSWORD} -k -i -d '{"name": "redshift", "dsType": "redshift", "hostname": "${redshiftHostname}", "port": 5439, "dbName": "testing", "username": "sqlpipe", "password": "${SQLPIPE-PASSWORD}"}' https://localhost:9000/api/v1/connections;
curl -u sqlpipe:${SQLPIPE-PASSWORD} -k -i -d '{"name": "snowflake", "dsType": "snowflake", "accountId": "${snowflakeAccountId}", "dbName": "testing", "username": "${snowflakeUsername}", "password": "${snowflakePassword}"}' https://localhost:9000/api/v1/connections;
# insert a transfer
curl -u sqlpipe:${SQLPIPE-PASSWORD} -k -i -d '{"sourceId": 1, "targetId": 1, "query": "select * from connections", "targetSchema": "public", "targetTable": "mytarget", "overwrite": true}' https://localhost:9000/api/v1/transfers
# insert a couple queries
curl -u sqlpipe:${SQLPIPE-PASSWORD} -k -i -d '{"connectionId": 1, "query": "create table newtable (id int)"}' https://localhost:9000/api/v1/queries
curl -u sqlpipe:${SQLPIPE-PASSWORD} -k -i -d '{"connectionId": 1, "query": "insert into newtable (id) values (1),(2)"}' https://localhost:9000/api/v1/queries
# env/spinup: Spinup cloud instances
.PHONY: env/spinup
env/spinup:
# aws rds create-db-instance \
# --db-instance-identifier sqlpipe-test-postgresql \
# --db-name testing \
# --backup-retention-period 0 \
# --db-instance-class db.t3.micro \
# --engine postgres \
# --no-multi-az \
# --vpc-security-group-ids ${rdsSecurityGroup} \
# --master-username sqlpipe \
# --master-user-password ${SQLPIPE-PASSWORD} \
# --storage-type gp2 \
# --allocated-storage 20 \
# --no-enable-performance-insights >/dev/null;
# aws rds create-db-instance \
# --db-instance-identifier sqlpipe-test-mysql \
# --db-name testing \
# --backup-retention-period 0 \
# --db-instance-class db.t3.micro \
# --engine mysql \
# --no-multi-az \
# --vpc-security-group-ids ${rdsSecurityGroup} \
# --master-username sqlpipe \
# --master-user-password ${SQLPIPE-PASSWORD} \
# --storage-type gp2 \
# --allocated-storage 20 \
# --no-enable-performance-insights >/dev/null;
# aws rds create-db-instance \
# --db-instance-identifier sqlpipe-test-mssql \
# --backup-retention-period 0 \
# --db-instance-class db.t3.small \
# --engine sqlserver-web \
# --no-multi-az \
# --vpc-security-group-ids ${rdsSecurityGroup} \
# --master-username sqlpipe \
# --master-user-password ${SQLPIPE-PASSWORD} \
# --storage-type gp2 \
# --allocated-storage 20 \
# --license-model license-included \
# --no-enable-performance-insights >/dev/null;
aws rds create-db-instance \
--db-instance-identifier sqlpipe-test-oracle \
--db-name testing \
--backup-retention-period 0 \
--db-instance-class db.t3.small \
--engine oracle-se2 \
--no-multi-az \
--vpc-security-group-ids ${rdsSecurityGroup} \
--master-username sqlpipe \
--master-user-password ${SQLPIPE-PASSWORD} \
--storage-type gp2 \
--allocated-storage 20 \
--license-model license-included \
--no-enable-performance-insights >/dev/null;
# aws redshift create-cluster \
# --node-type dc2.large \
# --master-username sqlpipe \
# --db-name testing \
# --cluster-type single-node \
# --master-user-password ${SQLPIPE-PASSWORD} \
# --vpc-security-group-ids ${rdsSecurityGroup} \
# --cluster-identifier sqlpipe-test-redshift >/dev/null;
# env/teardown: Spin down cloud instances
.PHONY: env/teardown
env/teardown:
aws rds delete-db-instance --db-instance-identifier sqlpipe-test-postgresql --skip-final-snapshot &> /dev/null;
# aws rds delete-db-instance --db-instance-identifier sqlpipe-test-mysql --skip-final-snapshot &> /dev/null;
aws rds delete-db-instance --db-instance-identifier sqlpipe-test-mssql --skip-final-snapshot &> /dev/null;
# aws rds delete-db-instance --db-instance-identifier sqlpipe-test-oracle --skip-final-snapshot &> /dev/null;
# aws redshift delete-cluster --cluster-identifier sqlpipe-test-redshift --skip-final-cluster-snapshot &> /dev/null;
# env/video: Spinup cloud instances
.PHONY: env/video
env/video:
aws rds create-db-instance \
--db-instance-identifier sqlpipe-video-postgresql \
--db-name video \
--backup-retention-period 0 \
--db-instance-class db.t3.micro \
--engine postgres \
--no-multi-az \
--vpc-security-group-ids ${rdsSecurityGroup} \
--master-username testuser \
--master-user-password MyTestSecret456 \
--storage-type gp2 \
--allocated-storage 20 \
--no-enable-performance-insights >/dev/null;
# aws rds create-db-instance \
# --db-instance-identifier sqlpipe-video-mysql \
# --db-name video \
# --backup-retention-period 0 \
# --db-instance-class db.t3.micro \
# --engine mysql \
# --no-multi-az \
# --vpc-security-group-ids ${rdsSecurityGroup} \
# --master-username testuser \
# --master-user-password MyTestSecret456 \
# --storage-type gp2 \
# --allocated-storage 20 \
# --no-enable-performance-insights >/dev/null;
# aws rds create-db-instance \
# --db-instance-identifier sqlpipe-test-mssql \
# --backup-retention-period 0 \
# --db-instance-class db.t3.small \
# --engine sqlserver-web \
# --no-multi-az \
# --vpc-security-group-ids ${rdsSecurityGroup} \
# --master-username sqlpipe \
# --master-user-password MyTestSecret456 \
# --storage-type gp2 \
# --allocated-storage 20 \
# --license-model license-included \
# --no-enable-performance-insights >/dev/null;
# aws rds create-db-instance \
# --db-instance-identifier sqlpipe-test-oracle \
# --db-name testing \
# --backup-retention-period 0 \
# --db-instance-class db.t3.small \
# --engine oracle-se2 \
# --no-multi-az \
# --vpc-security-group-ids ${rdsSecurityGroup} \
# --master-username sqlpipe \
# --master-user-password MyTestSecret456 \
# --storage-type gp2 \
# --allocated-storage 20 \
# --license-model license-included \
# --no-enable-performance-insights >/dev/null;
aws redshift create-cluster \
--node-type dc2.large \
--master-username testuser \
--db-name video \
--cluster-type single-node \
--master-user-password MyTestSecret456 \
--vpc-security-group-ids ${rdsSecurityGroup} \
--cluster-identifier sqlpipe-test-redshift >/dev/null;
# db/postgresql: Open shell to PostgreSQL testing DB
.PHONY: db/postgresql
db/postgresql:
PGPASSWORD=${postgresqlPassword} psql -h ${postgresqlHostname} -U ${postgresqlUsername} -d ${postgresqlDbName}
# db/redshift: Open shell to redshift testing DB
.PHONY: db/redshift
db/redshift:
PGPASSWORD=${redshiftPassword} psql -h ${redshiftHostname} -U ${redshiftUsername} -d ${redshiftDbName} -p 5439
# db/mysql: Open shell to MySQL testing DB
.PHONY: db/mysql
db/mysql:
mysql -h ${mysqlHostname} -u ${mysqlUsername} --password=${mysqlPassword} -D ${mysqlDbName}
# db/mssql: Open shell to MSSQL testing DB
.PHONY: db/mssql
db/mssql:
sqlcmd -S ${mssqlHostname}
# test: Test stuff
.PHONY: test
test:
go test -v -count=1 -run Setup ./...
go test -v -count=1 -run Transfers ./...
# setup: setup stuff
.PHONY: setup
setup:
go test -v -count=1 -run Setup ./...
# loadtest: Test load
.PHONY: loadtest
loadtest:
curl -u sqlpipe:${SQLPIPE} -k -i -d '{"sourceId": 2, "targetId": 2, "overwrite": true, "targetSchema": "public", "targetTable": "postgresql_load_table", "query": "select * from load_table"}' https://localhost:9000/api/v1/transfers;
# ==================================================================================== #
# QUALITY CONTROL
# ==================================================================================== #
## audit: tidy and vendor dependencies and format, vet and test all code
.PHONY: audit
audit: vendor
@echo 'Formatting code...'
go fmt ./...
@echo 'Vetting code...'
go vet ./...
# staticcheck ./...
@echo 'Running tests...'
go test -v -race -vet=off ./...
## vendor: tidy and vendor dependencies
.PHONY: vendor
vendor:
@echo 'Tidying and verifying module dependencies...'
go mod tidy
go mod verify
@echo 'Vendoring dependencies...'
go mod vendor
# ==================================================================================== #
# BUILD
# ==================================================================================== #
current_time = $(shell date -u +"%Y-%m-%dT%H:%M:%SZ")
git_description = $(shell git describe --always --dirty --tags --long)
linker_flags = '-s -X main.gitHash=${git_description}'
## build: build the application locally
.PHONY: build
build:
@echo 'Building cmd/sqlpipe...'
go build -ldflags=${linker_flags} -o=./sqlpipe ./cmd
## publish: build and publish app
.PHONY: publish
publish:
sh build.sh