switching from MySQL to PostgreSQL for Ruby on Rails for the sake of Heroku

前端 未结 4 580
臣服心动
臣服心动 2021-01-12 13:53

I\'m trying to push a brand new Ruby on Rails app to Heroku. Currently, it sits on MySQL. It looks like Heroku doesn\'t really support MySQL and so we are considering using

4条回答
  •  伪装坚强ぢ
    2021-01-12 14:38

    Common issues:

    1. GROUP BY behavior. PostgreSQL has a rather strict GROUP BY. If you use a GROUP BY clause, then every column in your SELECT must either appear in your GROUP BY or be used in an aggregate function.
    2. Data truncation. MySQL will quietly truncate a long string to fit inside a char(n) column unless your server is in strict mode, PostgreSQL will complain and make you truncate your string yourself.
    3. Quoting is different, MySQL uses backticks for quoting identifiers whereas PostgreSQL uses double quotes.
    4. LIKE is case insensitive in MySQL but not in PostgreSQL. This leads many MySQL users to use LIKE as a case insensitive string equality operator.

    (1) will be an issue if you use AR's group method in any of your queries or GROUP BY in any raw SQL. Do some searching for column "X" must appear in the GROUP BY clause or be used in an aggregate function and you'll see some examples and common solutions.

    (2) will be an issue if you use string columns anywhere in your application and your models aren't properly validating the length of all incoming string values. Note that creating a string column in Rails without specifying a limit actually creates a varchar(255) column so there actually is an implicit :limit => 255 even though you didn't specify one. An alternative is to use t.text for your strings instead of t.string; this will let you work with arbitrarily large strings without penalty (for PostgreSQL at least). As Erwin notes below (and every other chance he gets), varchar(n) is a bit of an anachronism in the PostgreSQL world.

    (3) shouldn't be a problem unless you have raw SQL in your code.

    (4) will be an issue if you're using LIKE anywhere in your application. You can fix this one by changing a like b to lower(a) like lower(b) (or upper(a) like upper(b) if you like to shout) or a ilike b but be aware that PostgreSQL's ILIKE is non-standard.

    There are other differences that can cause trouble but those seem like the most common issues.

    You'll have to review a few things to feel safe:

    • group calls.
    • Raw SQL (including any snippets in where calls).
    • String length validations in your models.
    • All uses of LIKE.

提交回复
热议问题