Invoking a large set of SQL from a Rails 4 application

前端 未结 8 1022
有刺的猬
有刺的猬 2020-12-30 07:11

I have a Rails 4 application that I use in conjunction with sidekiq to run asynchronous jobs. One of the jobs I normally run outside of my Rails application is

8条回答
  •  失恋的感觉
    2020-12-30 07:38

    I see this post is kind of old. But I would like to add my solution to it. I was in a similar situation; I also needed a way to force feed "PRAGMA foreign_keys = on;" into my sqlite connection (I could not find a previous post that spelled it out how to do it.) Anywho, this worked like a charm for me. It allowed me to write "pretty" sql and still get it executed. Blank lines are ignored by the if statement.

    conn = ActiveRecord::Base.establish_connection(adapter:'sqlite3',database:DB_NAME)
    sqls = File.read(DDL_NAME).split(';')
    sqls.each {|sql| conn.connection.execute(sql<<';') unless sql.strip.size == 0 }
    conn.connection.execute('PRAGMA foreign_keys = on;')
    

提交回复
热议问题