Skip to content

chore: Backport #345 to 7-0-stable #355

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
root = true

[*.rb]
indent_style = space
indent_size = 2
trim_trailing_whitespace = true
insert_final_newline = true
79 changes: 42 additions & 37 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@
name: Test

on:
# Triggers the workflow on push or pull request events.
push:
# This should disable running the workflow on tags, according to the
# on.<push|pull_request>.<branches|tags> GitHub Actions docs.
branches:
- "*"
branches: [master]
# Triggers the workflow on pull request events.
pull_request:
types: [opened, reopened, synchronize]

Expand All @@ -18,40 +15,69 @@ on:

# This allows a subsequently queued workflow run to interrupt previous runs.
concurrency:
group: '${{ github.workflow }} @ ${{ github.event.pull_request.head.label || github.head_ref || github.ref }}'
group: "${{ github.workflow }} @ ${{ github.ref }}"
cancel-in-progress: true

jobs:
test:
# Since the name of the matrix job depends on the version, we define another job with a more stable name.
test_results:
if: ${{ always() }}
runs-on: ubuntu-latest
name: Test Results
needs: [test]
steps:
- run: |
result="${{ needs.test.result }}"
if [[ $result == "success" || $result == "skipped" ]]; then
exit 0
else
exit 1
fi

test:
runs-on: ubuntu-22.04
strategy:
fail-fast: false
matrix:
crdb: [v23.1.5]
ruby: [ruby-head]
# https://www.cockroachlabs.com/docs/releases/release-support-policy
crdb: [v23.2, v24.1]
ruby: [3.4]
name: Test (crdb=${{ matrix.crdb }} ruby=${{ matrix.ruby }})
steps:
- name: Set Up Actions
uses: actions/checkout@v3
- name: Install GEOS
run: sudo apt-get install libgeos-dev
run: sudo apt-get install -yqq libgeos-dev
- name: Set Up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true
ruby-version: ${{ matrix.ruby }}
bundler-cache: true
- name: Install and Start Cockroachdb
run: |
# Download CockroachDB
wget -qO- https://binaries.cockroachdb.com/cockroach-${{ matrix.crdb }}.linux-amd64.tgz | tar xvz
readonly full_version=$(ruby -rnet/http -ruri -ryaml -e '
link = "https://raw.githubusercontent.com/cockroachdb/docs/main/src/current/_data/releases.yml"
puts YAML.safe_load(Net::HTTP.get(URI(link))).reverse.find {
_1["major_version"] == "${{ matrix.crdb }}" &&
_1["release_type"] == "Production" &&
!_1["cloud_only"] &&
!_1["withdrawn"] &&
!_1["release_name"].include?("-") # Pre-release
}["release_name"]
')

export PATH=./cockroach-${{ matrix.crdb }}.linux-amd64/:$PATH
echo "Downloading $full_version..."
wget -qO- "https://binaries.cockroachdb.com/cockroach-$full_version.linux-amd64.tgz" | tar xvz

export PATH=./cockroach-$full_version.linux-amd64/:$PATH
readonly urlfile=cockroach-url

# Start a CockroachDB server and wait for it to become ready.
rm -f "$urlfile"
rm -rf cockroach-data
# Start CockroachDB.
cockroach start-single-node --max-sql-memory=25% --cache=25% --insecure --host=localhost --spatial-libs=./cockroach-${{ matrix.crdb }}.linux-amd64/lib --listening-url-file="$urlfile" >/dev/null 2>&1 &
cockroach start-single-node --max-sql-memory=25% --cache=25% --insecure --host=localhost --spatial-libs=./cockroach-$full_version.linux-amd64/lib --listening-url-file="$urlfile" >/dev/null 2>&1 &
# Ensure CockroachDB is stopped on script exit.
# Wait until CockroachDB has started.
for i in {0..3}; do
Expand All @@ -60,27 +86,6 @@ jobs:
echo "server not yet available; sleeping for $backoff seconds"
sleep $backoff
done
cockroach sql --insecure -e "
CREATE DATABASE activerecord_unittest;
CREATE DATABASE activerecord_unittest2;
SET CLUSTER SETTING sql.stats.automatic_collection.enabled = false;
SET CLUSTER SETTING sql.stats.histogram_collection.enabled = false;
SET CLUSTER SETTING jobs.retention_time = '180s';
SET CLUSTER SETTING sql.defaults.experimental_alter_column_type.enabled = 'true';

ALTER RANGE default CONFIGURE ZONE USING num_replicas = 1, gc.ttlseconds = 30;
ALTER TABLE system.public.jobs CONFIGURE ZONE USING num_replicas = 1, gc.ttlseconds = 30;
ALTER RANGE meta CONFIGURE ZONE USING num_replicas = 1, gc.ttlseconds = 30;
ALTER RANGE system CONFIGURE ZONE USING num_replicas = 1, gc.ttlseconds = 30;
ALTER RANGE liveness CONFIGURE ZONE USING num_replicas = 1, gc.ttlseconds = 30;

SET CLUSTER SETTING kv.range_merge.queue_interval = '50ms';
SET CLUSTER SETTING kv.raft_log.disable_synchronization_unsafe = 'true';
SET CLUSTER SETTING jobs.registry.interval.cancel = '180s';
SET CLUSTER SETTING jobs.registry.interval.gc = '30s';
SET CLUSTER SETTING kv.range_split.by_load_merge_delay = '5s';

SET CLUSTER SETTING sql.defaults.experimental_temporary_tables.enabled = 'true';
"
cat ${{ github.workspace }}/setup.sql | cockroach sql --insecure
- name: Test
run: bundle exec rake test
39 changes: 26 additions & 13 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Getting started


## ActiveRecord adapters and you

There are two repositories for the ActiveRecord adapter. The one you're in
Expand Down Expand Up @@ -121,7 +120,6 @@ master branch, with an alpha build of CockroachDB. it would be even
better to be able to test multiple versions of the adapter, and do so
against different versions of CockroachDB.


## Adding feature support

As CockroachDB improves, so do the features that can be supported in
Expand All @@ -131,15 +129,34 @@ gates should be toggled. Something that would help this process would be
linking those issues back to this adapter so that part of the feature
completing includes updating the adapter.


## Execute only tests that run with a connection

I have not investigated if this is already possible, but I would assume
no.

A possible way to approach this would be to add a shim to cause any
tests that use it to fail, and grep the tests that pass and then skip
them.
- Check for TODO or NOTE tags that are referencing the old or new version of
rails.
```bash
rg 'TODO|NOTE' --after-context=2
```
- Check postgresql_specific_schema.rb changelog in rails, and apply the changes
you want. Ex:
```bash
git diff v7.1.4..v7.2.1 -- $(fd postgresql_specific_schema)
```
- Verify the written text at the beginning of the test suite, there are likely
some changes in excluded tests.
- Check for some important methods, some will change for sure:
- [ ] `def new_column_from_field(`
- [ ] `def column_definitions(`
- [ ] `def pk_and_sequence_for(`
- [ ] `def foreign_keys(` and `def all_foreign_keys(`
- [ ] ...
- Check for setups containing `drop_table` in the test suite.
Especially if you have tons of failure, this is likely the cause.
- In the same way, run `test/cases/fixtures_test.rb` first, and check
if this corrupted the test database for other tests.
- For both of the above, the diff of `schema.rb` can be useful:
```bash
git diff v7.1.2..v7.2.1 -- activerecord/test/schema/schema.rb
```

## Publishing to Rubygems

Expand All @@ -151,7 +168,6 @@ gem build ...
gem publish <output file>
```


# Notes

When executing the test suite, each test file will reload fixtures. This
Expand Down Expand Up @@ -182,10 +198,8 @@ cleaned up, or skipped until passing.
The purpose of these was to make the tests grep-able while going through
all the failures.


[cockroachdb/cockroach#20753]: https://github.com/cockroachdb/cockroach/issues/20753#issuecomment-352810425


## Tracked test failures

Some of the skipped failures are:
Expand Down Expand Up @@ -224,7 +238,6 @@ most of the touchpoints including test failures and temporary monkey
patches. Some monkey patches were made directly to Rails, which will
need to be cleaned up.


# Notes for the non-Rubyer

rbenv is an environment manager that lets you manage and swap between
Expand Down
13 changes: 13 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ module RailsTag
def call
req = gemspec_requirement
"v" + all_activerecord_versions.find { req.satisfied_by?(_1) }.version
rescue => e
warn "Unable to determine Rails version. Using last used. Error: #{e.message}"
lockfile = File.expand_path("Gemfile.lock", __dir__)
File.foreach(lockfile, chomp: true).find { _1[/tag: (.*)$/] }
Regexp.last_match(1)
end

def gemspec_requirement
Expand Down Expand Up @@ -47,6 +52,14 @@ group :development, :test do
gem "byebug"
gem "minitest-excludes", "~> 2.0.1"

# Needed for the test suite
gem "msgpack", ">= 1.7.0"
gem "mutex_m", "~> 0.2.0"
gem "drb"
gem "bigdecimal"
gem "benchmark"
gem "logger"

# Gems used by the ActiveRecord test suite
gem "bcrypt", "~> 3.1.18"
gem "mocha", "~> 1.14.0"
Expand Down
2 changes: 2 additions & 0 deletions activerecord-cockroachdb-adapter.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Gem::Specification.new do |spec|
spec.add_dependency "activerecord", "~> 7.0.3"
spec.add_dependency "pg", "~> 1.2"
spec.add_dependency "rgeo-activerecord", "~> 7.0.0"
# See https://github.com/rails/rails/issues/54263
spec.add_dependency 'concurrent-ruby', '1.3.4'

spec.add_development_dependency "benchmark-ips", "~> 2.9.1"

Expand Down
50 changes: 17 additions & 33 deletions bin/console
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,23 @@ require "active_record"
# structure_load(Post.connection_db_config, "awesome-file.sql")
require "active_record/connection_adapters/cockroachdb/database_tasks"

begin
retried = false
ActiveRecord::Base.establish_connection(
#Alternative version: "cockroachdb://root@localhost:26257/ar_crdb_console"
adapter: "cockroachdb",
host: "localhost",
port: 26257,
user: "root",
database: "ar_crdb_console"
)
ActiveRecord::Base.connection
rescue ActiveRecord::NoDatabaseError
raise if retried
system("cockroach sql --insecure --host=localhost:26257 --execute='create database ar_crdb_console'",
exception: true)
retried = true
retry
end

class Post < ActiveRecord::Base
end

unless Post.table_exists?
migration = Class.new(ActiveRecord::Migration::Current) do
def up
create_table("posts") do |t|
t.string :title
t.text :body
end
end
end
migration.migrate(:up)
end
schema_kind = ENV.fetch("SCHEMA_KIND", "default")

system("cockroach sql --insecure --host=localhost:26257 --execute='drop database if exists ar_crdb_console'",
exception: true)
system("cockroach sql --insecure --host=localhost:26257 --execute='create database ar_crdb_console'",
exception: true)

ActiveRecord::Base.establish_connection(
#Alternative version: "cockroachdb://root@localhost:26257/ar_crdb_console"
adapter: "cockroachdb",
host: "localhost",
port: 26257,
user: "root",
database: "ar_crdb_console"
)

load "#{__dir__}/console_schemas/#{schema_kind}.rb"

require "irb"
IRB.start(__FILE__)
9 changes: 9 additions & 0 deletions bin/console_schemas/default.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Post < ActiveRecord::Base
end

ActiveRecord::Schema.define do
create_table("posts") do |t|
t.string :title
t.text :body
end
end
23 changes: 23 additions & 0 deletions bin/console_schemas/schemas.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Post < ActiveRecord::Base
self.table_name = "bar.posts"
end

class Comment < ActiveRecord::Base
self.table_name = "foo.comments"
end

ActiveRecord::Schema.define do
create_schema("foo")
create_schema("bar")
create_table("bar.posts") do |t|
t.string :title
t.text :body
end

create_table("foo.comments") do |t|
t.integer :post_id
t.text :body
end

add_foreign_key "foo.comments", "bar.posts", column: "post_id"
end
21 changes: 5 additions & 16 deletions bin/start-cockroachdb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pid_file="$root_dir/tmp/cockroach.pid"
log_file="$root_dir/tmp/cockroachdb.log"

mkdir -p "$root_dir/tmp"
[[ -f "$pid_file" ]] && kill -9 $(cat "$pid_file") || true
rm -f "$pid_file"

if ! (( ${+commands[cockroach]} )); then
Expand All @@ -17,7 +18,9 @@ See https://www.cockroachlabs.com/docs/stable/install-cockroachdb.html'
fi

cockroach start-single-node \
--insecure --store=type=mem,size=0.25 --advertise-addr=localhost --pid-file "$pid_file" \
--insecure --store=type=mem,size=0.25 --advertise-addr=localhost \
--spatial-libs="$(geos-config --includes)" \
--pid-file "$pid_file" \
&> "$log_file" &

cockroach_pid=$!
Expand All @@ -27,21 +30,7 @@ until [[ -f "$pid_file" ]]; do
done


cat <<-SQL | cockroach sql --insecure --host=localhost:26257 > /dev/null
-- https://www.cockroachlabs.com/docs/stable/local-testing.html
SET CLUSTER SETTING kv.raft_log.disable_synchronization_unsafe = true;
SET CLUSTER SETTING kv.range_merge.queue_interval = '50ms';
SET CLUSTER SETTING jobs.registry.interval.gc = '30s';
SET CLUSTER SETTING jobs.registry.interval.cancel = '180s';
SET CLUSTER SETTING jobs.retention_time = '15s';
SET CLUSTER SETTING sql.stats.automatic_collection.enabled = false;
SET CLUSTER SETTING kv.range_split.by_load_merge_delay = '5s';
ALTER RANGE default CONFIGURE ZONE USING "gc.ttlseconds" = 600;
ALTER DATABASE system CONFIGURE ZONE USING "gc.ttlseconds" = 600;

CREATE DATABASE activerecord_unittest;
CREATE DATABASE activerecord_unittest2;
SQL
cat "$root_dir/setup.sql" | cockroach sql --insecure --host=localhost:26257 > /dev/null

tail -f "$log_file"

Expand Down
Loading
Loading