-2

I am creating a multi-tenant rails app for different clubs. I'm using the apartment gem so it creates different databases for each club which has more than 100 tables. now i want to take dump of one tenant and import it to other club tenant

2 Answers2

0

Apartment gem create different schema for different tenenats not databases. You can check difference here.

For your case you can copy record of one schema using below command:

pg_dump -U <username> -p <port> -n <source> <database name> >> <dump filename>

source is your schema name for source. It is your primary tenant which you want to copy.

target is you schema where you want copy all records.

Change your schema name in your new dump file by opening in any editor.

# edit the dump.file

CREATE SCHEMA <source>; => CREATE SCHEMA <target>;
ALTER SCHEMA <source> OWNER TO <username>; => ALTER SCHEMA <target> OWNER TO <username>; 

Now restore new dump file.

psql -U <username> -p <port> -d <database> -f <dump filename>

You can copy data from one tenant to another tenanat by creating a rake task and switching tenants.

Apartment::Tenant.switch!("one tenant")

# Retrive data that you want in a local object
# local_object = Model.all

Apartment::Tenant.switch!("second tenant")

# Save all data in second tenant from local_oject
# local_oject.each do |object|
#   object.save
# end
Dipak Gupta
  • 7,321
  • 1
  • 20
  • 32
0
using this way but not saving in new tenant    
Apartment::Tenant.switch("tenant1")
        members= Member.all
        Apartment::Tenant.switch("tenant2")
        members.each do |member|
          begin
            member.save
            puts "migrating member.id"
          rescue=>e
            puts e
          end
        end