Prime user table for the first user

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Prime user table for the first user

Ralph Shnelvar
Let's say I expect to have a billion users in my Postgres users table.

I want to make sure the first user gets to set it's role attribute to enum :admin as the default when the record is created and all other users are enum  :user.

Of course a billion users is ridiculous.  I'm just trying to understand what the, uh, best way to see if a Postgres table is empty without actually having to load it.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/16813798-742a-4aca-ab13-12742d2e6eaa%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Prime user table for the first user

Hassan Schroeder-2
On Tue, Jul 11, 2017 at 6:33 AM, Ralph Shnelvar <[hidden email]> wrote:

> I want to make sure the first user gets to set it's role attribute to enum
> :admin as the default when the record is created and all other users are
> enum  :user.
>
> Of course a billion users is ridiculous.  I'm just trying to understand what
> the, uh, best way to see if a Postgres table is empty without actually
> having to load it.

Assuming the table is "users" and User is an ActiveRecord model,
  (User.count == 0)
should work.

However, doing that for every subsequent user creation seems like
a bit of a waste. I would just use the db/seeds.rb file to create that
first admin user and be done with it :-)

FWIW,
--
Hassan Schroeder ------------------------ [hidden email]
twitter: @hassan
Consulting Availability : Silicon Valley or remote

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CACmC4yArZbpBoLiCkeFkrbHjSA8ON%3D%3DX7pwqgnTRZ%3Dsz69gZMA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Prime user table for the first user

Matt Jones-15
In reply to this post by Ralph Shnelvar


On Tuesday, 11 July 2017 09:33:05 UTC-4, Ralph Shnelvar wrote:
Let's say I expect to have a billion users in my Postgres users table.

I want to make sure the first user gets to set it's role attribute to enum :admin as the default when the record is created and all other users are enum  :user.

Of course a billion users is ridiculous.  I'm just trying to understand what the, uh, best way to see if a Postgres table is empty without actually having to load it.

I suspect `User.exists?` is going to be the lightest possible option. `User.count == 0` will also work, but I know there are some DBs where that operation can be expensive on large tables.

This is still going to do a query every time it checks, so if that's too much load you could cache the result:

class User < ActiveRecord::Base
  def self.any_users?
    @any_users ||= exists?
  end
end

Then your role-defaulting code can check `self.class.any_users?`, which will only run one query (per server) that returns true.

NOTE NOTE NOTE: the above is not 100% thread-safe. It assumes that there is exactly one user sending requests trying to be the first. If you're worried about somebody racing to sign up for that first account, you'll want to come up with a more secure approach.

--Matt Jones

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/34b5bba4-3914-4106-9f05-0e61053250c8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...