Class: Alteration
Defined Under Namespace
Classes: DuplicateAlteration, IrreversibleAlteration, MissingAlteration
Constant Summary
Constants inherited from Change
Class Attribute Summary collapse
-
.table_name ⇒ Object
Returns the value of attribute table_name.
-
.version_column ⇒ Object
Returns the value of attribute version_column.
Instance Attribute Summary collapse
-
#create_sql ⇒ Object
readonly
Returns the value of attribute create_sql.
-
#drop_sql ⇒ Object
readonly
Returns the value of attribute drop_sql.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Class Method Summary collapse
- .ensure_table_exists ⇒ Object
- .load(alterations_path) ⇒ Object
- .table ⇒ Object
- .version ⇒ Object
- .version=(new_version) ⇒ Object
Instance Method Summary collapse
- #create ⇒ Object
- #drop ⇒ Object
-
#initialize(version, create_sql, drop_sql) ⇒ Alteration
constructor
A new instance of Alteration.
Methods inherited from Change
Constructor Details
#initialize(version, create_sql, drop_sql) ⇒ Alteration
Returns a new instance of Alteration.
134 135 136 137 138 |
# File 'lib/tern.rb', line 134 def initialize(version, create_sql, drop_sql) @version = version @create_sql = create_sql @drop_sql = drop_sql end |
Class Attribute Details
.table_name ⇒ Object
Returns the value of attribute table_name.
89 90 91 |
# File 'lib/tern.rb', line 89 def table_name @table_name end |
.version_column ⇒ Object
Returns the value of attribute version_column.
90 91 92 |
# File 'lib/tern.rb', line 90 def version_column @version_column end |
Instance Attribute Details
#create_sql ⇒ Object (readonly)
Returns the value of attribute create_sql.
131 132 133 |
# File 'lib/tern.rb', line 131 def create_sql @create_sql end |
#drop_sql ⇒ Object (readonly)
Returns the value of attribute drop_sql.
132 133 134 |
# File 'lib/tern.rb', line 132 def drop_sql @drop_sql end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
130 131 132 |
# File 'lib/tern.rb', line 130 def version @version end |
Class Method Details
.ensure_table_exists ⇒ Object
96 97 98 99 100 101 102 |
# File 'lib/tern.rb', line 96 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
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/tern.rb', line 112 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 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 |
.table ⇒ Object
92 93 94 |
# File 'lib/tern.rb', line 92 def table DB[table_name] end |
.version ⇒ Object
104 105 106 |
# File 'lib/tern.rb', line 104 def version table.get(version_column) end |
.version=(new_version) ⇒ Object
108 109 110 |
# File 'lib/tern.rb', line 108 def version=(new_version) table.update version_column => new_version end |
Instance Method Details
#create ⇒ Object
140 141 142 143 |
# File 'lib/tern.rb', line 140 def create DB.run create_sql Alteration.version = version end |
#drop ⇒ Object
145 146 147 148 149 |
# File 'lib/tern.rb', line 145 def drop raise IrreversibleAlteration, "Alteration #{version.to_s.rjust(3, "0")} is irreversible" unless drop_sql DB.run drop_sql Alteration.version = version - 1 end |