vendredi 26 octobre 2018

open migration file from script

I'm writing a script to alter a bunch of tables at once in an app. But I'm stuck trying to write to the migration file that I'm generating,

here's what I have in the tool:

system "rails g migration AddDefaultValueToBooleans"
file = "#{Rails.root}db/migrate/*add_default_value_to_booleans.rb"

def replace(filepath, regexp, *args, &block)
  content = File.read(filepath).gsub(regexp, *args, &block)
  File.open(filepath, 'wb') { |file| file.write(content) }
end

replace( file, /^def change/mi) { |match| "change_column :my_table, :my_column, null: false, default: false" } 

replace is a nifty little method i found here https://stackoverflow.com/a/2141540/8480602

but it's not finding my file. i keep getting the error
No such file or directory @ rb_sysopen - root_path/migrate/*add_default_value_to_booleans.rb (Errno::ENOENT)

the filepath i'm using is wrong, but I can't seem to figure out how to write it correctly

Aucun commentaire:

Enregistrer un commentaire