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

Exclude the default tenant table if it exists in the procedure #184

Open
wants to merge 2 commits into
base: development
Choose a base branch
from

Conversation

zakky21
Copy link

@zakky21 zakky21 commented Jan 7, 2022

We are using a postgreSQL procedure.
And we have a problem that when we clone a tenant, the schema name of the default tenant table is also changed.

My solution

Added pg_exclude_clone_tables parameter. (I'm not sure about the name. Change it if you need to.)
When the pg_exclude_clone_tables parameter is true, the tables specified in excluded_models are excluded from the replacements done by patch_search_path.

As a side effect, exclude tables specified in excluded_models from the clone target because the SQL output by pg_dump malfunctions if there is a create statement for the table.

Examples

Setting

apartment.rb

Apartment.configure do |config|
  config.excluded_models = ['Company']
  config.use_schemas = true
  config.use_sql = true
end

migration

def up
  ActiveRecord::Base.connection.execute <<-PROCEDURE
    CREATE OR REPLACE FUNCTION test_function() RETURNS INTEGER AS $function$
    DECLARE
      r1 INTEGER;
      r2 INTEGER;
    BEGIN
      SELECT COUNT(*) INTO r1 FROM public.companies;  -- we expected to stay public schema
      SELECT COUNT(*) INTO r2 FROM public.users;
      RETURN r1 + r2;
    END;
    $function$ LANGUAGE plpgsql;
  PROCEDURE
end

Then create 'test' schema

Actual

    CREATE OR REPLACE FUNCTION test_function() RETURNS INTEGER AS $function$
    DECLARE
      r1 INTEGER;
      r2 INTEGER;
    BEGIN
      SELECT COUNT(*) INTO r1 FROM test.companies;  -- we expected to stay public schema
      SELECT COUNT(*) INTO r2 FROM test.users;
      RETURN r1 + r2;
    END;
    $function$ LANGUAGE plpgsql;

Expect

    CREATE OR REPLACE FUNCTION test_function() RETURNS INTEGER AS $function$
    DECLARE
      r1 INTEGER;
      r2 INTEGER;
    BEGIN
      SELECT COUNT(*) INTO r1 FROM public.companies;  -- we expected to stay public schema
      SELECT COUNT(*) INTO r2 FROM test.users;
      RETURN r1 + r2;
    END;
    $function$ LANGUAGE plpgsql;

fix rubocop

fix rubocop (line length)
fix test code
@mnovelo
Copy link
Collaborator

mnovelo commented Mar 27, 2024

@zakky21 I'm a new maintainer on this gem. Do you still want this to be considered?

@zakky21
Copy link
Author

zakky21 commented Mar 28, 2024

@mnovelo
Thank you for your response.

Please consider this.
Because we are currently handled by a monkey patch, and I would like to see it supported on the gem side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants