From the DataMapper document, I think there are at least four functions need to be called to have database setup:

DataMapper.setup(:default, 'sqlite:///path/to/project.db')
DataMapper.finalize
DataMapper.auto_migrate!
DataMapper.auto_upgrade!

In many DataMapper+Sinatra tutorials I learned that auto_migrate! and auto_upgrade! are not supposed to be called every time the app is loaded on production server. But in the meanwhile many examples call these functions in the main ruby file of the sinatra app, say app.rb, without additional check. And some examples do not call finalize at all. So far I am confused and I am not sure what to do on the production server.

Take this following simple app.rb for example, I have some questions:

  1. Where and when should finalize be called?
  2. When deploying the app first time there is no db file on the production server, how do I have it automatically created? Or do I have to create the project.db file manually?
  3. Since the auto_upgrade! is wrapped in :development block, it won't be called on production server. How am I supposed to upgrade database when I add or remove columns in it?
require 'sinatra'
require 'data_mapper'

configure do
  DataMapper.setup :default, "sqlite3://#{Dir.pwd}/project.db"
end

class Book
  include DataMapper::Resource
  property :id, Serial
  property :title, Text

  belongs_to :author
end

class Author
  include DataMapper::Resource
  property :id, Serial
  property :name, Text

  has n, :books
end

configure :development do
  DataMapper.auto_upgrade!
end

get '/:id' do
  @author = Author.get params[:id]
  erb :list_author_and_his_books # The template has nothi
Edit
Report