Class: Alteration

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

Defined Under Namespace

Classes: DuplicateAlteration, IrreversibleAlteration, MissingAlteration

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, version, create_sql, drop_sql) ⇒ Alteration

Returns a new instance of Alteration.



160
161
162
163
164
165
# File 'lib/tern.rb', line 160

def initialize(name, version, create_sql, drop_sql)
  @name = name
  @version = version
  @create_sql = create_sql
  @drop_sql = drop_sql
end

Class Attribute Details

.table_nameObject

Returns the value of attribute table_name.



117
118
119
# File 'lib/tern.rb', line 117

def table_name
  @table_name
end

.version_columnObject

Returns the value of attribute version_column.



118
119
120
# File 'lib/tern.rb', line 118

def version_column
  @version_column
end

Instance Attribute Details

#versionObject (readonly)

Returns the value of attribute version.



158
159
160
# File 'lib/tern.rb', line 158

def version
  @version
end

Class Method Details

.ensure_table_existsObject



124
125
126
127
128
129
130
# File 'lib/tern.rb', line 124

def ensure_table_exists
  vc = version_column # because create_table? block is run with different binding and can't access version_column
  DB.create_table? table_name do
    column vc, :integer, :null => false
  end
  table.insert version_column => 0 if table.count == 0
end

.load(alterations_path) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/tern.rb', line 140

def load(alterations_path)
  alterations = Dir.glob("#{alterations_path}/[0-9]*.sql").map do |path|
    raise "This can't happen" unless File.basename(path) =~ /^(\d+)/
    version = $1.to_i
    create_sql, drop_sql = parse File.read(path)
    new path, version, create_sql, drop_sql
  end.sort_by(&:version)

  alterations.each_with_index do |a, i|
    expected = i+1
    raise DuplicateAlteration, "Alteration #{a.version.to_s.rjust(3, "0")} is duplicated" if a.version < expected
    raise MissingAlteration, "Alteration #{expected.to_s.rjust(3, "0")} is missing" if a.version > expected
  end

  alterations
end

.tableObject



120
121
122
# File 'lib/tern.rb', line 120

def table
  DB[table_name]
end

.versionObject



132
133
134
# File 'lib/tern.rb', line 132

def version
  table.get(version_column)
end

.version=(new_version) ⇒ Object



136
137
138
# File 'lib/tern.rb', line 136

def version=(new_version)
  table.update version_column => new_version
end

Instance Method Details

#createObject



167
168
169
170
# File 'lib/tern.rb', line 167

def create
  run create_sql, name
  Alteration.version = version
end

#dropObject



172
173
174
175
176
# File 'lib/tern.rb', line 172

def drop
  raise IrreversibleAlteration, "Alteration #{version.to_s.rjust(3, "0")} is irreversible" unless drop_sql
  run drop_sql, name
  Alteration.version = version - 1
end