Changeset 146 for trunk/examples/blog.rb

Show
Ignore:
Timestamp:
08/15/2006 13:33:54 (2 years ago)
Author:
why
Message:
  • lib/camping/db.rb: The create_schema method takes some options now. The :assume option lets you force a default version if no migration table is found. So, if you know that the user has some of the right tables (you deduce that they have version 1.0 tables,) then you can call create_schema :assume => 1.0 and the 1.0 (and below) upgrades will be skipped.
  • examples/: altered all the examples to use migrations and create_schema :assume.
Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/examples/blog.rb

    r134 r146  
    1313 
    1414module Blog::Models 
    15     def self.schema(&block) 
    16         @@schema = block if block_given? 
    17         @@schema 
    18     end 
    19    
    2015    class Post < Base; belongs_to :user; end 
    2116    class Comment < Base; belongs_to :user; end 
    2217    class User < Base; end 
    23 end 
    24  
    25 Blog::Models.schema do 
    26     create_table :blog_posts, :force => true do |t| 
    27       t.column :id,       :integer, :null => false 
    28       t.column :user_id,  :integer, :null => false 
    29       t.column :title,    :string,  :limit => 255 
    30       t.column :body,     :text 
    31     end 
    32     create_table :blog_users, :force => true do |t| 
    33       t.column :id,       :integer, :null => false 
    34       t.column :username, :string 
    35       t.column :password, :string 
    36     end 
    37     create_table :blog_comments, :force => true do |t| 
    38       t.column :id,       :integer, :null => false 
    39       t.column :post_id,  :integer, :null => false 
    40       t.column :username, :string 
    41       t.column :body,     :text 
    42     end 
    43     execute "INSERT INTO blog_users (username, password) VALUES ('admin', 'camping')" 
     18 
     19    class CreateTheBasics < V 1.0 
     20      def self.up 
     21        create_table :blog_posts, :force => true do |t| 
     22          t.column :id,       :integer, :null => false 
     23          t.column :user_id,  :integer, :null => false 
     24          t.column :title,    :string,  :limit => 255 
     25          t.column :body,     :text 
     26        end 
     27        create_table :blog_users, :force => true do |t| 
     28          t.column :id,       :integer, :null => false 
     29          t.column :username, :string 
     30          t.column :password, :string 
     31        end 
     32        create_table :blog_comments, :force => true do |t| 
     33          t.column :id,       :integer, :null => false 
     34          t.column :post_id,  :integer, :null => false 
     35          t.column :username, :string 
     36          t.column :body,     :text 
     37        end 
     38        User.create :username => 'admin', :password => 'camping' 
     39      end 
     40      def self.down 
     41        drop_table :blog_posts 
     42        drop_table :blog_users 
     43        drop_table :blog_comments 
     44      end 
     45    end 
    4446end 
    4547 
     
    267269def Blog.create 
    268270    Camping::Models::Session.create_schema 
    269     unless Blog::Models::Post.table_exists? 
    270         ActiveRecord::Schema.define(&Blog::Models.schema) 
    271     end 
     271    Blog::Models.create_schema :assume => (Blog::Models::Post.table_exists? ? 1.0 : 0.0) 
    272272end 
    273273