Class: Definition

Inherits:
Change show all
Defined in:
lib/tern.rb

Constant Summary

Constants inherited from Change

Change::SPLIT_MARKER

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes inherited from Change

#create_sql, #drop_sql, #name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Change

parse, #run

Constructor Details

#initialize(name, id, sequence, create_sql, drop_sql) ⇒ Definition

Returns a new instance of Definition.



83
84
85
86
87
88
89
# File 'lib/tern.rb', line 83

def initialize(name, id, sequence, create_sql, drop_sql)
  @name = name
  @id = id
  @sequence = sequence
  @create_sql = create_sql
  @drop_sql = drop_sql
end

Class Attribute Details

.table_nameObject

Returns the value of attribute table_name.



43
44
45
# File 'lib/tern.rb', line 43

def table_name
  @table_name
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



80
81
82
# File 'lib/tern.rb', line 80

def id
  @id
end

#sequenceObject (readonly)

Returns the value of attribute sequence.



81
82
83
# File 'lib/tern.rb', line 81

def sequence
  @sequence
end

Class Method Details

.ensure_table_existsObject



49
50
51
52
53
54
55
56
# File 'lib/tern.rb', line 49

def ensure_table_exists
  DB.create_table? table_name do
    primary_key :id
    column :sequence, :text, :null => false
    column :create_sql, :text, :null => false
    column :drop_sql, :text, :null => false
  end
end

.load_existingObject



58
59
60
61
62
# File 'lib/tern.rb', line 58

def load_existing
  table.order(:id).all.map do |row|
    new "existing definition: #{row[:id]}", row[:id], row[:sequence], row[:create_sql], row[:drop_sql]
  end.group_by { |d| d.sequence }
end

.load_targets(sequence_yml_path) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/tern.rb', line 64

def load_targets(sequence_yml_path)
  definition_sequences = YAML.load(File.read(sequence_yml_path))
  sequence_dir = File.dirname(sequence_yml_path)

  definition_sequences.keys.each do |sequence|
    definition_sequences[sequence] = definition_sequences[sequence].map do |f|
      path = File.join(sequence_dir, f)
      create_sql, drop_sql = parse File.read(path)
      Definition.new path, nil, sequence, create_sql, drop_sql
    end
  end

  definition_sequences
end

.tableObject



45
46
47
# File 'lib/tern.rb', line 45

def table
  DB[table_name]
end

Instance Method Details

#createObject



91
92
93
94
# File 'lib/tern.rb', line 91

def create
  run create_sql, name
  table.insert :sequence => sequence, :create_sql => create_sql, :drop_sql => drop_sql
end

#dropObject



96
97
98
99
# File 'lib/tern.rb', line 96

def drop
  run drop_sql, name
  table.filter(:id => id).delete
end

#tableObject



101
102
103
# File 'lib/tern.rb', line 101

def table
  self.class.table
end