Editing a schema.rb file


Can you edit a schema.rb file after a rails migration?

I ran a migration and there are new column names that don’t match up with the table columns found in the main branch.


Yes and no.

The schema.rb is usually checked into the repo and is considered the authoritative source of the schema for the application. The migrations are just used as a way to modify that schema incrementally across different systems (different developers, different environments, etc). In that sense whatever you schema.rb file says is the system is the schema.

That being said, the schema.rb is generated and a dump of your database schema. Therefore if you manually edit it to have things not in your database those changes will just be overwritten the next time a new copy is generated.

The question is why are there new column names being dumped that aren’t already in the schema.rb? Is that from other branches that haven’t been merged in yet? In that case I wouldn’t add those to the schema until those branches are merged in. The -p flag is useful on git add to pick and choose what parts of the schema.rb file you want to commit vs leave out.

If the extra columns were manually added to your database vs going through migrations are they in all databases (i.e. all devs, all environments)? If not then you don’t have a way for other databases to incrementally change their schema to match. In that case you might want to create a migration for those other systems making sure that the migration is idempotent for systems that had the columns added manually. If there aren’t other systems (i.e. no other devs, no production env, etc) then you can just skip doing the migration, commit the schema.rb file as dumped and call it a day.