Django Haystack with multiple databases - multiple-databases

Django recently added support for using multiple databases and "database routing". Does Haystack deal intelligently (or at all) with multiple databases?

I'm using a multi database configuration on production with django-haystack and whoosh and so far I didn't have any problems. Because haystack uses models as any application does every query passes through the router.

Related

How to dynamically use different existing database in django

i have around ten to twenty databases with same structure in mysql,
and how can i load those database dynamically in django?
(there will be a table storing those databases name, which may add/delete during the programme)
edit:
the dynamically way means that while running my website, create the orm and do somethings of another database by data input by users or which stored in the database
those database are:
1)with the same structure
2)only the name of database is different
3)not define in my setting.py when the web is started
Django has a built-in feature to support multiple databases check it out here https://docs.djangoproject.com/en/3.0/topics/db/multi-db/#multiple-databases

Connecting to external database the idiomatic way in RoR?

I am creating a RoR app, and need to connect to my company's MySQL database (which has nothing to do with this app in itself) to gather some data and report it through the RoR app. What is the idiomatic way to connect to this database? I'll just be running some fairly lite select queries off of it, however they often involve table joins. Should I just connect as through this were not a RoR app?
If your queries are mostly bound to single tables or utilize only a few joins, you can actually define models for them. It would seem that allowing ActiveRecord to operate on them may be the most idiomatic method to do it inside Rails.
First, define the company database in your database.yml
class ExternalDbTable
# Connect to the db
establish_connection :connx_from_database_yml
# Define table if this query is bound to a single table
set_table_name 'ext_db_table'
set_primary_key 'pk_column'
end
From there, you can define named_scopes as you would with a Rails model and basically enjoy all ActiveRecord's benefits. If you don't have to access more than a handful of tables on the external db, you can create models for each and define has_many/belongs_to relationships as you normally would in ActiveRecord. However if it is a large number of tables and you have the ability to create a view on the external database, you can create a model pointed to the view which performs the joins for you. Then define named_scopes against the view as necessary.

Django session handling

I am building a django application. Using SQLAlchemy as ORM for saving data like purchase order for the user . However want to use django ORM for the session middleware.
Is it a bad design decision to use both of these ORM components ?
If I should use only one ORM , which one I should use.
Thanks
J
If you're going with Django, you probably chose it because you feel it gives you more of what's needed for your project than the other Python web frameworks. If you use SQLAlchemy with Django, you're losing a lot of what Django gives you (e.g. tie-in with the forms library, the large collection of django apps, etc.)
Also, I wouldn't recommend using both. It's not something that would be impossible, but I just don't see the advantage to forcing yourself to go back and forth between two completely different APIs to be doing the same thing.
I've had no issues using both. I use Django's model for handling session data and user authentication, and SQLAlchemy for the database my application uses.
I find SQLAlchemy is easier to work with. However:
You can't easily manage security / user groups for your SQlAlchemy objects. (Use Django for those components)
I wouldn't recommend having SQLAlchemy and Django to access the same tables, it's better off with one ORM.
Otherwise - if your SQLAlchemy tables are separate from Django, and aren't required for security, form generation, or accessing Django tables directly, then it works great. I don't use Django's forms, and so most everything except for my session & user data is in SQLAlchemy.
If you had to use only one, use the Django ORM, because otherwise you lose out on a lot of features built into Django, and you might as well go with another framework like pylons.

Cucumber with Database_Cleaner for MySQL and MongoDB

I am building a Rails app that uses MySQL for some models and MongoDB for others (through the mongo_mapper gem).
We have started to build out cucumber (with capybara and webdriver) tests for the app and are coming across some trouble with IDs being referenced that don't exist. I believe I have tracked this down to old data in the MongoDB.
At this point, database_cleaner is doing its job with the MySQL records, but not the MongoDB ones.
There is a discussion at the cucumber-rails project about using MongoDB, but I believe it assumes that you are only using MongoDB, not both MongoDB and MySQL together.
Is there a way to have the database_cleaner clean both MySQL and MongoDB? Or is it only one or the other?
I found this article on how to drop all of the MongoDB content before running the tests, but I believe this will delete all data including the records I am using for local development...
Thanks.
Assuming you are doing something like this when you tell rails which Mongo DB to talk to:
MongoDatabase = "mongodb://localhost/yourdb_#{Rails.env}"
Then in your tests, do:
/spec/spec_helper.rb
MongoMapper.database.collections.select { |c| c.name != 'system.indexes' }.each(&:drop)
(above is for MongoMapper, but idea is the same for Mongoid -- just drop to the database level and drop all collections).
This will only drop data in your test database, not your dev db. Used in conjunction with the DB cleaner, you're good to go.

Multiple Rails app, single MySQL database

I intend to have multiple Rails apps each for site.com, api.site.com, admin.site.com. All apps will access the same tables from one single MySQL database. Apps and database runs in the same server.
Is there any settings in Rails, ActiveRecord or MySQL that I need to be concerned about for above access scenerio? Thanks
Running: Rails 2.3.5, MySQL 5.0, Nginx, Passenger, RubyEE
This configuration tends to be quite difficult to maintain.
In every app, you would need to keep schema.rb and models in sync in order to use the same database. It means lot of duplication.
This isn't probably a good idea. Instead, you might want to design the application to meet one of the following scenario:
one Rails application which handles site.com, api.site.com and admin.site.com (why do you need separate app?)
multiple Rails applications, but just one interact with the db. The others uses the main application API (quite complex)
different apps with different purposes (for instance, you might want to use Sinatra + Datamapper for api.site.com)
The first option is probably the best one in most cases.
I answered similar question here. You can do it and sometimes it is reasonable.