Class: Miguel::Migrator

Inherits:
Object
  • Object
show all
Defined in:
lib/miguel/migrator.rb

Overview

Class for generating database migration from one schema to another.

Instance Method Summary collapse

Instance Method Details

#change_migration(from, to, out = Dumper.new) ⇒ Object

Generate one way Sequel migration.



221
222
223
224
225
226
227
# File 'lib/miguel/migrator.rb', line 221

def change_migration( from, to, out = Dumper.new )
  out.dump "Sequel.migration" do
    out.dump "change" do
      changes( from, to, out )
    end
  end
end

#changes(from, to, out = Dumper.new) ⇒ Object

Generate code for changing one schema to another.



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/miguel/migrator.rb', line 194

def changes( from, to, out = Dumper.new )
  from_keys = prepare_keys( from.tables )
  to_keys = prepare_keys( to.tables )

  old_keys, same_keys, new_keys = separate( from_keys, to_keys )

  from_names = from.table_names
  to_names = to.table_names

  old_names, same_names, new_names = separate( from_names, to_names )

  old_tables = from.named_tables( old_names )
  new_tables = to.named_tables( new_names )

  from_tables = from.named_tables( same_names )
  to_tables = to.named_tables( same_names )

  dump_drop_foreign_keys( out, old_keys )
  dump_drop_tables( out, old_tables )
  dump_alter_tables( out, from_tables, to_tables )
  dump_add_tables( out, new_tables )
  dump_add_foreign_keys( out, new_keys )

  out
end

#full_migration(from, to, out = Dumper.new) ⇒ Object Also known as: migration

Generate both ways Sequel migration.



230
231
232
233
234
235
236
237
238
239
# File 'lib/miguel/migrator.rb', line 230

def full_migration( from, to, out = Dumper.new )
  out.dump "Sequel.migration" do
    out.dump "up" do
      changes( from, to, out )
    end
    out.dump "down" do
      changes( to, from, out )
    end
  end
end