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

Error Dropping Tenant #46

Closed
bmahnke opened this issue May 6, 2020 · 3 comments
Closed

Error Dropping Tenant #46

bmahnke opened this issue May 6, 2020 · 3 comments

Comments

@bmahnke
Copy link

bmahnke commented May 6, 2020

Steps to reproduce

  1. Postgres Extension Migration:
    execute 'CREATE EXTENSION pgcrypto WITH SCHEMA pg_catalog;'
    enable_extension "uuid-ossp"
  1. Account Model(.rb):
Account(id: uuid, name: string, account_number: string, subdomain: string, created_at: datetime, updated_at: datetime)
class Account < ApplicationRecord
  after_create :create_tenant
  private
    def create_tenant
      Apartment::Tenant.create(subdomain)
    end
end
  1. Create database using ENV configuration (not database.yml (ex: .env.development)
DATABASE_URL=postgres://##:password@localhost:5432/###
  1. Create db/demo.rb file with content:
schemas = Account.all.pluck(:subdomain)
schemas.each do |schema|
  Apartment::Tenant.drop(schema)
end
Account.delete_all
load "db/seeds.rb"
  1. < Create models to go below account (ex: Users) >
  2. Create database: rails db:drop db:create db:migrate
  3. Seed Data: rails r db/demo.rb
  4. Reset Database: rails db:drop db:create db:migrate

Expected behavior

I expect the error segment to not appear -- not sure if it's necessarily a bad thing as things appear to be correct in the end and I can seed the database after this with the same Account information and the schemas are created.

Appears to be an issue handling UUID's on the tenants?

Actual behavior

Error (after step 7):

Dropping ["be6c9a5c-66e7-4bc7-8db1-e98b34d7b40b", "First", "73a67d3cf158e61a0b1a", "first", Wed, 06 May 2020 18:20:55 UTC +00:00, Wed, 06 May 2020 18:20:55 UTC +00:00] tenant
Error while dropping tenant ["be6c9a5c-66e7-4bc7-8db1-e98b34d7b40b", "First", "73a67d3cf158e61a0b1a", "first", Wed, 06 May 2020 18:20:55 UTC +00:00, Wed, 06 May 2020 18:20:55 UTC +00:00]: PG::SyntaxError: ERROR:  syntax error at or near "be6c9a5c"
LINE 1: DROP SCHEMA "["be6c9a5c-66e7-4bc7-8db1-e98b34d7b40b", "First...
                       ^
Dropping ["073f33e6-8a21-4e71-9f3c-479ce71f224d", "Second", "019772af66a637c83ba1", "sec", Wed, 06 May 2020 18:20:59 UTC +00:00, Wed, 06 May 2020 18:20:59 UTC +00:00] tenant
Error while dropping tenant ["073f33e6-8a21-4e71-9f3c-479ce71f224d", "Second", "019772af66a637c83ba1", "sec", Wed, 06 May 2020 18:20:59 UTC +00:00, Wed, 06 May 2020 18:20:59 UTC +00:00]: PG::SyntaxError: ERROR:  syntax error at or near "073"
LINE 1: DROP SCHEMA "["073f33e6-8a21-4e71-9f3c-479ce71f224d", "Second...

                       ^
Dropped database 'dev'
Created database 'dev'
... 
<rebuilds database>

System configuration

  • Database: (Tell us what database and its version you use.)
    pg-0.21.0

  • Apartment version:
    ros-apartment-2.5.0

  • Apartment config (in config/initializers/apartment.rb or so):

require 'apartment/elevators/subdomain'
Apartment.configure do |config|
  config.excluded_models = %w{ Account }
  config.tenant_names = lambda { Account.pluck(&:subdomain) }
  config.use_schemas = true 
  config.persistent_schemas = ['shared_extensions']
  config.pg_excluded_names = ["uuid_generate_v4"]
end

Rails.application.config.middleware.use Apartment::Elevators::Subdomain
Apartment::Elevators::Subdomain.excluded_subdomains = ['www', 'admin']
  • Rails (or ActiveRecord) version:
    6.0.1

  • Ruby version:
    2.6.3

@rpbaltazar
Copy link
Contributor

Thank you for the ticket. Will take a look at it tomorrow to try to reproduce and understand what is causing it.

@bmahnke
Copy link
Author

bmahnke commented May 12, 2020

Seems to be this line in my apartment.rb file. The & is the culprit (in my local testing). This issue can probably be closed.

config.tenant_names = lambda { Account.pluck(&:subdomain) }

@rpbaltazar
Copy link
Contributor

Thank you for the feedback. Your conclusion appears to be correct. Closing this for now

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

No branches or pull requests

2 participants