/var/www/discourse の Gemfile を使用して rails runner を使用する方法

sqlite3 からデータを読み込んで Discourse にインポートする必要があるスクリプトがあります。

rails runner

/shared/add_in_reverse.rb に次のようなものを使用できると仮定しました。

require "sqlite3"

# データベースを開く
db = SQLite3::Database.new "sqlite3.db"

k = 1234
db.execute( "SELECT * from foo where id = ?'", [ k ] ) do |row|
   puts row
end

title = "hello there"
u = User.find_or_create_by(username: "mexample") do |user|
   user.email = "me@example.com"
end
c = Category.find_or_create_by(name: "Something by mexample") do |category|
   category.user = u
end
Topic.create!( title: title, category: c, user: u )

そして、次のように実行します。

rails r /shared/add_in_reverse.rb

しかし、これは cannot load such file -- sqlite3 (LoadError) で失敗します。

bundler: failed to load command: script/rails (script/rails)

internal:/usr/local/lib/ruby/site_ruby/3.2.0/rubygems/core_ext/kernel_require.rb:38:in `require': cannot load such file -- sqlite3 (LoadError)
        from internal:/usr/local/lib/ruby/site_ruby/3.2.0/rubygems/core_ext/kernel_require.rb:38:in `require'
        from /var/www/discourse/vendor/bundle/ruby/3.2.0/gems/bootsnap-1.17.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:17:in `require'
        from /var/www/discourse/vendor/bundle/ruby/3.2.0/gems/zeitwerk-2.6.12/lib/zeitwerk/kernel.rb:38:in `require'

Gemfile と bundle exec?

Ruby は時々 Gemfile を使用する必要があることを思い出しました。そのため、/var/www/discourse ディレクトリ内にいる場合、bundle exec を前に付けることができると考えました。しかし、それは失敗します。

#  bundle exec rails r /shared/add_in_reverse.rb
fatal: detected dubious ownership in repository at '/var/www/discourse'
To add an exception for this directory, call:

        git config --global --add safe.directory /var/www/discourse
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/activerecord-7.0.7/lib/active_record/connection_adapters/postgresql_adapter.rb:81:in `rescue in new_client': We could not find your database: discourse. Which can be found in the database configuration file located at config/database.yml. (ActiveRecord::NoDatabaseError)

To resolve this issue:

- Did you create the database for this app, or delete it? You may need to create your database.
- Has the database name changed? Check your database.yml config has the correct database name.
...

IMPORT=1

わかりました。IMPORT=1 を含む例を見ました。それが必要ですか?

IMPORT=1 rails r /shared/add_in_reverse.rb
You are trying to install in deployment mode after changing
your Gemfile. Run `bundle install` elsewhere and add the
updated Gemfile.lock to version control.

If this is a development machine, remove the /var/www/discourse/Gemfile freeze
by running `bundle config unset deployment`.

The list of sources changed
The dependencies in your gemfile changed

You have added to the Gemfile:
* mysql2
* redcarpet
* sqlite3 (~> 1.3, >= 1.3.13)
* ruby-bbcode-to-md
* reverse_markdown
* tiny_tds
* csv
* parallel
# IMPORT=1 bundle exec rails r /shared/add_in_reverse.rb
You are trying to install in deployment mode after changing
your Gemfile. Run `bundle install` elsewhere and add the
updated Gemfile.lock to version control.

If this is a development machine, remove the /var/www/discourse/Gemfile freeze
by running `bundle config unset deployment`.

The list of sources changed
The dependencies in your gemfile changed

You have added to the Gemfile:
* mysql2
* redcarpet
* sqlite3 (~> 1.3, >= 1.3.13)
* ruby-bbcode-to-md
* reverse_markdown
* tiny_tds
* csv
* parallel

Gemfile は一切変更していません。Gemfile で指定されている sqlite を使用したいだけです。

なぜ 2 つの rails/ruby?

問題は、パスにあるものが間違っていて、バンドルされた Ruby を使用すべきということでしょうか?Ruby と Rails がたくさんありますが、バンドルされたものを使用しても機能しません(または、正しく使用する方法がわかりません)。

# which ruby
/usr/local/bin/ruby
# which rails
/usr/local/bin/rails
# find /var/www/discourse/ | grep ruby | grep vendor | grep bin | grep rails
/var/www/discourse/vendor/bundle/ruby/3.2.0/bin/rails

# /var/www/discourse/vendor/bundle/ruby/3.2.0/bin/rails runner /shared/add_in_reverse.rb
/usr/local/lib/ruby/site_ruby/3.2.0/rubygems.rb:264:in `find_spec_for_exe': can't find gem railties (>= 0.a) with executable rails (Gem::GemNotFoundException)
        from /usr/local/lib/ruby/site_ruby/3.2.0/rubygems.rb:283:in `activate_bin_path'
        from /var/www/discourse/vendor/bundle/ruby/3.2.0/bin/rails:25:in `<main>'

バッククォートで sqlite バイナリを使用できると思います。結局、1行しか取得していません。しかし、私がやろうとしていることが簡単ではない理由がわかりません。sqlite はすべての新しい Rails プロジェクトのデフォルトの DB だと思っていました。Discourse では意図的に無効にされたのでしょうか?