jish/pre-commit

Migration check reports a failure on initial commit of schema.rb

Closed this issue · 1 comments

When creating a new Rails project, one of the first steps I do is run rake db:migrate which creates a schema.rb file. There are no migrations yet, and the schema.rb has a schema version of 0, like this:

ActiveRecord::Schema.define(version: 0) do
end

When trying to commit this to git, this causes the migration check to fail due to this condition:

elsif migration_files.none? && schema_files.any?
  "You're trying to change the schema without adding a migration file"

Is there an easy way to fix this?

Or, is this my fault and I shouldn't be running db:migrate until I've defined my first migration? 😄

jish commented

"Is there an easy way to fix this?"

That's a good question. The logic is currently pretty simplistic. Basically, "did you modify a migration file, and not the schema", or the other way around.

A workaround is to just commit using git commit -n in this case since you know you want to modify the schema without adding a migration.

I guess we could add in another condition for this specific case which could check for version: 0 or something...