Class: LolDba::Migration
- Inherits:
-
Object
- Object
- LolDba::Migration
- Defined in:
- lib/lol_dba/sql_migrations/migration.rb
Instance Attribute Summary collapse
-
#full_name ⇒ Object
readonly
Returns the value of attribute full_name.
-
#writer ⇒ Object
readonly
Returns the value of attribute writer.
Instance Method Summary collapse
- #connection ⇒ Object
- #down ⇒ Object
- #generate_instead_of_executing ⇒ Object
-
#initialize(migration_file) ⇒ Migration
constructor
A new instance of Migration.
- #migration_class ⇒ Object
- #name ⇒ Object
- #number ⇒ Object
- #to_s ⇒ Object
- #up ⇒ Object
Constructor Details
#initialize(migration_file) ⇒ Migration
Returns a new instance of Migration.
5 6 7 8 9 |
# File 'lib/lol_dba/sql_migrations/migration.rb', line 5 def initialize(migration_file) @full_name = File.basename(migration_file, '.rb') @writer = LolDba::Writer.new("#{@full_name}.sql") require Rails.root.join(migration_file) end |
Instance Attribute Details
#full_name ⇒ Object (readonly)
Returns the value of attribute full_name.
3 4 5 |
# File 'lib/lol_dba/sql_migrations/migration.rb', line 3 def full_name @full_name end |
#writer ⇒ Object (readonly)
Returns the value of attribute writer.
3 4 5 |
# File 'lib/lol_dba/sql_migrations/migration.rb', line 3 def writer @writer end |
Instance Method Details
#connection ⇒ Object
41 42 43 |
# File 'lib/lol_dba/sql_migrations/migration.rb', line 41 def connection ActiveRecord::Base.connection end |
#down ⇒ Object
34 35 36 37 38 39 |
# File 'lib/lol_dba/sql_migrations/migration.rb', line 34 def down generate_instead_of_executing do migration_class.migrate(:down) connection.execute("DELETE FROM schema_migrations WHERE version = #{number}") end end |
#generate_instead_of_executing ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/lol_dba/sql_migrations/migration.rb', line 45 def generate_instead_of_executing migration_mocker = LolDba::MigrationMocker.new(writer) migration_mocker.redefine_migration_methods yield migration_mocker.reset_methods end |
#migration_class ⇒ Object
23 24 25 |
# File 'lib/lol_dba/sql_migrations/migration.rb', line 23 def migration_class name.camelize.split('.')[0].constantize end |
#name ⇒ Object
15 16 17 |
# File 'lib/lol_dba/sql_migrations/migration.rb', line 15 def name /^(\d+)_(.*)$/.match(full_name)[2] end |
#number ⇒ Object
11 12 13 |
# File 'lib/lol_dba/sql_migrations/migration.rb', line 11 def number /^(\d+)_(.*)$/.match(full_name)[1] end |
#to_s ⇒ Object
19 20 21 |
# File 'lib/lol_dba/sql_migrations/migration.rb', line 19 def to_s full_name end |
#up ⇒ Object
27 28 29 30 31 32 |
# File 'lib/lol_dba/sql_migrations/migration.rb', line 27 def up generate_instead_of_executing do migration_class.migrate(:up) connection.execute("INSERT INTO schema_migrations (version) VALUES (#{number})") end end |