[BUG] Database migrations won't run only on test environment
DerekCrosson opened this issue · 1 comments
Describe the bug
When I run the rails rswag:specs:swaggerize
command with a test database that is migrated, the tables are removed and the database cannot migrate. This is an example of the error:
❯ rails rswag:specs:swaggerize
/Users/derek/.rbenv/versions/3.2.2/bin/ruby -I/Users/derek/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rspec-core-3.12.2/lib:/Users/derek/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rspec-support-3.12.1/lib /Users/derek/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rspec-core-3.12.2/exe/rspec --pattern spec/requests/\*\*/\*_spec.rb,\ spec/api/\*\*/\*_spec.rb,\ spec/integration/\*\*/\*_spec.rb --format Rswag::Specs::SwaggerFormatter --dry-run --order defined
rails aborted!
StrongMigrations::UnsafeMigration:
=== Dangerous operation detected #strong_migrations ===
The force option will destroy existing tables.
If this is intended, drop the existing table first.
Otherwise, remove the force option.
/Users/derek/CodeRepos/project/db/schema.rb:17:in `block in <main>'
/Users/derek/CodeRepos/project/db/schema.rb:13:in `<main>'
Tasks: TOP => db:test:load_schema
(See full trace by running task with --trace)
Migrations are pending. To resolve this issue, run:
bin/rails db:migrate RAILS_ENV=test
You have 10 pending migrations:
20230805234244_create_users.rb
20230806004118_create_profiles.rb
20230808221223_create_organisations.rb
20230808221647_create_departments.rb
20230808221947_create_roles.rb
20230808222357_create_memberships.rb
20230808223257_add_roles.rb
20230808223822_create_system_organisation_and_super_admin_department.rb
20230808224151_create_system_user_and_profile.rb
20230808224258_create_membership_for_system_user.rb
No examples found.
/Users/derek/.rbenv/versions/3.2.2/bin/ruby -I/Users/derek/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rspec-core-3.12.2/lib:/Users/derek/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rspec-support-3.12.1/lib /Users/derek/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rspec-core-3.12.2/exe/rspec --pattern spec/requests/\*\*/\*_spec.rb,\ spec/api/\*\*/\*_spec.rb,\ spec/integration/\*\*/\*_spec.rb --format Rswag::Specs::SwaggerFormatter --dry-run --order defined failed
Even if I manually edit the schema.rb file and remove all instance of force: :cascade
this code is added back to the schema.rb file only when rswag is run. If I migrate the databases using the rails migrate command there is no issue (with or without the force: :cascade code). None of my migrations have force: :cascade in them.
Steps to Test or Reproduce
Install strong migrations and migrate both the development and test database, then run: rails rswag:specs:swaggerize
Expected behavior
There are no errors and the API documentation is generated from the rspec specs.
Additional context
It seems like rswag may be running db:schema:dump
internally to make the schema to be different from what's being auto-generated by Rails but I can't find this in the code so I'm not sure if this is the case.
Dependency versions
The version of are you using for:
- Rswag: 2.9
- RSpec: 3.120.0
- Rails: 7.0.6
- Ruby: 3.2.2
Relates to which version of OAS (OpenAPI Specification)
- OAS2
- OAS3
- OAS3.1
Seems like the migration issue is in rspec, I disabled migrations from running in my test environment using config.active_record.maintain_test_schema = false
. Now when I generate the docs only the pre-existing specs are inserted into the swagger.yaml file. Any new specs do not get added. All tests do run when I run the rspec
.
Edit: Fixed, one of my tests was causing issues. Happy to keepactive_record.maintain_test_schema
turned off for now.