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

Associate Table Prefixes with DBs #991

Closed
Articdive opened this issue Dec 15, 2019 · 9 comments
Closed

Associate Table Prefixes with DBs #991

Articdive opened this issue Dec 15, 2019 · 9 comments

Comments

@Articdive
Copy link

Articdive commented Dec 15, 2019

Would be an amazing feature if we could give DBs custom Table prefixes (so 1 model has a different table prefix in different DBs e.g. in DB 1: HELLO_USERS and in DB 2: BYE_USERS), I'm currently working on a project and need to convert data from 1 DB to the other, but they have different table prefixes, I currently use a little reflection to make it somewhat possible but I could see this as a useful feature. Let me know if you would like some help with Impl..

@ipolevoy
Copy link
Member

Did you find a way to do this ?

@Articdive
Copy link
Author

Articdive commented Dec 16, 2019

@ipolevoy I managed to use reflection to make it work and loaded it all into memory once (using reflection to change the @ Table annotation for load_prefix) and then changed the model's DB to the save DB also using reflection to change the @ Table (save_prefix) Annotation. Pretty much

  • Change into load prefix
  • Load into Memory
  • Close ActiveJDBC (actively close all connections)
  • Change DBName and Table prefix for save
  • Reinit ActiveJDBC
    It's a very hacky way around it, I realized (the reason why I close this) is that models are only linked to 1 DB and this feature would therefore be kinda useless for my case. If models could be linked to multiple DBs (I haven't researched ACtiveJDBC enough to know) then it could become a very useful feature.

@ipolevoy
Copy link
Member

@Articdive would something like this work for you: https://javalite.io/database_connection_management#multiple-database-example ?

@Articdive
Copy link
Author

@ipolevoy unfortuantely that wouldn't work, I really need 1 Model (not 2 different ones) to be saved/loaded in 2 different DBs, I realized however that this does kind off defeat the purpose of ActiveJDBC.

@ipolevoy
Copy link
Member

ipolevoy commented Dec 17, 2019

@Articdive actually, you can do this like this:

public class Person extends Model{}

Base.open(driver, url1, user1, password1);
List<Person> people = Person..where(...);
Base.close(); 

Base.open(driver, url2, user2, password2);
for(Person p: people){
   p.set("first_name", "John").saveIt();
}
Base.close(); 

so, what happens here, you read data from one database then modify it and write to another. The model only cares that there is an available default connection.

Will this help?

@Articdive
Copy link
Author

Articdive commented Dec 19, 2019

This is pretty much what I do, this simplifies the process I use by using the Base db (instead of reinit and stuffs) and stuff so that already helps a ton. Now it would be amazing if we could have a setTablePrefix("") or something so that we can set the table prefix, at the moment I use reflection to change the @ Table annotation and change the referenced table. So this ticket could be reopened for this exact case (a table prefix for a db)!

@ipolevoy
Copy link
Member

@Articdive there is a way to fake it by using a sharding method, please see https://javalite.io/sharding.
I think between the example above and sharding, you can do what you need without any additional changes or reflection

@Articdive
Copy link
Author

okay this seems to have worked thank you @ipolevoy!

@ipolevoy
Copy link
Member

Awesome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants