Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does rake db:migrate sometimes add trailing whitespace to structure.sql?

On some of our developer machines rake db:migrate adds trailing whitespace to structure.sql which is really annoying because every time a change is made to the database we have to first remove all trailing whitespace from the file.

Anyone know what could be up with that? Where would the whitespace come from? Has it to do with PostgreSQL or is it something else?

like image 779
mhenrixon Avatar asked Nov 15 '12 09:11

mhenrixon


3 Answers

Here's a solution that you can commit to version control: trim the trailing whitespace as a db:migrate hook.

In lib/tasks/db.rake:

namespace :db do
  def remove_whitespace_in_structure
    if Rails.env.development?
      `sed -i '' -e's/[[:space:]]*$//' db/structure.sql`
    end
  end

  task :migrate do
    remove_whitespace_in_structure
  end
end

The above code may look like it's overwriting db:migrate, but it's a hook that will run right after the normal db:migrate task.

like image 112
Charlie Tran Avatar answered Nov 13 '22 08:11

Charlie Tran


I just set up a git filter for this. Unfortunately, this is not something you can add to the repo; each team member will have to set it up.

In .gitconfig (or .git/config)

[filter "remove-trailing-whitespace"]
  clean = sed -E 's/[[:space:]]*$//'
  smudge = cat

In .gitattributes or .git/info/attributes

db/structure.sql filter=remove-trailing-whitespace

See the documentation on gitattributes for more information on git filters.

like image 45
Amiel Martin Avatar answered Nov 13 '22 08:11

Amiel Martin


It is the best day of my life, and maybe yours:

In PG 11, this error is not here anymore since Tablespace is not written to the dump. So you can upgrade your PG version and get rid of the hook you had for space deletion.

like image 1
Ulysse BN Avatar answered Nov 13 '22 07:11

Ulysse BN