3

I have two rails application base1 and base2.

base1 uses db1 and have multiple tenants inside this database, its using apartment gem.

base2 is single tenant application and has database db2 as primary, and also uses data from db1.

Now the problem is, base2 application established connection to db1 and i can get the data db1 data in public tenant in base2 application.
How to get data from different schemas of db1 in base2 application?

Zia Ul Rehman Mughal
  • 2,119
  • 24
  • 44
Abhishek Aravindan
  • 1,432
  • 6
  • 23
  • Do you frequently need to change schema in your `base2` app's models? Also, do db1 and db2 share any table names? I think i can help you here but i need more details. Also is there any writes being done on db1 from base 2 app? – Zia Ul Rehman Mughal Jun 21 '19 at 07:37
  • base 2 is a single tenant application which is used to get the data from base1 application which is a multitenant application. i dnt want to change schema of base2 application it have its own schema. – Abhishek Aravindan Jun 21 '19 at 07:59
  • No, i think you didn't understand my comment, let me rephrase. When you will access `db1` from `base2`, you want to tell it to look specific table in specific schema(not in public). So my question is, when accessing db1 from base2, do you need to access same table data from different schemas or just want data from one schema? My second question was, if you need to do any writes on db1 from base2 app. – Zia Ul Rehman Mughal Jun 21 '19 at 08:02
  • sorry.now i got your comment. i only need data from one schema at a time. and i also want to write on db1 from base2 app – Abhishek Aravindan Jun 21 '19 at 08:06
  • Ok, there is a table_name option which you can set to something specific in model like `self.table_name = 'schema_name.table_name'` but this will force use of same schema for all queries. Does this work for you or you want to adjust schema when querying everytime? – Zia Ul Rehman Mughal Jun 21 '19 at 08:11
  • See this as well: https://stackoverflow.com/a/8838881/4738391 – Zia Ul Rehman Mughal Jun 21 '19 at 08:14
  • this wont work for me.when the application become live its not posible to change the code each and every time. i need to change the schema from application its self – Abhishek Aravindan Jun 21 '19 at 08:15
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/195322/discussion-between-zia-ul-rehman-mughal-and-abhishek-aravindan). – Zia Ul Rehman Mughal Jun 21 '19 at 08:15

1 Answers1

6

As per discussions in comments.

def self.with_schema(schema_name)
    class_name = self.name + schema_name.camelize
    table_name = self.table_name
    if !Object.const_defined?(class_name)
      Object.const_set(
        class_name, Class.new(self) do
          self.table_name = "#{schema_name}.#{table_name}"
        end
      )
    end
    class_name.constantize
  end

Add this to your application record and you can do things like: Data.schema('schema_name').all

Zia Ul Rehman Mughal
  • 2,119
  • 24
  • 44
  • 1
    Yeah !!!! this worked,.... Thank you so much!!!!!! `Data.with_schema('schema_name').all like the method name – Abhishek Aravindan Jun 21 '19 at 09:34
  • this didnt work when i added this to the application record, but when i add to the specific model this worked – Abhishek Aravindan Jun 21 '19 at 09:41
  • ok.... i figured it out!... this will work when added to application record.In my case the model is inherited from second database model (since i was using multiple database) i should add to record which it inherits from... Thank you again !!! :) – Abhishek Aravindan Jun 21 '19 at 09:47
  • This was working for me when I added it in application record(id didn't even added it in individual models). There might be something different going on in your app. Of-course you have to adjust for your environment. @AbhishekAravindan – Zia Ul Rehman Mughal Jun 21 '19 at 09:47