rom-rb / rom

Data mapping and persistence toolkit for Ruby
https://rom-rb.org
MIT License
2.09k stars 161 forks source link

Schema breaks configuration reuse #356

Closed cflipse closed 8 years ago

cflipse commented 8 years ago

(adapted from https://github.com/rom-rb/rom-sql/issues/93 )

Schemas break when you attempt to reuse them:

require 'bundler/inline'
require 'json'

gemfile(:install) do
  gem 'rom'
  gem 'rom-sql'                                                                    
  gem 'sqlite3'
end

class Users < ROM::Relation[:sql]                                                  
  schema(:users, infer: true)                                                      
end                                                                                

config = ROM::Configuration.new(:sql, "sqlite::memory")                            

gateway = config.gateways[:default]                                                
migrations = []                                                                    

mig = gateway.migration do                                                         
  change do                                                                        
    create_table :users do                                                         
      primary_key :id                                                              
      column :name, String, null: false                                            
    end                                                                            
  end                                                                              
end                                                                                

mig.apply(gateway.connection, :up)                           
config.register_relation Users

container1 = ROM.container(config)
container2 = ROM.container(config)     

Results in:

/home/flip/.gem/ruby/2.3.0/gems/rom-2.0.0/lib/rom/schema.rb:81:in `finalize!': can't modify frozen ROM::SQL::Schema (RuntimeError)
        from /home/flip/.gem/ruby/2.3.0/gems/rom-sql-0.8.0/lib/rom/sql/schema.rb:24:in `finalize!'
        from /home/flip/.gem/ruby/2.3.0/gems/rom-2.0.0/lib/rom/setup/finalize/finalize_relations.rb:53:in `build_relation'
        from /home/flip/.gem/ruby/2.3.0/gems/rom-2.0.0/lib/rom/setup/finalize/finalize_relations.rb:25:in `block (2 levels) in run!'
        from /home/flip/.gem/ruby/2.3.0/gems/rom-2.0.0/lib/rom/setup/finalize/finalize_relations.rb:24:in `each'
        from /home/flip/.gem/ruby/2.3.0/gems/rom-2.0.0/lib/rom/setup/finalize/finalize_relations.rb:24:in `block in run!'
        from /home/flip/.gem/ruby/2.3.0/gems/rom-2.0.0/lib/rom/relation_registry.rb:6:in `initialize'
        from /home/flip/.gem/ruby/2.3.0/gems/rom-2.0.0/lib/rom/setup/finalize/finalize_relations.rb:23:in `new'
        from /home/flip/.gem/ruby/2.3.0/gems/rom-2.0.0/lib/rom/setup/finalize/finalize_relations.rb:23:in `run!'
        from /home/flip/.gem/ruby/2.3.0/gems/rom-2.0.0/lib/rom/setup/finalize.rb:93:in `load_relations'
        from /home/flip/.gem/ruby/2.3.0/gems/rom-2.0.0/lib/rom/setup/finalize.rb:62:in `run!'
        from /home/flip/.gem/ruby/2.3.0/gems/rom-2.0.0/lib/rom/create_container.rb:35:in `finalize'
        from /home/flip/.gem/ruby/2.3.0/gems/rom-2.0.0/lib/rom/create_container.rb:13:in `initialize'
        from /home/flip/.gem/ruby/2.3.0/gems/rom-2.0.0/lib/rom/create_container.rb:54:in `initialize'
        from /home/flip/.gem/ruby/2.3.0/gems/rom-2.0.0/lib/rom/create_container.rb:59:in `new'
        from /home/flip/.gem/ruby/2.3.0/gems/rom-2.0.0/lib/rom/create_container.rb:59:in `container'
        from two-container.rb:36:in `<main>'
ginjo commented 8 years ago

I'm also running up against this exception. I have a ROM::Configuration object with relations containing schemas - this is used to build my base ROM::Container. Elsewhere in my project I take the rom config object, add to it, and try to create a new container. I get pretty much the exact same error as above.

My current workaround is to build the base config object on-the-fly with a method, which allows me to create additional "fresh" config objects on demand. It seems to work, but I haven't tested it thoroughly yet.

solnic commented 8 years ago

We've fixed this in master, right @flash-gordon?

flash-gordon commented 8 years ago

@solnic I think so https://github.com/rom-rb/rom/blob/master/lib/rom/schema.rb#L81 @cflipse could you check it against master branch? If it works, we should release a fix

cflipse commented 8 years ago

yep, looks good