Skip to content

Commit 6b969d0

Browse files
BuonOmorafiss
authored andcommitted
feat(testing): Automate cockroachdb setup
A non-trivial setup is necessary for cockroachdb to run quickly enough for tests. We provide a command to make that setup, and links to the doc for the ones who want to do it manually.
1 parent 71ba40a commit 6b969d0

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

CONTRIBUTING.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@ override and monkey-patch functionality.
1515

1616
## Setup and running tests
1717

18-
In CockroachDB, create two databases to be used by the ActiveRecord test suite:
18+
### CockroachDB
19+
20+
First, You should setup a cockroachdb local instance. You can use the
21+
`bin/start-cockroachdb` to help you with that task. Otherwise, once setup,
22+
create two databases to be used by the ActiveRecord test suite:
1923
activerecord_unittest and activerecord_unittest2.
2024

2125
```sql
2226
CREATE DATABASE activerecord_unittest;
23-
2427
CREATE DATABASE activerecord_unittest2;
2528
```
2629

bin/start-cockroachdb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env zsh
2+
3+
set -eu
4+
5+
die() { echo "$0: $*" 1>&2 ; false; }
6+
7+
root_dir="$(dirname $(dirname "$0:A"))"
8+
pid_file="$root_dir/tmp/cockroach.pid"
9+
log_file="$root_dir/tmp/cockroachdb.log"
10+
11+
mkdir -p "$root_dir/tmp"
12+
rm -f "$pid_file"
13+
14+
if ! (( ${+commands[cockroach]} )); then
15+
die 'the `cockroach` toolchain is not installed.
16+
See https://www.cockroachlabs.com/docs/stable/install-cockroachdb.html'
17+
fi
18+
19+
cockroach start-single-node \
20+
--insecure --store=type=mem,size=0.25 --advertise-addr=localhost --pid-file "$pid_file" \
21+
&> "$log_file" &
22+
23+
cockroach_pid=$!
24+
25+
until [[ -f "$pid_file" ]]; do
26+
sleep 1
27+
done
28+
29+
30+
cat <<-SQL | cockroach sql --insecure --host=localhost:26257 > /dev/null
31+
-- https://www.cockroachlabs.com/docs/stable/local-testing.html
32+
SET CLUSTER SETTING kv.raft_log.disable_synchronization_unsafe = true;
33+
SET CLUSTER SETTING kv.range_merge.queue_interval = '50ms';
34+
SET CLUSTER SETTING jobs.registry.interval.gc = '30s';
35+
SET CLUSTER SETTING jobs.registry.interval.cancel = '180s';
36+
SET CLUSTER SETTING jobs.retention_time = '15s';
37+
SET CLUSTER SETTING sql.stats.automatic_collection.enabled = false;
38+
SET CLUSTER SETTING kv.range_split.by_load_merge_delay = '5s';
39+
ALTER RANGE default CONFIGURE ZONE USING "gc.ttlseconds" = 600;
40+
ALTER DATABASE system CONFIGURE ZONE USING "gc.ttlseconds" = 600;
41+
42+
CREATE DATABASE activerecord_unittest;
43+
CREATE DATABASE activerecord_unittest2;
44+
SQL
45+
46+
tail -f "$log_file"
47+
48+
trap "kill $cockroach_pid" EXIT

0 commit comments

Comments
 (0)