DB Migrations #27
-
Hello, Thank you so much for getting this together! I was trying it out but the specs currently fails because he missing migrations.
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 |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments
-
@kevinrobayna Hi there, can you post the error backtrace here to investigate this in detail? |
Beta Was this translation helpful? Give feedback.
-
oh sorry it just fails saying that the migrations aren't applied |
Beta Was this translation helpful? Give feedback.
-
@kevinrobayna I hope you're doing well, can you give us more context? container = Testcontainers::PostgresContainer.new("postgres:15")
client = PG.connect(host: 'localhost', user: "test", password: "test", port: 57567, dbname: "test")
client.exec("SELECT 1 AS number").first the previous code was executed on Linux and Mac. I think that you can give us more context if you run: container.start
container.logs and paste here the output |
Beta Was this translation helpful? Give feedback.
-
Hi @kevinrobayna, this configuration worked for me in Rails project with RSpec: First, looks like ActiveRecord connections are loaded before the 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 'rake' somewhere in the top of the file.
# Checks for pending migrations and applies them before tests are run.
# If you are not using ActiveRecord, you can remove these lines.
begin
ActiveRecord::Migration.maintain_test_schema!
rescue ActiveRecord::PendingMigrationError => e
abort e.to_s.strip
end And finally, stopped and removed the container in the config.after(:suite) do
config.postgres_container&.stop
config.postgres_container&.remove
end Also, IIRC, the config of database.yml have over DATABASE_URL so if you want to use the env variable instead of using As you can see, this requires extra setup (calling
Let me know if the suggestion works for your project. |
Beta Was this translation helpful? Give feedback.
-
Thank you for looking into this. TIL, the connection happens even the before 😅 will try that on a repository at work and let you know of the findings! |
Beta Was this translation helpful? Give feedback.
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 theActiveRecord::Migration.maintain_test_schema!
generated by RSpec installer by default: