title: What is the correct way to setup database with DataMapper and Sinatra on production server?
body: <p>From the <a href="http://datamapper.org/getting-started.html">DataMapper document</a>, I think there are at least four functions need to be called to have database setup:</p>
  
  <pre class="lang-rb prettyprint-override"><code>DataMapper.setup(:default, 'sqlite:///path/to/project.db')
  DataMapper.finalize
  DataMapper.auto_migrate!
  DataMapper.auto_upgrade!
  </code></pre>
  
  <p>In many DataMapper+Sinatra tutorials I learned that <code>auto_migrate!</code> and <code>auto_upgrade!</code> 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 <code>app.rb</code>, without additional check. And some examples do not call <code>finalize</code> at all. So far I am confused and I am not sure what to do on the production server.</p>
  
  <p>Take this following simple <code>app.rb</code> for example, I have some questions:</p>
  
  <ol>
  <li>Where and when should <code>finalize</code> be called?</li>
  <li>When deploying the app first time there is no <code>db</code> file on the production server, how do I have it automatically created? Or do I have to create the <code>project.db</code> file manually?</li>
  <li>Since the <code>auto_upgrade!</code> is wrapped in <code>:development</code> block, it won't be called on production server. How am I supposed to upgrade database when I add or remove columns in it?</li>
  </ol>
  
  <pre class="lang-rb prettyprint-override"><code>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 nothing to do with this question, ignore it
  end
  
  get '/new' do
    # Some code for user to input book or author details
  end
  
  get '/create' do
    # Some code to create book or author in db
  end
  </code></pre>
  
  <p>Thanks for reading this long post :D</p>