Remove UTF-8 BOM from query strings

Issue #196 wontfix
Ricky Ng-Adam
created an issue

Bang my head on creating a view from within an ActiveRecord migration for a while:

ActiveRecord::StatementInvalid: PG::SyntaxError: ERROR:  syntax error at or near "CREATE"
LINE 1: CREATE OR REPLACE VIEW searchables AS SELECT entity_id as s...

I wanted to use a file as input. Although the same string directly in the source code would work, the output from IO.read wouldn't. I finally traced this down to the "invisible" BOM being submitted to pg and fixed it by removing it:

    filename = File.expand_path('../views/searchables.sql', File.dirname(__FILE__))
    # remove UTF-8 BOM
    execute IO.read(filename).gsub!("\xEF\xBB\xBF".force_encoding("UTF-8"), '')

...could this be done automatically?

Comments (3)

  1. Lars Kanis

    It could, but since the PostgreSQL developers decided to treat the BOM as text, I don't see a reason to have a different view on this. However you can use IO.read like this:

    execute IO.read(filename, open_args: ["r:bom|utf-8"] )
    
  2. Log in to comment