Skip to content
\n

Do you have any ideas of wether we can provide to the testcontainer a schema file to start with or something to avoid running db migrations in the specs

","upvoteCount":1,"answerCount":5,"acceptedAnswer":{"@type":"Answer","text":"

Hi @kevinrobayna, this configuration worked for me in Rails project with RSpec:

\n

First, looks like ActiveRecord connections are loaded before the before(:suite) block, so we need to create the container before, for reference I put the configuration just before the ActiveRecord::Migration.maintain_test_schema! generated by RSpec installer by default:

\n
RSpec.configuration.add_setting :postgres_container, default: nil\nRSpec.configuration.postgres_container = Testcontainers::PostgresContainer.new(\"postgres:15\").start\nENV['DATABASE_URL'] = RSpec.configuration.postgres_container.database_url(database: 'pgblog_test')\n\nRails.application.load_tasks\nRake::Task['db:test:prepare'].invoke # require 'rake' somewhere in the top of the file.\n# Checks for pending migrations and applies them before tests are run.\n# If you are not using ActiveRecord, you can remove these lines.\nbegin\n  ActiveRecord::Migration.maintain_test_schema!\nrescue ActiveRecord::PendingMigrationError => e\n  abort e.to_s.strip\nend
\n

And finally, stopped and removed the container in the after(:suite) block in the rails_helper.rb:

\n
  config.after(:suite) do\n    config.postgres_container&.stop\n    config.postgres_container&.remove\n  end
\n

Also, IIRC, the config of database.yml have over DATABASE_URL so if you want to use the env variable instead of using establish_connection manually you can set the url: <%= ENV[\"DATABASE_URL\"] %> key in database.yml.

\n

As you can see, this requires extra setup (calling rake db:test:prepare in RSpec instead of invoking it before running the rspec command). I think we should add an RSpec integration to the gem to make all this easier, e.g simplify everything with just:

\n
 require 'testcontainers/postgres/rspec'\n
\n

Let me know if the suggestion works for your project.

","upvoteCount":1,"url":"https://github.com/testcontainers/testcontainers-ruby/discussions/27#discussioncomment-6826483"}}}
Discussion options

You must be logged in to vote

Hi @kevinrobayna, this configuration worked for me in Rails project with RSpec:

First, looks like ActiveRecord connections are loaded before the before(:suite) block, so we need to create the container before, for reference I put the configuration just before the ActiveRecord::Migration.maintain_test_schema! generated by RSpec installer by default:

RSpec.configuration.add_setting :postgres_container, default: nil
RSpec.configuration.postgres_container = Testcontainers::PostgresContainer.new("postgres:15").start
ENV['DATABASE_URL'] = RSpec.configuration.postgres_container.database_url(database: 'pgblog_test')

Rails.application.load_tasks
Rake::Task['db:test:prepare'].invoke # require 'rak…

Replies: 5 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by guilleiguaran
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants
Converted from issue

This discussion was converted from issue #17 on August 25, 2023 20:03.