Class: Migrate::Storage::DB
- Inherits:
-
Object
- Object
- Migrate::Storage::DB
- Defined in:
- lib/migrate/storage/db.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
-
#create_tables ⇒ Object
Will create database model used by tool.
- #current_version ⇒ Object
- #delete(version) ⇒ Object
-
#exec_sql(sql) ⇒ Object
Executes SQL.
- #extract_version(results) ⇒ Object
- #get_migration(version) ⇒ Object
- #highest_version ⇒ Object
-
#initialize(config) ⇒ DB
constructor
A new instance of DB.
- #list_migrations(selects, limit) ⇒ Object
- #log_down(version) ⇒ Object
- #log_up(version) ⇒ Object
- #lowest_version ⇒ Object
- #migrations_from(from) ⇒ Object
- #migrations_range(from, to, is_up) ⇒ Object
- #new_migration(version = 0, description = "") ⇒ Object
- #next_version ⇒ Object
- #prev_version ⇒ Object
- #print(results, title = "") ⇒ Object
- #tables_exists? ⇒ Boolean
-
#tx ⇒ Object
Creates new transaction.
- #type ⇒ Object
- #version_exists?(version) ⇒ Boolean
Constructor Details
#initialize(config) ⇒ DB
Returns a new instance of DB.
8 9 10 |
# File 'lib/migrate/storage/db.rb', line 8 def initialize(config) @config = config end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
6 7 8 |
# File 'lib/migrate/storage/db.rb', line 6 def config @config end |
Instance Method Details
#create_tables ⇒ Object
Will create database model used by tool
160 161 162 |
# File 'lib/migrate/storage/db.rb', line 160 def create_tables raise "Implementation for creating tables not found" end |
#current_version ⇒ Object
87 88 89 90 91 92 |
# File 'lib/migrate/storage/db.rb', line 87 def current_version self.extract_version self.exec_sql <<-eos SELECT * FROM #{config.version_number} LIMIT 1 eos end |
#delete(version) ⇒ Object
132 133 134 |
# File 'lib/migrate/storage/db.rb', line 132 def delete(version) self.exec_sql "DELETE FROM #{@config.version_info} WHERE version=#{version}" end |
#exec_sql(sql) ⇒ Object
Executes SQL
169 170 171 |
# File 'lib/migrate/storage/db.rb', line 169 def exec_sql(sql) raise "Implementation for executing SQL script not found" end |
#extract_version(results) ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/migrate/storage/db.rb', line 52 def extract_version(results) if results && results.count > 0 results[0]["version"] else raise VersionNotFound end end |
#get_migration(version) ⇒ Object
116 117 118 119 120 121 122 123 |
# File 'lib/migrate/storage/db.rb', line 116 def get_migration(version) res = self.exec_sql "SELECT * FROM #{@config.version_info} WHERE version=#{version}" if res && res.count > 0 res[0] else raise VersionNotFound end end |
#highest_version ⇒ Object
68 69 70 71 72 73 74 75 76 |
# File 'lib/migrate/storage/db.rb', line 68 def highest_version self.extract_version self.exec_sql <<-eos SELECT version FROM #{@config.version_info} ORDER BY version DESC LIMIT 1 eos rescue VersionNotFound => e 0 end |
#list_migrations(selects, limit) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/migrate/storage/db.rb', line 28 def list_migrations(selects, limit) self.exec_sql <<-eos SELECT #{(selects == nil ? "*" : selects)} FROM #{@config.version_info} ORDER BY last_up, version #{limit != nil ? "LIMIT #{limit}" : ""} eos end |
#log_down(version) ⇒ Object
108 109 110 111 112 113 114 |
# File 'lib/migrate/storage/db.rb', line 108 def log_down(version) self.exec_sql "UPDATE #{@config.version_info} SET last_down=now() WHERE version=#{version}" lowest_version = self.lowest_version version_to_save = lowest_version.to_i < version.to_i ? self.prev_version().to_i : 0 self.exec_sql "UPDATE #{@config.version_number} SET version=#{version_to_save}" end |
#log_up(version) ⇒ Object
103 104 105 106 |
# File 'lib/migrate/storage/db.rb', line 103 def log_up(version) self.exec_sql "UPDATE #{@config.version_info} SET last_up=now() WHERE version=#{version}" self.exec_sql "UPDATE #{@config.version_number} SET version=#{version}" end |
#lowest_version ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/migrate/storage/db.rb', line 60 def lowest_version self.extract_version self.exec_sql <<-eos SELECT version FROM #{@config.version_info} ORDER BY version LIMIT 1 eos end |
#migrations_from(from) ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/migrate/storage/db.rb', line 44 def migrations_from(from) self.exec_sql <<-eos SELECT * FROM #{@config.version_info} WHERE version >= #{from} ORDER BY version eos end |
#migrations_range(from, to, is_up) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/migrate/storage/db.rb', line 36 def migrations_range(from, to, is_up) self.exec_sql <<-eos SELECT * FROM #{@config.version_info} WHERE version >= #{from} AND version <= #{to} ORDER BY version #{!is_up ? "DESC" : ""} eos end |
#new_migration(version = 0, description = "") ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/migrate/storage/db.rb', line 16 def new_migration(version=0, description="") self.exec_sql <<-eos INSERT INTO #{@config.version_info} (version, description, created_date) VALUES(#{version}, '#{description}', now()) eos res = self.exec_sql <<-eos SELECT * FROM #{@config.version_info} ORDER BY version DESC LIMIT 1 eos res[0] end |
#next_version ⇒ Object
78 79 80 81 82 83 84 85 |
# File 'lib/migrate/storage/db.rb', line 78 def next_version self.extract_version self.exec_sql <<-eos SELECT version FROM #{@config.version_info} WHERE version > (SELECT version FROM #{@config.version_number} LIMIT 1) ORDER BY version LIMIT 1 eos end |
#prev_version ⇒ Object
94 95 96 97 98 99 100 101 |
# File 'lib/migrate/storage/db.rb', line 94 def prev_version self.extract_version self.exec_sql <<-eos SELECT version FROM #{@config.version_info} WHERE version < (SELECT version FROM #{@config.version_number} LIMIT 1) ORDER BY version DESC LIMIT 1 eos end |
#print(results, title = "") ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/migrate/storage/db.rb', line 136 def print(results, title="") rows = [] headings = results[0].keys results.each do |result| row = [] result.each do |column, value| if column == "description" if value.length > 70 value = value.scan(/.{1,70}/).join("\n") end end row << value end rows << row end table = Terminal::Table.new :headings => headings, :rows => rows table.title = title puts table end |
#tables_exists? ⇒ Boolean
164 165 166 |
# File 'lib/migrate/storage/db.rb', line 164 def tables_exists? raise "Implementation for checking if version tables already exists not found" end |
#tx ⇒ Object
Creates new transaction. Should accept block.
174 175 176 |
# File 'lib/migrate/storage/db.rb', line 174 def tx raise "Implementation for starting new transaction not found" end |
#type ⇒ Object
12 13 14 |
# File 'lib/migrate/storage/db.rb', line 12 def type @config.storage end |
#version_exists?(version) ⇒ Boolean
125 126 127 128 129 130 |
# File 'lib/migrate/storage/db.rb', line 125 def version_exists?(version) self.get_migration(version) true rescue VersionNotFound false end |