djezzzl / database_consistency

The tool to avoid various issues due to inconsistencies and inefficiencies between a database schema and application models.
MIT License
1.05k stars 46 forks source link

Checker for implicit_order_column for table with uuid as primary key #197

Closed developie0610 closed 1 year ago

developie0610 commented 1 year ago

Hi @djezzzl , I'm working on a project that recently start utilize uuid as our primary key for new database's table. We find it's a common pattern to define implicit_order_column (supported Rails >=6) with such tables to maintain the records's order in queries.

We have reason to think that this approach would be a common practice compare to using sortable uuid algorithm, and given that we are relying heavily on database_consistency, having a checker would be a great resource.

toydestroyer commented 1 year ago

@djezzzl, any chance you were able to take a look at this?

djezzzl commented 1 year ago

Hi @developie0610,

Thank you for reporting this!

Hi @toydestroyer,

Thank you for pinging me; it seems I overlooked this.

Could you please help me to understand what the checker should check? That every model with UUID as a primary key have implicit_order_column defined?

developie0610 commented 1 year ago

Could you please help me to understand what the checker should check? That every model with UUID as a primary key have implicit_order_column defined?

Yes. That is what we are looking for @djezzzl and I don't see the need for more complications.

developie0610 commented 1 year ago

Let me know if this is a sensible checker, I am happy to contribute on the actual work since we are the primary user as far as I'm concerned.

djezzzl commented 1 year ago

Hey @developie0610,

Feel free to make a PR if you have time; however, I'm happy to help if you can't.

Could you give me a minimal code example (model/schema with meaningful code there), though, so I can repeat it in my tests and do a proper checker?

developie0610 commented 1 year ago

awesome, thanks @djezzzl. I'll contribute the PR. You can refer to this arbitrary setup to verify:

Model:

class User < ApplicationRecord
  self.implicit_order_column = 'created_at'
end

Migration:

class CreateUser < ActiveRecord::Migration[7.0]
  def change
    enable_extension 'pgcrypto'

    create_table :users, id: :uuid  do |t|
      t.string :name
      t.timestamps
    end
  end
end

-> Expected result from User.last: SELECT "users".* FROM "users" ORDER BY "users"."created_at" DESC, "users"."id" DESC

djezzzl commented 1 year ago

Thank you for sharing this!

I'll contribute to the PR.

Great then! Please feel free to ping me for any assistance; if you can't finish, please let me know, and I can do that.

djezzzl commented 1 year ago

Hi @developie0610,

Thank you for your contribution!

I just released this on 1.7.18. Please have a look.

BTW, could you please fill in the WIKI page?

https://github.com/djezzzl/database_consistency/wiki/implicitorderingchecker

P.S. Have a great weekend!