-
-
Save pnomolos/56684748c43a0ab68c8f to your computer and use it in GitHub Desktop.
Call-tree example for ActiveRecord vs. rom-sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'active_record' | |
require 'hotch' | |
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:') | |
ActiveRecord::Base.connection.execute("CREATE TABLE IF NOT EXISTS users(id int primary key, name varchar(255), email varchar(255), age int)"); | |
class User < ActiveRecord::Base | |
end | |
User.delete_all | |
Hotch() do | |
100.times do | |
User.create(name: 'Jane Doe', email: 'jane@doe.org', age: 21) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A sample Gemfile | |
source "https://rubygems.org" | |
gem 'activerecord' | |
gem 'hotch' | |
gem 'rom-sql' | |
gem 'sqlite3' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rom-sql' | |
require 'hotch' | |
setup = ROM.setup(:sql, 'sqlite::memory') | |
setup.default.connection.create_table(:users) do | |
primary_key :id | |
String :name | |
String :email | |
Integer :age | |
end | |
# Right now this needs to be done manually as the table schema is read at the ROM.setup call | |
setup.default.instance_variable_set(:@schema, setup.default.connection.tables) | |
ROM.commands(:users) do | |
define(:create) do | |
result :one | |
end | |
end | |
rom = ROM.finalize.env | |
create_user = rom.command(:users).create | |
Hotch() do | |
100.times do | |
create_user.call(name: 'Jane Doe', email: 'jane@doe.org', age: 21) | |
end | |
end |
Looks like it's a gotcha - when you setup an adapter it looks at existing tables and stores that for later inference. In your example you create tables during setup so that won't work.
Hard to say if it's a bug or not. Hmm
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You didn't register any command so it doesn't exist in the registry. I'm not sure what you wanted to check but if you only want to see the command then defining relation and mapper is not needed (ROM infers relations automatically from db schema if you didn't define any).