Changeset 146

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.
Location:
trunk
Files:
6 modified

Legend:

Unmodified
Added
Removed
  • trunk/Rakefile

    r134 r146  
    88NAME = "camping" 
    99REV = File.read(".svn/entries")[/committed-rev="(\d+)"/, 1] rescue nil 
    10 VERS = "1.4" + (REV ? ".#{REV}" : "") 
     10VERS = ENV['VERSION'] || ("1.4" + (REV ? ".#{REV}" : "")) 
    1111CLEAN.include ['**/.*.sw?', '*.gem', '.config'] 
    1212RDOC_OPTS = ['--quiet', '--title', "Camping, the Documentation", 
  • trunk/examples/README

    r113 r146  
    22 
    33From this directory, run: `camping .` 
     4 
     5NOTE: You'll need the active_record and acts_as_versioned gems installed. 
  • 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 
  • trunk/examples/campsh.rb

    r112 r146  
    2323 
    2424    def self.create 
    25         unless CampSh::Models::Command.table_exists? 
    26             ActiveRecord::Schema.define do 
    27                 create_table :campsh_commands, :force => true do |t| 
    28                     t.column :id,         :integer, :null => false 
    29                     t.column :author,     :string,  :limit => 40 
    30                     t.column :name,       :string,  :limit => 255 
    31                     t.column :created_at, :datetime 
    32                     t.column :doc,        :text 
    33                     t.column :code,       :text 
    34                 end 
    35                 CampSh::Models::Command.create_versioned_table 
    36             end 
    37         end 
     25        Models.create_schema :assume => (Models::Command.table_exists? ? 1.0 : 0.0) 
    3826    end 
    3927end 
     
    4432        validates_presence_of :author 
    4533        acts_as_versioned 
     34    end 
     35    class CreateBasics < V 1.0 
     36        def self.up 
     37            create_table :campsh_commands, :force => true do |t| 
     38                t.column :id,         :integer, :null => false 
     39                t.column :author,     :string,  :limit => 40 
     40                t.column :name,       :string,  :limit => 255 
     41                t.column :created_at, :datetime 
     42                t.column :doc,        :text 
     43                t.column :code,       :text 
     44            end 
     45            Command.create_versioned_table 
     46            Command.reset_column_information 
     47        end 
     48        def self.down 
     49            drop_table :campsh_commands 
     50            Command.drop_versioned_table 
     51        end 
    4652    end 
    4753end 
     
    128134            @cmd = @cmd.versions.find_by_version(version) unless version.nil? or version == @cmd.version.to_s 
    129135            @title = "Editing #{name}" 
    130             @author = cookies.cmd_author or CampSh::ANON 
     136            @author = @cookies.cmd_author || CampSh::ANON 
    131137            render :edit 
    132138        end 
    133139        def post(name) 
    134             if input.author != CampSh::ANON 
    135                 cookies.cmd_author = input.author 
    136             end 
     140            @cookies.cmd_author = input.command.author 
    137141            Command.find_or_create_by_name(name).update_attributes(input.command) 
    138142            redirect Show, name 
     
    560564                           :class => "navlink", :accesskey => "E" 
    561565            unless @version.version == 1 
     566                text " | " 
    562567                a 'Back in time', :href => R(Show, @version.name, @version.version-1) 
    563568            end 
    564569            unless @version.version == @cmd.version 
     570                text " | " 
    565571                a 'Next',    :href => R(Show, @version.name, @version.version+1) 
     572                text " | " 
    566573                a 'Current', :href => R(Show, @version.name) 
    567574            end 
    568             small "(#{ @cmd.version.size } revisions)" 
     575            if @cmd.versions.size > 1 
     576                small "(#{ @cmd.versions.size } revisions)" 
     577            end 
    569578        end 
    570579    end 
  • trunk/examples/tepee.rb

    r121 r146  
    66 
    77module Tepee::Models 
    8   def self.schema(&block) 
    9     @@schema = block if block_given? 
    10     @@schema 
    11   end 
    12    
     8 
    139  class Page < Base 
    1410    PAGE_LINK = /\[\[([^\]|]*)[|]?([^\]]*)\]\]/ 
     
    1713    acts_as_versioned 
    1814  end 
    19 end 
    2015 
    21 Tepee::Models.schema do 
    22   create_table :tepee_pages, :force => true do |t| 
    23     t.column :title, :string, :limit => 255 
    24     t.column :body, :text 
     16  class CreateTepee < V 1.0 
     17    def self.up 
     18      create_table :tepee_pages, :force => true do |t| 
     19        t.column :title, :string, :limit => 255 
     20        t.column :body, :text 
     21      end 
     22      Page.create_versioned_table 
     23      Page.reset_column_information 
     24    end 
     25    def self.down 
     26      drop_table :tepee_pages 
     27      Page.drop_versioned_table 
     28    end 
    2529  end 
    26   Tepee::Models::Page.create_versioned_table 
     30 
    2731end 
    2832 
     
    130134 
    131135def Tepee.create 
    132   unless Tepee::Models::Page.table_exists? 
    133     ActiveRecord::Schema.define(&Tepee::Models.schema) 
    134     Tepee::Models::Page.reset_column_information 
    135   end 
     136  Tepee::Models.create_schema :assume => (Tepee::Models::Page.table_exists? ? 1.0 : 0.0) 
    136137end 
    137138 
  • trunk/lib/camping/db.rb

    r145 r146  
    2525  end 
    2626 
    27   def self.create_schema 
     27  def self.create_schema(opts = {}) 
     28    opts[:assume] ||= 0 
     29    opts[:version] ||= @final 
    2830    if @migrations 
    2931      unless SchemaInfo.table_exists? 
     
    3537      end 
    3638 
    37       si = SchemaInfo.find(:first) || SchemaInfo.new(:version => 0) 
     39      si = SchemaInfo.find(:first) || SchemaInfo.new(:version => opts[:assume]) 
    3840      @migrations.each do |k| 
    3941        k.migrate(:up) if si.version < k.version 
    4042      end 
    41       si.update_attributes(:version => @final) 
     43      si.update_attributes(:version => opts[:version]) 
    4244    end 
    4345  end