Class: Lore::Migration

Inherits:
Object show all
Defined in:
lib/lore/migration.rb

Overview

Usage:

class User_Profile_Migration < Lore::Migration

  def up
    create_table(:user_profile) { 
      schema :public
      primary_key :user_profile_id, :user_profile_id_seq
      index :user_name

      user_name Lore::Type.varchar(50), :null => false, :unique => true
      is_admin Lore::Type.boolean, :null => false, :default => false
      registered Lore::Type.timestamp, :null => false, :default => 'now()'
    }
  end

  def down
    drop_table(:user_profile)
    drop_sequence(:user_profile_id_seq)
  end

end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMigration

Returns a new instance of Migration.



36
37
38
39
40
41
42
43
44
45
# File 'lib/lore/migration.rb', line 36

def initialize()
  @primary_keys   = []
  @sequences      = {}
  @fields         = []
  @field_params   = {}
  @indices        = []
  @drop_tables    = []
  @drop_sequences = []
  @constraints    = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(field_name, type, params = {}) ⇒ Object



100
101
102
# File 'lib/lore/migration.rb', line 100

def method_missing(field_name, type, params={})
  add_field(field_name, type, params)
end

Instance Attribute Details

#drop_tablesObject (readonly)

Returns the value of attribute drop_tables.



34
35
36
# File 'lib/lore/migration.rb', line 34

def drop_tables
  @drop_tables
end

#field_paramsObject (readonly)

Returns the value of attribute field_params.



34
35
36
# File 'lib/lore/migration.rb', line 34

def field_params
  @field_params
end

#fieldsObject (readonly)

Returns the value of attribute fields.



34
35
36
# File 'lib/lore/migration.rb', line 34

def fields
  @fields
end

#indicesObject (readonly)

Returns the value of attribute indices.



34
35
36
# File 'lib/lore/migration.rb', line 34

def indices
  @indices
end

#primary_keysObject (readonly)

Returns the value of attribute primary_keys.



34
35
36
# File 'lib/lore/migration.rb', line 34

def primary_keys
  @primary_keys
end

#schema(schema_name) ⇒ Object (readonly)

Returns the value of attribute schema.



34
35
36
# File 'lib/lore/migration.rb', line 34

def schema
  @schema
end

#tableObject (readonly)

Returns the value of attribute table.



34
35
36
# File 'lib/lore/migration.rb', line 34

def table
  @table
end

Instance Method Details

#add_constraint(constraint) ⇒ Object



92
93
94
# File 'lib/lore/migration.rb', line 92

def add_constraint(constraint)
  @constraints << constraint
end

#add_field(field_name, type, params = {}) ⇒ Object



86
87
88
89
90
# File 'lib/lore/migration.rb', line 86

def add_field(field_name, type, params={})
  @fields << field_name
  @field_params[field_name] = params
  @field_params[field_name][:type] = type
end

#create_table(table_name, schema = :public, &block) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/lore/migration.rb', line 47

def create_table(table_name, schema=:public, &block)
  instance_eval(&block)
  @table = table_name
  factory = Model_Factory.new(table_name)
  @primary_keys.each { |p|
    factory.add_attribute(p, :null => false, :type => Lore::Type.integer)
    factory.add_primary_key(:attribute => p, 
                            :key_name => "#{table_name}_pkey", 
                            :sequence => @sequences[p] )
  }
  @fields.each { |f|
    factory.add_attribute(f, @field_params[f])
  }
  factory.build_table
end

#downObject



73
74
# File 'lib/lore/migration.rb', line 73

def down()
end

#drop_sequence(sequence_name) ⇒ Object



66
67
68
# File 'lib/lore/migration.rb', line 66

def drop_sequence(sequence_name)
  @drop_sequences << sequence_name
end

#drop_table(table_name) ⇒ Object



63
64
65
# File 'lib/lore/migration.rb', line 63

def drop_table(table_name)
  @drop_tables << table_name
end

#index(field_name) ⇒ Object



96
97
98
# File 'lib/lore/migration.rb', line 96

def index(field_name)
  @indices << field_name
end

#primary_key(key_name, sequence_name = nil) ⇒ Object



76
77
78
79
80
# File 'lib/lore/migration.rb', line 76

def primary_key(key_name, sequence_name=nil)
  key_name = key_name.to_sym
  @primary_keys << key_name
  @sequences[key_name] = sequence_name.to_sym if sequence_name
end

#upObject



70
71
# File 'lib/lore/migration.rb', line 70

def up()
end