Class: Migrations
- Inherits:
-
Object
- Object
- Migrations
- Defined in:
- lib/migrations.rb
Instance Attribute Summary collapse
-
#path ⇒ Object
Returns the value of attribute path.
-
#verbose ⇒ Object
Returns the value of attribute verbose.
Instance Method Summary collapse
- #applied(n = nil) ⇒ Object
- #apply(version, update_table = true) ⇒ Object
- #down(n = nil) ⇒ Object
- #ensure_migration_table ⇒ Object
- #generate(name) ⇒ Object
- #get_migrations ⇒ Object
-
#initialize(db = nil) ⇒ Migrations
constructor
A new instance of Migrations.
- #insert(version) ⇒ Object
- #list ⇒ Object
- #pending(n = nil) ⇒ Object
- #remove(version) ⇒ Object
- #revert(version, update_table = true) ⇒ Object
- #up(n = nil) ⇒ Object
Constructor Details
#initialize(db = nil) ⇒ Migrations
Returns a new instance of Migrations.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/migrations.rb', line 9 def initialize(db=nil) config_file = "config/config.rb" if db @db = db elsif File.exist? config_file config = {} instance_eval(File.read(config_file)).each do |key, value| config[key] = value end if !config[:db] raise "Database connection is not defined in config.rb" end @db = Sequel.connect("#{config[:db][:adapter]}://#{config[:db][:user]}:#{config[:db][:password]}@#{config[:db][:host]}/#{config[:db][:database]}") else raise "#{config_file} was not found and no database handle was passed to us." end @path = 'migrations' @verbose = true end |
Instance Attribute Details
#path ⇒ Object
Returns the value of attribute path.
6 7 8 |
# File 'lib/migrations.rb', line 6 def path @path end |
#verbose ⇒ Object
Returns the value of attribute verbose.
7 8 9 |
# File 'lib/migrations.rb', line 7 def verbose @verbose end |
Instance Method Details
#applied(n = nil) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/migrations.rb', line 87 def applied(n=nil) filesystem, database, all = get_migrations applied = [] database.keys.sort.reverse.each do |migration| if (n.nil? or applied.length < n) applied.push migration end end applied end |
#apply(version, update_table = true) ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/migrations.rb', line 168 def apply(version, update_table=true) migration = @path + '/' + version + '.rb' load migration m = Object::const_get(version).new(@db) @verbose and print "Applying: #{version} ... " m.up @verbose and puts "ok" update_table and insert(version) end |
#down(n = nil) ⇒ Object
160 161 162 163 164 165 166 |
# File 'lib/migrations.rb', line 160 def down(n=nil) ensure_migration_table applied(n).each do |version| revert(version) end end |
#ensure_migration_table ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/migrations.rb', line 136 def ensure_migration_table begin table = @db[:migration].map(:version) rescue Sequel::DatabaseError => e if e..match /Table .*? doesn\'t exist/ @db.create_table :migration do primary_key :id String :version, :null => false DateTime :applied_at end else raise e end end end |
#generate(name) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/migrations.rb', line 101 def generate(name) if !File.exist? @path FileUtils.mkdir_p @path end t = Time.now while 1 t = Time.at(t.to_i + 1) class_name = 'M' + t.strftime('%Y%m%d_%H%M%S') + '_' + name.gsub(/[^a-zA-Z0-9_]/i, '') migration = "#{@path}/#{class_name}.rb" if !File.exist? migration break end end File.open(migration,'w') do |f| f.write(" require 'migrations/migration' class #{class_name} < Migration def up end def down end end ") @verbose and puts "Create new migration: #{migration}" true end end |
#get_migrations ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/migrations.rb', line 33 def get_migrations ensure_migration_table filesystem = [] database = {} all = [] Dir.glob(@path + '/*.rb').map do |migration| class_name = migration.match(/(M[0-9]{8}_[0-9]{6}_[a-zA-Z0-9_]+)\.rb\z/)[1] filesystem.push class_name all.push class_name end @db[:migration].each do |migration| database[migration[:version]] = migration[:applied_at] if !all.include? migration[:version] all.push migration[:version] end end [filesystem,database,all] end |
#insert(version) ⇒ Object
184 185 186 |
# File 'lib/migrations.rb', line 184 def insert(version) @db[:migration].insert(:version => version, :applied_at => Time.now) end |
#list ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/migrations.rb', line 57 def list filesystem, database, all = get_migrations list = [] all.sort.each do |migration| list.push({ :version => migration, :applied => database[migration] ? true : false, :applied_at => database[migration] ? database[migration] : '' }) end list end |
#pending(n = nil) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/migrations.rb', line 73 def pending(n=nil) filesystem, database, all = get_migrations pending = [] filesystem.sort.each do |migration| if !database.keys.include?(migration) and (n.nil? or pending.length < n) pending.push migration end end pending end |
#remove(version) ⇒ Object
208 209 210 |
# File 'lib/migrations.rb', line 208 def remove(version) @db[:migration].where('version = ?',version).delete end |
#revert(version, update_table = true) ⇒ Object
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/migrations.rb', line 188 def revert(version, update_table=true) migration = @path + '/' + version + '.rb' if !File.exist? migration raise "Migration file #{migration} not found." end load migration m = Object::const_get(version).new(@db) @verbose and print "Rolling back: #{version} ... " m.down @verbose and puts "ok" update_table and remove(version) end |
#up(n = nil) ⇒ Object
152 153 154 155 156 157 158 |
# File 'lib/migrations.rb', line 152 def up(n=nil) ensure_migration_table pending(n).each do |version| apply(version) end end |