Skip to content

Commit

Permalink
chore(devex): Ease development
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
BuonOmo authored and rafiss committed Oct 2, 2023
1 parent b4378a8 commit e645a35
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 38 deletions.
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
5 changes: 5 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
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
4 changes: 4 additions & 0 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")
rm -f "$pid_file"

if ! (( ${+commands[cockroach]} )); then
Expand Down Expand Up @@ -41,6 +42,9 @@ ALTER DATABASE system CONFIGURE ZONE USING "gc.ttlseconds" = 600;
CREATE DATABASE activerecord_unittest;
CREATE DATABASE activerecord_unittest2;
SET CLUSTER SETTING sql.defaults.experimental_alter_column_type.enabled = 'true';
SET CLUSTER SETTING sql.defaults.experimental_temporary_tables.enabled = 'true';
SQL

tail -f "$log_file"
Expand Down
12 changes: 7 additions & 5 deletions test/cases/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,12 @@ def clean_up_connection_handler
end
end

def load_schema
# silence verbose schema loading
original_stdout = $stdout
$stdout = StringIO.new
def load_schema(shush = true)
if shush
# silence verbose schema loading
original_stdout = $stdout
$stdout = StringIO.new
end

adapter_name = ActiveRecord::Base.connection.adapter_name.downcase
adapter_specific_schema_file = SCHEMA_ROOT + "/#{adapter_name}_specific_schema.rb"
Expand All @@ -225,7 +227,7 @@ def load_schema

ActiveRecord::FixtureSet.reset_cache
ensure
$stdout = original_stdout
$stdout = original_stdout if shush
end

if ENV['COCKROACH_LOAD_FROM_TEMPLATE'].nil? && ENV['COCKROACH_SKIP_LOAD_SCHEMA'].nil?
Expand Down

0 comments on commit e645a35

Please sign in to comment.