rubyonjets / jets

Ruby on Jets
http://rubyonjets.com
MIT License
2.6k stars 181 forks source link

Migrations fail to run on test database #337

Closed murjax closed 5 years ago

murjax commented 5 years ago

Checklist

My Environment

Software Version
Operating System MacOS Mojave 10.14.6
Jets 2.0.3
Ruby 2.5.3

Expected Behaviour

When I create a migration and run jets db:migrate (no JETS_ENV or JETS_ENV_REMOTE specified), I expect the migration to be applied to the local development and test databases.

Current Behavior

When I create a migration and run jets db:migrate (no JETS_ENV or JETS_ENV_REMOTE specified), the migration is applied to the local development database only.

Step-by-step reproduction instructions

Create a demo application:

  1. jets new demo
  2. cd demo.
  3. jets generate scaffold post title:string
  4. jets db:create.
  5. jets db:migrate.

Write a test that touches the posts table. I wrote a controller test for this example:

describe PostsController, type: :controller do
  it "index returns a success response" do
    get '/posts'
    expect(response.status).to eq 200
  end
end

Running this test, I get the following error:

ActionView::Template::Error: Mysql2::Error: Table 'demo_test.posts' doesn't exist: SELECT `posts`.* FROM `posts`

Now I run the migration again specifying the test environment: JETS_ENV=test jets db:migrate.

Upon running the test again, the test passes because the posts table now exists.

Code Sample

Clone this sample application and follow the reproduction steps in the README.

peiyee commented 5 years ago

@tongueroo can i take this issue? Will open a PR if i can found a fix for it.

tongueroo commented 5 years ago

Handled by #346 Thanks to @peiyee

Note, the behavior that jets db:migrate only migrates the development db but not the test db is consistent with rails db:migrate. So only the development db will get migrated, not the test db.

Interestingly, the db:create command creates databases for both the development and test. Guessing db:migrate does not behave the same way as db:create because you may still be working on migration code and it would be annoying to deal with cleaning up the accidentally migrated test db.

Anyway, @peiyee PR #346 addresses this by checking at the beginning of running specs whether or not the migrations have been ran on the test DB yet. If the test db does not have update to date migrations, it will exit and print an error.