Migrating record to non multi tenant database to multi tenant database

55 Views Asked by At

I'm using rails gem rails-on-service/apartment with a setup of a multi-table database. I have an existing database using our client and will transfer the record to the new database. with one different table which is the User table

the old user table has a username, password, email, and status_id.

the new user table has a username, password, email, status_id, and tenant_id.

before transferring I already create a tenant. and it will create a schema and database. then I will use pg_restore and indicate the new database(this will be the newly created tenant.) all are doing well unfortunately the User table has not copied the record.

1

There are 1 best solutions below

2
Hamza Khalid On

Not familiar with rails-on-service/apartment but worked on acts_as_tenant gem which from the looks of it works the same way, the problem may seem to be because your user table for the new database has a foreign key dependency to the tenant table.

So your users will belong to a specific tenant so when migrating either you should specify which tenant these users would belong to. In our case we used to migrate users tenant wise. So Something like if we had two Two tenants

Migrate Tenant 1 Users

Tenant.set 1 # or in your case it might be something like st(tenant_name:String) 
user = User.find_or_create_by(params)  
user.tenant_id ==> 1

This would automatically set tenant_id for any users created to the tenant we just switched to