/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/my_custom_script.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를 사용하려는 것입니다.

왜 rails/ruby가 두 개나 있나요?

문제가 경로에 있는 ruby가 잘못된 것이어서 vendored ruby를 사용해야 하는 것인가요? ruby와 rails가 여러 개 있는 것을 볼 수 있습니다. 하지만 vendored 버전을 사용하면 작동하지 않습니다(또는 올바르게 사용하는 방법을 모릅니다).

# 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 바이너리에 백틱을 그냥 사용해도 되는 것 같네요. 어차피 한 줄만 가져오려는 건데요. 그런데 제가 하려는 일이 왜 이렇게 어려운지 모르겠어요. sqlite가 모든 새로운 Rails 프로젝트의 기본 DB인 줄 알았는데, Discourse에서는 의도적으로 비활성화된 건가요?