LoginSignup
0
0

More than 1 year has passed since last update.

test時にActiveRecord::DatabaseAlreadyExists: Text file busyが発生!!

Posted at

はじめに

Ruby on Railsでアプリを開発中にテストを行うと、下記のエラーが発生しました(提案されたコマンドdb:test:load => db:test:purgeを入力しても解決せず)。

terminal
$ rails t
rake aborted!
ActiveRecord::DatabaseAlreadyExists: Text file busy @ apply2files - /home/vagrant/workspace/railstutorial6_sample/db/test.sqlite3
-e:1:in `<main>'

Caused by:
Errno::ETXTBSY: Text file busy @ apply2files - /home/vagrant/workspace/railstutorial6_sample/db/test.sqlite3
-e:1:in `<main>'
Tasks: TOP => db:test:load => db:test:purge
(See full trace by running task with --trace)

多くの方の資料を参考に解決できました。ありがとうございます‼
この問題は、アプリを開発する際に都度発生しそうなので、備忘録として書き留めておきます。

開発環境

  • HostOS: windows11 Home
  • GestOS: ubuntu/xenial64
  • ruby: 2.7.5
  • Ruby on Rails: 6.1.6

なぜ発生するのか

windowsを使用すると仮想環境で作成することが多いと思いますが、この問題はVagrantの共有フォルダ内にプロジェクトを作成すると都度発生するもののようです。

なにが原因が正直わかりませんが、詳細がわかれば追記していきます。

解決方法

解決方法としては、2点。

  • プロジェクトを共有フォルダ外に作成する
  • database.ymlで指定されている参照先を共有フォルダ外に指定する(test.sqlite3を共有フォルダ外に複製)
    の方法があります(他の方法もあるかも...)。

プロジェクトはローカルと共有しておきたいので、今回はdatabase.ymlの参照先を変更する方法(test.sqlite3を移動)を使用します。

手順

  1. 共有フォルダ外にフォルダを作成します
  2. 作成したフォルダにファイル(test.sqlite3など)を複製します
  3. database.ymlで指定されている参照先を変更します

ファルダの作成

ちなみに、共有フォルダは以下の設定にしています。

Vagrantfile
config.vm.define :ruby_machine do |machine|
  machine.vm.synced_folder "./workspace", "/home/vagrant/workspace"
end

では、共有フォルダ外にファルダを作成していきます。

terminal
vagrant at ubuntu-xenial in ~
$ mkdir rails_db

ファイルの複製

次に作成したフォルダに下記のファイルのコピーを作成します。ついでに、development.sqlite3のファイルも移動させます。

terminal
# プロジェクトへ移動
# rails_dbフォルダへファイルを複製
vagrant at ubuntu-xenial in ~/workspace/railstutorial_app
$ cp db/development.sqlite3 db/test.sqlite3 /home/vagrat/rails_db

# 複製されているかの確認
$ ll /home/vagrant/rails_db
-rwxrwxr-x 1 vagrant vagrant 5.0K May 26 03:49 development.sqlite3
-rw-r--r-- 1 vagrant vagrant 5.0K May 26 03:51 test.sqlite3

database.ymlの設定変更

下記のdatabase: db/development.sqlite3 database: db/test.sqlite3の箇所をコメントアウトし、先程複製したファイルのパスを設定します.

database.yml

development:
  <<: *default
  # database: db/development.sqlite3
  database: /home/vagrant/rails_db/development.sqlite3

test:
  <<: *default
  # database: db/test.sqlite3
  database: /home/vagrant/rails_db/test.sqlite3

これでrails tが通るようになりました.

terminal
$ rails t
Running via Spring preloader in process 4444
Run options: --seed 45160

# Running:

..

Finished in 1.530231s, 1.9605 runs/s, 1.3070 assertions/s.
2 runs, 2 assertions, 0 failures, 0 errors, 0 skips

参考資料

Railsチュートリアル3章のエラー解決×2
[小ネタ][Ruby on Rails]bundle install時にText file busy エラー
今更ながら Ruby on Rails Tutorial をやってみた (その7)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0