rails-on-services / apartment

Database multi-tenancy for Rack (and Rails) applications
388 stars 159 forks source link

ActiveRecord::StatementInvalid: PG::DuplicateSchema: ERROR: schema "insales22810" already exists #284

Closed stap780 closed 2 months ago

stap780 commented 2 months ago

Hello,

i try create tenant and have error (look attachment). schema rb — rails_projects 2024-09-12 15-21-05

Loading development environment (Rails 7.1.4) User Pluck (1.2ms) SELECT "public"."users"."subdomain" FROM "public"."users" Available Tenants: [nil, "teletri", "test2", "ketago", "support_ketago_com"] Use st 'tenant' to switch tenants & tenant_list to see list [1] [development][public] pry(main)> Apartment::Tenant.create('test1234') (12.5ms) BEGIN; CREATE SCHEMA "test1234"; COMMIT; (3.4ms) CREATE SCHEMA "insales22810" ActiveRecord::StatementInvalid: PG::DuplicateSchema: ERROR: schema "insales22810" already exists

from /Users/administrator/.rvm/gems/ruby-3.2.0/gems/activerecord-7.1.4/lib/active_record/connection_adapters/postgresql/database_statements.rb:55:in `exec' Caused by PG::DuplicateSchema: ERROR: schema "insales22810" already exists

from /Users/administrator/.rvm/gems/ruby-3.2.0/gems/activerecord-7.1.4/lib/active_record/connection_adapters/postgresql/database_statements.rb:55:in `exec' [2] [development][public] pry(main)> exit

stap780 commented 2 months ago

as i understand the problem is that file schema.rb have records - create_schema "insales22810" and another

I don't understand - my file schema.rb need to have information about create_schema ? or something wrong with setup?

stap780 commented 2 months ago

and i use - bundle exec rake db:schema:dump - to update db/schema.rb

mnovelo commented 2 months ago

@stap780 the create schema lines in the schema.rb were added by Rails, not by Apartment. We have used a similar workaround to this to have this not occur https://github.com/rails-on-services/apartment/pull/276

I'm looking to include this workaround in the next release of the Apartment gem

stap780 commented 2 months ago

@mnovelo I right understand that better is to wait while you upgrade gem?

gertjanjansen commented 1 month ago

@mnovelo thanks for adding this fix. Would be great if you could include this in the next release.