Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
developie0610 opened this issue May 11, 2023 · 8 comments
Closed
Labels
enhancement New feature or request

Comments

@developie0610
Copy link
Contributor

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
Copy link
Contributor

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

@djezzzl
Copy link
Owner

djezzzl commented Jun 27, 2023

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
Copy link
Contributor Author

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
Copy link
Contributor Author

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
Copy link
Owner

djezzzl commented Jun 28, 2023

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
Copy link
Contributor Author

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

  • PostgresQL
  • Rails 7

Schema:

# db/schema.rb

 enable_extension "plpgsql"

  create_table "users", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
    t.string "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

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
Copy link
Owner

djezzzl commented Jun 29, 2023

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 djezzzl added the enhancement New feature or request label Jul 10, 2023
@djezzzl
Copy link
Owner

djezzzl commented Jul 22, 2023

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!

@djezzzl djezzzl closed this as completed Jul 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants