igorkasyanchuk / rails_db

Rails Database Viewer and SQL Query Runner
https://www.railsjazz.com/
MIT License
1.46k stars 111 forks source link

RailsDB don't work with UUID primary keys #57

Closed Spierki closed 7 years ago

Spierki commented 7 years ago

Given this table definition :

class CreateShops < ActiveRecord::Migration
  def change
    create_table :shops, **id: :uuid** do |t|
      t.string :name
      t.string :base_url
      t.string :api_key
      t.string :picture

      t.timestamps null: false
    end
  end
end

RailsDB raises this exception when I try to delete a record :

ActiveRecord::StatementInvalid (PG::SyntaxError: ERROR:  syntax error at or near "dcd1e2"
LINE 1: DELETE FROM shops WHERE id = 62dcd1e2-ae77-4389-9bda-f50b07e...
                                       ^
: DELETE FROM shops WHERE id = 62dcd1e2-ae77-4389-9bda-f50b07e75f36;):
  activerecord (4.2.7) lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `async_exec'
  activerecord (4.2.7) lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `block in execute'
  activerecord (4.2.7) lib/active_record/connection_adapters/abstract_adapter.rb:484:in `block in log'
  activesupport (4.2.7) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  activerecord (4.2.7) lib/active_record/connection_adapters/abstract_adapter.rb:478:in `log'
  activerecord (4.2.7) lib/active_record/connection_adapters/postgresql/database_statements.rb:154:in `execute'
...

It looks like RailsDB do not support UUID primary keys.

Spierki commented 7 years ago

I can suggest this simple working fix because there is no easy way to run parametrized queries through ActiveRecord::Base.connection:

module RailsDb
  module Adapters

    class BaseAdapter
      extend ::RailsDb::Connection

     # ...

      def self.delete(table_name, pk_name, pk_id)
        case pk_id
          when Fixnum, Bignum then
            execute("DELETE FROM #{table_name} WHERE #{pk_name} = #{pk_id}")
          else
            execute("DELETE FROM #{table_name} WHERE #{pk_name} = '#{pk_id}'")
        end
      end
igorkasyanchuk commented 7 years ago

Yes, looks good. Could you make a PR with this fix?

Spierki commented 7 years ago

Done