Code Monkey home page Code Monkey logo

pg_rls's Introduction

Contributors Forks Stargazers Issues LinkedIn Hireable Donate


PgRls

PostgreSQL Row Level Security
The Rails right way to do multitenancy


Explore the docs »

Report Bug · Request Feature · API Repo

Table of Contents

It's time we start doing multitenancy right! You can avoid creating a separate Postgres schema/databases for each customer or trying to ensure the WHERE clause of every single query includes the particular company. Just integrate PgRls seamlessly to your application.

This gem will integrate PostgreSQL RLS to help you develop a great multitenancy application.

Required Installation

Installing

Add this line to your application's Gemfile:

gem 'pg_rls'

And then execute:

$ bundle install

Or install it yourself with:

$ gem install pg_rls

Instructions

rails generate pg_rls:install company #=> where company eq tenant model name

You can change company to anything you'd like, for example, tenant This will generate the model and inject all the required code

For any new model that needs to be under rls, you can generate it by writing

rails generate pg_rls user #=> where user eq model name

and it will generate all the necesary information for you.

You can swtich to another tenant by using

PgRls::Tenant.switch :app #=> where app eq tenant name

Don't forget to update how you want PgRls to find your tenant, you can set multiple options by modifying api/config/initializers/pg_rls.rb search_methods

# app/config/database.yml
<% def db_username
    return PgRls.username unless ENV['AS_DB_ADMIN']

    Rails.application.credentials.dig(:database, :server_1, :username)
   end %>

...

development:
  <<: *default
  database: example_development
  username: <%= db_username %> # Apply this to production and all env including tests

...

Testing

If you are getting PG::InsufficientPrivilege: ERROR: permission denied you can override does permistion by running RAILS_ENV=test rake db:grant_usage

Many application uses some sort of database cleaner before running thair spec so on each test that we run we'll have an empty state. Usually, those gems clear our user configuration for the database. To solve this issue, we must implement the following:

# spec/rails_helper.rb

...
# some database cleaning strategy

config.before(:suite) do
  # Create the tenant which in this example is company and we are using FactoryBot
  FactoryBot.create(:company, subdomain: 'app')
  # In this default case our initializer is set to search by subdomain so will use it
  PgRls::Tenant.switch :app
end
...

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/dandush03/pg_rls. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the PgRls project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

Note

Currently we only support subdomain as a searcher but will soon integrate slug/domain and cookies support we recommed the use of ``

Show your support

Give a ⭐️ if you like this project!

If this project help you reduce time to develop, you can give me a cup of coffee :)

paypal

pg_rls's People

Contributors

atayl16 avatar dandush03 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

pg_rls's Issues

[Suggestion] Readme Improvements

Hello @Dandush03 !

It's really awesome to see an alumnus from Microverse develop their own gem!
I'd just like to give you some recommendations about this project if you don't mind:

  1. In your Readme, you forgot to delete the following line from the template you used:

TODO: Delete this and the text above, and describe your gem

  1. I would recommend you to change the introduction part to the description of what the gem exactly is, for example, from what I understood by looking into your code, I believe that it is something to the PG setup for RoR, so you could try something like this:

PgRls allows you to quickly set up your working and production environments by doing X, Y, and Z, making it easier for your project to scale

  1. If you believe that it is possible (keep in mind that sometimes it doesn't make sense to do so), you could come up with a more suggestive/descriptive name, just as for example the activestorage-validator gem. So this way, people might find out about your gem with a simple google search!

Migration and Rollback Issues with Incorrect User and Password

Description:

Encountering a PG::InsufficientPrivilege error when attempting to run migrations or rollbacks. The process incorrectly uses app_user and the gem's password instead of the credentials defined in the initializer.

Steps to Reproduce:

  • Check out a branch with a previous version of the PG RLS gem.
  • Run rails db:rollback to revert a migration.
  • Switch to a branch with the current version of the PG RLS gem.
  • Attempt to run rails db:migrate or rails db:rollback.
  • Observe the PG::Permissions error in the console.

Expected Behavior:

Migrations and rollbacks should use the correct admin user and the password defined in the Rails initializer.

Actual Behavior:

The process attempts to execute with app_user and the gem's default password, leading to permission errors.

Environment:

  • Ruby version: 3.2.2
  • Rails version: 7.0.8
  • Sidekiq version: 7.2.0
  • PG RLS gem version: v0.1.4

image

Retry Mechanism Failing in Sidekiq Jobs

Description:

Sidekiq jobs are not retrying as expected when they fail. Despite setting the retry option, failed jobs do not re-attempt execution.

Steps to Reproduce:

  • Create a sample Sidekiq job that intentionally raises an error.
  • Set sidekiq_options with retry: 3.
  • Trigger the job and observe its behavior after failure.
  • Check the Sidekiq console to confirm if the job is not retrying.

Expected Behavior:

The job should automatically retry three times after an initial failure.

Actual Behavior:

The job does not retry after failing.

Environment:

  • Ruby version: 3.2.2
  • Rails version: 7.0.8
  • Sidekiq version: 7.2.0
  • PG RLS gem version: v0.1.4

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.