Method: RubyPitaya::RubyPitaya.create_migration

Defined in:
lib/rubypitaya.rb

.create_migration(migration_name) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rubypitaya.rb', line 22

def self.create_migration(migration_name)
  migration_name = "#{migration_name}_migration" unless migration_name.underscore.end_with?('migration')
  migration_timestamp = Time.now.utc.to_i
  migration_file_name = "#{migration_timestamp}_#{migration_name.underscore}.rb"
  migration_class_name = migration_name.camelcase

  template_struct = OpenStruct.new(
    class_name: migration_class_name,
  )

  template = File.open(Path::MIGRATION_TEMPLATE_PATH, &:read)
  template_result = ERB.new(template).result(template_struct.instance_eval { binding })

  migration_file_path = File.join(Path::MIGRATIONS_FOLDER_PATH, migration_file_name)
  File.open(migration_file_path, 'w') { |f| f.write(template_result) }

  migration_file_name
end