Skip to content

Commit 0c60b9c

Browse files
authored
refactor: various changes (#274)
Fix console command. Allow for TEST_FILES and TEST_FILES_AR env to coexist. Remove undocumented envs and useless code.
1 parent 62b6a6d commit 0c60b9c

File tree

5 files changed

+74
-58
lines changed

5 files changed

+74
-58
lines changed

Rakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ end
2626

2727
Rake::TestTask.new do |t|
2828
t.libs = ARTest::CockroachDB.test_load_paths
29-
t.test_files = test_files
29+
t.test_files = RakeHelpers.test_files
3030
t.warning = !!ENV["WARNING"]
3131
t.verbose = false
3232
end

bin/console

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,49 @@
11
#!/usr/bin/env ruby
22

3-
require "bundler/setup"
4-
require "activerecord/cockroachdb"
3+
$:.unshift(File.expand_path("../lib", __dir__))
54

6-
# You can add fixtures and/or initialization code here to make experimenting
7-
# with your gem easier. You can also use a different console, if you like.
5+
# require "bundler/setup"
6+
# Bundler.require :development
87

9-
# (If you use this, don't forget to add pry to your Gemfile!)
10-
# require "pry"
11-
# Pry.start
8+
require "active_record"
9+
# This allows playing with the rake task as well. Ex:
10+
#
11+
# ActiveRecord::Tasks::DatabaseTasks.
12+
# structure_load(Post.connection_db_config, "awesome-file.sql")
13+
require "active_record/connection_adapters/cockroachdb/database_tasks"
14+
15+
begin
16+
retried = false
17+
ActiveRecord::Base.establish_connection(
18+
adapter: "cockroachdb",
19+
host: "localhost",
20+
port: 26257,
21+
user: "root",
22+
database: "ar_crdb_console"
23+
)
24+
ActiveRecord::Base.connection
25+
rescue ActiveRecord::NoDatabaseError
26+
raise if retried
27+
system("cockroach sql --insecure --host=localhost:26257 --execute='create database ar_crdb_console'",
28+
exception: true)
29+
retried = true
30+
retry
31+
end
32+
33+
class Post < ActiveRecord::Base
34+
end
35+
36+
unless Post.table_exists?
37+
migration = Class.new(ActiveRecord::Migration::Current) do
38+
def up
39+
create_table("posts") do |t|
40+
t.string :title
41+
t.text :body
42+
end
43+
end
44+
end
45+
migration.migrate(:up)
46+
end
1247

1348
require "irb"
1449
IRB.start(__FILE__)

build/teamcity-test.sh

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,7 @@ run_cockroach
7070

7171
if ! (RUBYOPT="-W0" TESTOPTS="-v" bundle exec rake test); then
7272
echo "Tests failed"
73-
HAS_FAILED=1
74-
else
75-
echo "Tests passed"
76-
HAS_FAILED=0
73+
exit 1
7774
fi
7875

79-
if [ $HAS_FAILED -eq 1 ]; then
80-
exit 1
81-
fi
76+
echo "Tests passed"

lib/activerecord-cockroachdb-adapter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
if defined?(Rails)
1+
if defined?(Rails::Railtie)
22
module ActiveRecord
33
module ConnectionAdapters
44
class CockroachDBRailtie < ::Rails::Railtie

test/support/rake_helpers.rb

Lines changed: 28 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,38 @@
1-
COCKROACHDB_TEST_HELPER = 'test/cases/helper_cockroachdb.rb'
2-
3-
def test_files
4-
env_activerecord_test_files ||
5-
env_cockroachdb_test_files ||
6-
only_activerecord_test_files ||
7-
only_cockroachdb_test_files ||
8-
all_test_files
9-
end
1+
# frozen_string_literal: true
102

11-
def env_activerecord_test_files
12-
return unless ENV['TEST_FILES_AR'] && !ENV['TEST_FILES_AR'].empty?
3+
module RakeHelpers
4+
COCKROACHDB_TEST_HELPER = 'test/cases/helper_cockroachdb.rb'
135

14-
@env_ar_test_files ||= ENV['TEST_FILES_AR'].
15-
split(',').
16-
map { |file| File.join ARTest::CockroachDB.root_activerecord, file.strip }.
17-
sort.
18-
prepend(COCKROACHDB_TEST_HELPER)
19-
end
6+
module_function
207

21-
def env_cockroachdb_test_files
22-
return unless ENV['TEST_FILES'] && !ENV['TEST_FILES'].empty?
8+
# Look for TEST_FILES_AR or TEST_FILES env variables
9+
# to set specific tests, otherwise load every tests
10+
# from active_record and this adapter.
11+
def test_files
12+
ar_test_files = ENV.fetch('TEST_FILES_AR', '')
13+
cr_test_files = ENV.fetch('TEST_FILES', '')
2314

24-
@env_test_files ||= ENV['TEST_FILES'].split(',').map(&:strip)
25-
end
15+
return all_test_file if ar_test_files.empty? && cr_test_files.empty?
2616

27-
def only_activerecord_test_files
28-
return unless ENV['ONLY_TEST_AR']
29-
activerecord_test_files
30-
end
17+
ar_test_files = ar_test_files.
18+
split(',').
19+
map { |file| File.join ARTest::CockroachDB.root_activerecord, file.strip }.
20+
then { _1.prepend(COCKROACHDB_TEST_HELPER) unless _1.empty? }.
21+
prepend(COCKROACHDB_TEST_HELPER)
3122

32-
def only_cockroachdb_test_files
33-
return unless ENV['ONLY_TEST_COCKROACHDB']
34-
cockroachdb_test_files
35-
end
23+
cr_test_files = cr_test_files.split(',').map(&:strip)
3624

37-
def all_test_files
38-
activerecord_test_files + cockroachdb_test_files
39-
end
25+
ar_test_files + cr_test_files
26+
end
4027

41-
def activerecord_test_files
42-
Dir.
43-
glob("#{ARTest::CockroachDB.root_activerecord}/test/cases/**/*_test.rb").
44-
reject{ |x| x =~ /\/adapters\/mysql2\// }.
45-
reject{ |x| x =~ /\/adapters\/sqlite3\// }.
46-
sort.
47-
prepend(COCKROACHDB_TEST_HELPER)
48-
end
28+
def all_test_files
29+
activerecord_test_files = Dir.
30+
glob("#{ARTest::CockroachDB.root_activerecord}/test/cases/**/*_test.rb").
31+
reject { _1.match?(%r(/adapters/(?:mysql2|sqlite3)/) }.
32+
prepend(COCKROACHDB_TEST_HELPER)
33+
34+
cockroachdb_test_files = Dir.glob('test/cases/**/*_test.rb')
4935

50-
def cockroachdb_test_files
51-
Dir.glob('test/cases/**/*_test.rb')
36+
activerecord_test_files + cockroachdb_test_files
37+
end
5238
end

0 commit comments

Comments
 (0)