Skip to content

Commit b4bf6cb

Browse files
committed
chore(devex): Ease development
- Allow using various schemas for the console, and always start with a clean slate. - If no internet, select latest used Rails version. - Allow to easily show schema loading logs. - Closer CRDB configuration between CI and local dev. - Add editor config file.
1 parent 98cd7d2 commit b4bf6cb

File tree

7 files changed

+72
-38
lines changed

7 files changed

+72
-38
lines changed

.editorconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
root = true
2+
3+
[*.rb]
4+
indent_style = space
5+
indent_size = 2
6+
trim_trailing_whitespace = true
7+
insert_final_newline = true

Gemfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ module RailsTag
1010
def call
1111
req = gemspec_requirement
1212
"v" + all_activerecord_versions.find { req.satisfied_by?(_1) }.version
13+
rescue => e
14+
warn "Unable to determine Rails version. Using last used. Error: #{e.message}"
15+
lockfile = File.expand_path("Gemfile.lock", __dir__)
16+
File.foreach(lockfile, chomp: true).find { _1[/tag: (.*)$/] }
17+
Regexp.last_match(1)
1318
end
1419

1520
def gemspec_requirement

bin/console

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,39 +12,23 @@ require "active_record"
1212
# structure_load(Post.connection_db_config, "awesome-file.sql")
1313
require "active_record/connection_adapters/cockroachdb/database_tasks"
1414

15-
begin
16-
retried = false
17-
ActiveRecord::Base.establish_connection(
18-
#Alternative version: "cockroachdb://root@localhost:26257/ar_crdb_console"
19-
adapter: "cockroachdb",
20-
host: "localhost",
21-
port: 26257,
22-
user: "root",
23-
database: "ar_crdb_console"
24-
)
25-
ActiveRecord::Base.connection
26-
rescue ActiveRecord::NoDatabaseError
27-
raise if retried
28-
system("cockroach sql --insecure --host=localhost:26257 --execute='create database ar_crdb_console'",
29-
exception: true)
30-
retried = true
31-
retry
32-
end
33-
34-
class Post < ActiveRecord::Base
35-
end
36-
37-
unless Post.table_exists?
38-
migration = Class.new(ActiveRecord::Migration::Current) do
39-
def up
40-
create_table("posts") do |t|
41-
t.string :title
42-
t.text :body
43-
end
44-
end
45-
end
46-
migration.migrate(:up)
47-
end
15+
schema_kind = ENV.fetch("SCHEMA_KIND", "default")
16+
17+
system("cockroach sql --insecure --host=localhost:26257 --execute='drop database if exists ar_crdb_console'",
18+
exception: true)
19+
system("cockroach sql --insecure --host=localhost:26257 --execute='create database ar_crdb_console'",
20+
exception: true)
21+
22+
ActiveRecord::Base.establish_connection(
23+
#Alternative version: "cockroachdb://root@localhost:26257/ar_crdb_console"
24+
adapter: "cockroachdb",
25+
host: "localhost",
26+
port: 26257,
27+
user: "root",
28+
database: "ar_crdb_console"
29+
)
30+
31+
load "#{__dir__}/console_schemas/#{schema_kind}.rb"
4832

4933
require "irb"
5034
IRB.start(__FILE__)

bin/console_schemas/default.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Post < ActiveRecord::Base
2+
end
3+
4+
ActiveRecord::Schema.define do
5+
create_table("posts") do |t|
6+
t.string :title
7+
t.text :body
8+
end
9+
end

bin/console_schemas/schemas.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Post < ActiveRecord::Base
2+
self.table_name = "bar.posts"
3+
end
4+
5+
class Comment < ActiveRecord::Base
6+
self.table_name = "foo.comments"
7+
end
8+
9+
ActiveRecord::Schema.define do
10+
create_schema("foo")
11+
create_schema("bar")
12+
create_table("bar.posts") do |t|
13+
t.string :title
14+
t.text :body
15+
end
16+
17+
create_table("foo.comments") do |t|
18+
t.integer :post_id
19+
t.text :body
20+
end
21+
22+
add_foreign_key "foo.comments", "bar.posts", column: "post_id"
23+
end

bin/start-cockroachdb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pid_file="$root_dir/tmp/cockroach.pid"
99
log_file="$root_dir/tmp/cockroachdb.log"
1010

1111
mkdir -p "$root_dir/tmp"
12+
[[ -f "$pid_file" ]] && kill -9 $(cat "$pid_file")
1213
rm -f "$pid_file"
1314

1415
if ! (( ${+commands[cockroach]} )); then
@@ -41,6 +42,9 @@ ALTER DATABASE system CONFIGURE ZONE USING "gc.ttlseconds" = 600;
4142
4243
CREATE DATABASE activerecord_unittest;
4344
CREATE DATABASE activerecord_unittest2;
45+
46+
SET CLUSTER SETTING sql.defaults.experimental_alter_column_type.enabled = 'true';
47+
SET CLUSTER SETTING sql.defaults.experimental_temporary_tables.enabled = 'true';
4448
SQL
4549

4650
tail -f "$log_file"

test/cases/helper.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,12 @@ def clean_up_connection_handler
194194
end
195195
end
196196

197-
def load_schema
198-
# silence verbose schema loading
199-
original_stdout = $stdout
200-
$stdout = StringIO.new
197+
def load_schema(shush = true)
198+
if shush
199+
# silence verbose schema loading
200+
original_stdout = $stdout
201+
$stdout = StringIO.new
202+
end
201203

202204
adapter_name = ActiveRecord::Base.connection.adapter_name.downcase
203205
adapter_specific_schema_file = SCHEMA_ROOT + "/#{adapter_name}_specific_schema.rb"
@@ -210,7 +212,7 @@ def load_schema
210212

211213
ActiveRecord::FixtureSet.reset_cache
212214
ensure
213-
$stdout = original_stdout
215+
$stdout = original_stdout if shush
214216
end
215217

216218
if ENV['COCKROACH_LOAD_FROM_TEMPLATE'].nil? && ENV['COCKROACH_SKIP_LOAD_SCHEMA'].nil?

0 commit comments

Comments
 (0)