Standalone ruby -- How to load different environments from database.yml
I would set up the connection once at the start of your background process. Once you've established the connection once, all of the models will work correctly anyway.
Your connection establishment code would look something like this:
@environment = ENV['RACK_ENV'] || 'development'
@dbconfig = YAML.load(File.read('config/database.yml'))
ActiveRecord::Base.establish_connection @dbconfig[@environment]
How to use ActiveRecord for Multiple Environment outside Rails?
What rails does may look like magic, but it's actually very simple (well, this case, at least). Here's the outline:
At startup, app loads all available database configurations. By convention, they are stored in YAML format in config/database.yml
.
Then, current environment is determined. The easiest way to do this is environment variables. For example:
MY_ENV=production ruby my_script.rb
Then, in the script, you fetch current env, pick corresponding connection configuration and use it to connect.
connection_configs = YAML.load(File.read('config/database.yml'))
current_env = ENV['MY_ENV'] || 'development' # if no value, assume development mode
ActiveRecord::Base.establish_connection(connection_configs[current_env])
RoR environment in Ruby standalone script
The easiest way is to change the shebang of your script from :
#!/usr/bin/ruby
to
#!/path/to/your/rails/script/runner
Et voilà, your script will be run with the full rails environment loaded. You can also run your script as ./my_script -e production
to have it run with the production database.
Rails database.yml configuration with different sockets on dev, test and production
You can specify the Rails environment when you run the Rake task.
rake db:migrate RAILS_ENV=production
How can I load ActiveRecord database tasks on a Ruby project outside Rails?
You could try the standalone-migrations gem:
https://github.com/thuss/standalone-migrations
passenger start standalone with RAILS_ENV=production
Solved! I passed now this command:
passenger start -a 127.0.0.1 -p 3000 -d --environment production
Trouble connecting to database in standalone ActiveRecord app
I do this also, but I don't use rake, I just use pure ruby. Here are a couple of methods from my project where I use Active record outside of rails.
Basically I connect to the database, check to see if the tables exist, and if not I load the schema file. Once the schema is loaded, I just use normal ActiveRecord methods to populate the database and do queries. I have no need to do migrations so I have not tried that
class MyDb
attr_reader :file_name
def initialize(file_name)
@file_name = file_name
end
def connect()
@connection = ActiveRecord::Base.establish_connection(
adapter: "sqlite3",
database: filename,
)
create_tables unless tables_exists?
end
def tables_exists?
ActiveRecord::Base.connection.tables.size > 0
end
def create_tables()
puts "database is empty...\ncreating tables"
path_to_current_file = File.expand_path(File.dirname(__FILE__))
load("#{path_to_current_file}/ar_support/schema.rb")
end
end
Then from inside a top level ruby script you can do something like.
my_db = MyDb.new("mydb.sqlite3")
my_db.connect
Here is an example schema.rb file that I also use.
ActiveRecord::Schema.define(version: 20180430230151) do
create_table "chips", force: :cascade do |t|
t.string "name"
end
create_table "macros", force: :cascade do |t|
t.string "name"
t.integer "chip_id"
t.index ["chip_id"], name: "index_macros_on_chip_id"
end
create_table "measurements", force: :cascade do |t|
t.string "name"
t.float "value"
t.string "group_name"
t.string "plot"
t.boolean "test_result"
t.string "test_expression"
t.integer "run_id"
t.index ["run_id"], name: "index_measurements_on_run_id"
end
create_table "runs", force: :cascade do |t|
t.string "source_dir"
t.string "source_file"
t.integer "macro_id"
t.string "corner_name"
t.string "sim_type"
t.string "netlist_type"
t.index ["macro_id"], name: "index_runs_on_macro_id"
end
create_table "tags", force: :cascade do |t|
t.string "name"
t.integer "run_id"
t.string "value"
t.index ["run_id"], name: "index_tags_on_run_id"
end
end
Start using the database. (You also need to define your models)
chip = Chip.find_or_create_by(name: e.chip)
macro = chip.macros.find_or_create_by(name: e.macro)
configuration file for a standalone ruby script
First, you can run an independent script that writes to a YAML configuration file:
require "yaml"
File.write("path_to_yaml_file", [arg1, arg2].to_yaml)
Then, read it within your app:
require "yaml"
arg1, arg2 = YAML.load_file("path_to_yaml")
# use arg1, arg2
...
Related Topics
Devise Not Working Well with Multiple Subdomains on Ror3 Application
Solving Dependency Constraints
Simulating Race Conditions in Rspec Unit Tests
Generating Sequential Numbers in Multi-User Saas Application
Ruby Rake Load Tasks from a Gem
Check If the Sum of Two Different Numbers in an Array Equal a Variable Number
How to Refer a Local Gem in Ruby
Install Bundler Using Rvm, Ruby 1.8.7 with Gcc 4.7 on Linux
What Purpose Can Anonymous Modules Serve
Why Array.Reverse_Each Is Faster Than Array.Reverse.Each
Ruby - No Pid Found in Tmp/Pids/Thin.Pid (Thin::Pidfilenotfound)
Watermark in Existing PDF in Ruby
How to Refresh a Page with Turbolinks
Rails Installation Error :The 'Atomic' Native Gem Requires Installed Build Tools
How to Have One Resource in Routes for Namespace and Root Path Altogether - Rails 4
Ruby on Rails: Two References with Different Name to the Same Model