Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Rails Pacific - Multitenancy with Rails

Be66f1ccc28a6a3dfb248454b8513b17?s=47 Ryan Bigg
October 25, 2014

Rails Pacific - Multitenancy with Rails

Covers the lessons learned about Multitenancy with Rails in the process of writing my book.

Be66f1ccc28a6a3dfb248454b8513b17?s=128

Ryan Bigg

October 25, 2014
Tweet

Transcript

  1. lifx.co

  2. #21 Not #21

  3. Multitenancy with Rails Ryan Bigg

  4. None
  5. None
  6. Multitenancy?

  7. None
  8. None
  9. None
  10. Organization Team Repos

  11. User Repos

  12. None
  13. DOGLIFEBETA! Garçon Fluffy Brutus “Like Facebook, but for dogs”

  14. Multitenancy with Rails?

  15. None
  16. Is it… ✔ well maintained ✔ well documented ✔ clean

    code
  17. acts_as_tenant ✔ well documented

  18. acts_as_tenant ✔ well maintained ✔ 140 commits, 19 contributors, low

    issue count ✔ CI on Travis
  19. acts_as_tenant ✔ clean code

  20. acts_as_tenant

  21. RequestStore.store

  22. None
  23. Thread.current Thread.current

  24. apartment ✔ well documented ✔ well maintained ✔ clean code

  25. “If you are using PostgreSQL, Apartment by default will set

    up a new schema and migrate into there.”
  26. “If you are using PostgreSQL, Apartment by default will set

    up a new schema and migrate into there.”
  27. None
  28. “A database contains one or more named schemas, which in

    turn contain tables.” “There are several reasons why one might want to use schemas: * To allow many users to use one database without interfering with each other.”
  29. “To allow many users to use one database without interfering

    with each other.”
  30. ryanbigg=# SHOW search_path; search_path ---------------- "$user",public (1 row) "$user",public

  31. fluffy.doglife.com garcon.doglife.com DOGLIFE BETA! brutus.doglife.com 1 2 3

  32. (1.4ms) CREATE SCHEMA "garcon" <more tables go here>

  33. None
  34. None
  35. None
  36. DEPLOY IT!

  37. Heroku Postgres

  38. Backup time 24 hours

  39. PostgreSQL is great

  40. PostgreSQL with too many schemas

  41. Part 2: Schema-less Multitenancy

  42. Apartment

  43. None
  44. TEST ALL THE TIME

  45. TEST ALL THE TIME?

  46. TEST ALL THE TIME

  47. Are you testing right now? No?

  48. TEST ALL THE TIME OR ELSE

  49. A codebase without tests

  50. fluffy.doglife.com Fluffy’s Posts current_account = Fluffy @posts = current_account.posts fluffy.doglife.com/posts

    Feature #1: Viewing Posts
  51. A guest on Account A’s subdomain should only see Account

    A’s posts Expected to not see “<Account B’s post>” Feature #1: Viewing Posts
  52. Feature #1: Viewing Posts

  53. Feature #1: Viewing Posts

  54. Feature #1: Viewing Posts

  55. A guest on Account A’s subdomain should only see Account

    A’s posts Feature #1: Viewing Posts
  56. fluffy.doglife.com Fluffy’s Posts current_account = Fluffy @posts = current_account.posts fluffy.doglife.com/posts

    Feature #1: Viewing Posts
  57. fluffy.doglife.com Fluffy’s Albums current_account = Fluffy @albums = current_account.albums fluffy.doglife.com/albums

    Fluffy’s First Birthday Pictures @pictures = @album.pictures fluffy.doglife.com/albums/1/pictures Feature #2: Viewing Pictures
  58. A guest on Account A’s subdomain should only see Account

    A’s albums Expected to not see “<Account B’s album>” Feature #2: Viewing Pictures
  59. Feature #2: Viewing Pictures

  60. Feature #2: Viewing Pictures

  61. A guest on Account A’s subdomain should only see Account

    A’s albums Feature #2: Viewing Pictures
  62. A guest on Account A’s subdomain viewing an album shows

    pictures from that album Expected to not see “<Account B’s picture>” Feature #2: Viewing Pictures
  63. Feature #2: Viewing Pictures

  64. Feature #2: Viewing Pictures

  65. A guest on Account A’s subdomain viewing an album shows

    pictures from that album Feature #2: Viewing Pictures
  66. fluffy.doglife.com Fluffy’s Albums current_account = Fluffy @albums = current_account.albums fluffy.doglife.com/albums

    Fluffy’s First Birthday Pictures @pictures = @album.pictures fluffy.doglife.com/albums/1/pictures Feature #2: Viewing Pictures
  67. Feature #2: Viewing Pictures

  68. REGRESSION TEST ALL THE TIME

  69. Feature #2: Viewing Pictures

  70. None
  71. Part 3: Subscriptions

  72. None
  73. doglife.com/sign_up plan = Basic ($9.99) Feature #3: User subscribes to

    a plan ???
  74. “Writing code to manage subscriptions is easy.” - nobody, ever

  75. Your subscriptions code

  76. PayPal

  77. None
  78. PayPal

  79. None
  80. “Stripe makes recurring and subscription-based billing easy.”

  81. None
  82. None
  83. None
  84. :CreateRecurringPaymentsProfileRequestDetails PayPal

  85. None
  86. “My company makes your life easy.”

  87. doglife.com/sign_up plan = Basic ($9.99) Feature #3: User subscribes to

    a plan
  88. None
  89. me@ryanbigg.com me@ryanbigg.com ⁉ me@ryanbigg.com

  90. leanpub.com/multi-tenancy-rails/c/railspacific $10 off