Search code examples
rubysinatradatamapperpadrino

Using Padrino and DataMapper to access an existing Database


I'm migrating a sinatra app I have that acts as a backend UI for our DNS database. I've already got the DM configs in the sinatra app but want to migrate it to padrino so I can make it cleaner and easier to read, but also because I want to play around to padrino. If I just generate a new model, can I perform the datamapper mapping in that model, including specifying the db application and get away with doing that instead of using a generator?

What do I need to do to be able to access models on a different database, ideally without damaging that data base (read only)


Solution

  • Right so you actually can do this I found out with a bit of trial and error. Specify the datamapper database source in the config/boot.rb there's a section labelled Padrino.after_load, you'll want to add in your new DataMapper source here

    DataMapper.setup(:myalternatedatasource, "MY_ALTERNATE_DB_URL
    

    Then in your model file you'll want to specify

    def self.default_repository_name
        :myalternatedatasource
    end
    

    And it'll all work as intended!