Class: ActiveRecordPrettyKey::PrettyKeyTicket
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- ActiveRecordPrettyKey::PrettyKeyTicket
- Defined in:
- lib/active_record_pretty_key/pretty_key_ticket.rb
Class Method Summary collapse
-
.next_id ⇒ Object
Generate a new unique ID that works with PostgreSQL, MySQL, and SQLite.
- .next_sqid ⇒ Object
Class Method Details
.next_id ⇒ Object
Generate a new unique ID that works with PostgreSQL, MySQL, and SQLite
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/active_record_pretty_key/pretty_key_ticket.rb', line 14 def self.next_id transaction do # Detect database type adapter = connection.adapter_name.downcase if [ "postgresql", "postgis" ].include?(adapter) # PostgreSQL approach result = connection.execute( "INSERT INTO pretty_key_tickets (stub) VALUES ('a') ON CONFLICT (stub) DO UPDATE SET id = currval('pretty_key_tickets_id_seq') RETURNING id" ) result[0]["id"] elsif [ "mysql", "mysql2" ].include?(adapter) # MySQL approach connection.execute("REPLACE INTO pretty_key_tickets (stub) VALUES ('a');") connection.execute("SELECT LAST_INSERT_ID()").first[0] else # SQLite approach (and fallback for others) connection.execute("REPLACE INTO pretty_key_tickets (stub) VALUES ('a');") last.id end end end |
.next_sqid ⇒ Object
39 40 41 |
# File 'lib/active_record_pretty_key/pretty_key_ticket.rb', line 39 def self.next_sqid @sqids.encode([ self.next_id ]) end |