Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Connect to the database lazily #418

Closed
godfat opened this issue Jan 11, 2012 · 3 comments
Closed

Feature request: Connect to the database lazily #418

godfat opened this issue Jan 11, 2012 · 3 comments

Comments

@godfat
Copy link
Contributor

godfat commented Jan 11, 2012

I want to define my models before connecting to the database,
but sequel didn't allow me to do so. Then I put a trick there:

First make it connected to a in-memory SQLite database,
then defining the model (i.e. define schema and serialization)
Lastly reconnect to the real database appropriately.

I didn't know this won't work well in sequel before,
spending a lot of time digging, trying, and googling around.

Below is what I am doing right now to workaround that:

Sequel::Model.db = Sequel.connect('sqlite:memory') # dummy connect

class Item < Sequel::Model
  plugin :schema
  set_schema do
    # HACK
    primary_key   :id, :type => 'Serial', :auto_increment => false
    Text        :data
  end

  # HACK
  create_table # this is needed! otherwise, the serialization would be
               # broken, due to different inclusion order or something

  plugin :serialization
  serialize_attributes [Yajl::Encoder.method(:encode),
                        Yajl::Parser .method(:parse)], :data
  # connect to the real database
  def self.connect
    self.db = Sequel.connect('postgres://example.com/database')
    self.db.loggers << Logger.new($stdout)
  end
end

# somewhere
Item.connect

I know that to support this, it might mean to rewrite a lot of
internal architecture inside sequel. But... I still hope if this
could be considered? Or, giving a better error message,
instead of crashing randomly?

Thank you!

@jeremyevans
Copy link
Owner

Sequel is this way by design. It needs to parse the database's schema at model creation time. Why is it a problem to connect to the database before loading your model classes?

You can work around this issue by using anonymous classes:

Item = Class.new(Sequel::Model)

Then just don't call set_dataset/dataset= until after the database connection has been made. You are responsible for picking up the pieces when this breaks.

Also, just for future reference, feature requests should be sent to the Google Group, the GitHub issue tracker should only be used for bugs/pull requests.

@godfat
Copy link
Contributor Author

godfat commented Jan 12, 2012

Thanks for the reply.

@godfat
Copy link
Contributor Author

godfat commented Jan 12, 2012

Regarding why it is a problem connecting to a database before loading
model classes,
it is not really a problem, but it would be more flexible if it is
allowed to connect to the
database whenever really needed.

For example, then I could put utilities methods inside model classes
and use them without connecting to a database if I don't want to.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants