开发者

How to tell if a table exists in DataMapper

This code is failing on the last line specifically due to the check for table_exists? How do I correctly do this in Datamapper?


require 'sinatra'
require 'DataMapper'

DataMapper::setup(:default, "sq开发者_JAVA百科lite3://#{Dir.pwd}/blog.db")

class Post
    include DataMapper::Resource
    property :id, Serial
    property :title, String
    property :body, Text
    property :created_at, DateTime
end

DataMapper.finalize

# automatically create the post table
DataMapper.auto_migrate! unless Post.table_exists?


Justin,

If you have dm-migrations required (which basically means that you're using an RDBMS adapter anyway), you can do the following to find out if a table (or column inside that table) exists.

# Find out if the table named 'people' exists
DataMapper.repository(:default).adapter.storage_exists?('people')

# Find out if there's a 'name' column in the 'people' table
DataMapper.repository(:default).adapter.field_exists?('people', 'name')

Note that these API methods only get mixed into the adapter if dm-migrations are required and you're using a DataObjectsAdapter descendent.


You could use DataMapper.auto_update! which should be non-destructive (adds tables/columns only).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜