How to use TypeOrm in a database already in production? - typeorm

we have a platform already in production with a MySQL database, our goal is to create another platform consuming the same database...
Is it possible to use TypeOrm on a database already in production?

I don't understand what you means by using TypeOrm on a database already in production!
If you mean doing some queries to the database itself, so yes you can do that.
But in principle you can have 100 platforms linking to the same database, there is no problem.

Related

Changing database from PostgreSQL to MySQL

I am building an e-commerce application using spree in rails. The application uses PostgreSQL as database. I want it to be changed to MySQL. How do I achieve this?
The short answer is, you don't. I've been through three Spree migrations where we were either using legacy data, or switching databases and trust me; you want nothing to do with this.
If you don't need to take the old data with you, you should be fine just running the migrations on a fresh MySQL database. If you need the legacy data, God be with you...
Converting Spree database schema is nothing nice, and it's hard to just pull what you need out of the database since because almost every data model depends on a foreign key from another. There are dental procedures I would go through voluntarily before attempting what you are proposing. This also begs the question; why do you need to go to MySQL from Postgres?

Rails: create system needed records if they don't exist

I'm working on an app that must integrate with an existing database and other legacy apps. Throughout the legacy apps, implicit assumptions are made that certain records in certain tables exist.
For instance, legacy code assumes that a record with name 'Valid' exists in the table 'statuses', and a record with username 'System' exists in the users table.
As I'm digging into the legacy code, I'm writing down these implicit assumptions to make them explicit, so that I'm able to:
setup a small development db instead of using a dump of the very very huge production db;
setup a staging db (again, without the zillions of records from production);
run tests in a clean, predictable db.
be idempotent and DO NOT mess the existing db.
So the question is: what would you use to create system records if they don't exist?
Would you opt for a rails migration, seeds, an initializer or what?
I think seeds would be the way to go here.

MongoDB with PostgreSQL in One Rails App

Can I use MongoDB and a PostgreSQL in one rails app? Specifically I will eventually want to use something like MongoHQ. So far I have failed to get this working in experimentation. And it concerns me that the MongoDB documentation specifically says I have to disable ActiveRecord. Any advice would be appreciated.
You don't need to disable ActiveRecord to use MongoDB. Check out Mongoid and just add the gem plus any models along side any of your existing ActiveRecord models. You should note that MongoHQ is just a hosting service for MongoDB and can be used alongside any Object Document Mapper (ODM).
For further details check http://mongoid.org/en/mongoid/docs/installation.html. Just skip the optional 'Getting Rid of Active Record' step.
On a recent client site I worked with a production system that merged MySQL and MongoDB data with a single Java app. To be honest, it was a nightmare. To join data between the two databases required complex Java data structures and lots of code, which is actually databases do best.
One use-case for a two database system is to have the pure transactional data in the SQL database, and the aggregate the data into MongoDB for reporting etc. In fact this had been the original plan at the client, but along the way the databases became interrelated for transactional data.
The system has become so difficult to maintain that is is planned to be scrapped and replaced with a MongoDB-only solution (using Meteor.js).
Postgres has excellent support for JSON documents via it's jsonb datatype, and it is fully supported under Rails 4.2, out of the box. I have also worked with this and I find it a breeze, and I would recommend this approach.
This allows an easy mix of SQL and NoSQL transactions, eg
select id, blast_results::json#>'{"BlastOutput2","report","results","search","hits"}'
from blast_caches
where id in
(select primer_left_blast_cache_id
from primer3_output_pairs where id in (185423,185422,185421,185420,185419) )
It doesn't offer the full MongoDB data manipulation features, but probably is enough for most needs.
Some useful links here:
http://nandovieira.com/using-postgresql-and-jsonb-with-ruby-on-rails
https://dockyard.com/blog/2014/05/27/avoid-rails-when-generating-json-responses-with-postgresql
There are also reports that it can outperform MongoDB on json:
http://www.slideshare.net/EnterpriseDB/the-nosql-way-in-postgres
Another option would be to move your Rails app entirely to MongoDB, and Rails has very good support for MongoDB.
I would not recommend running two databases, based on personal observations on how it can go bad.

Moving from SQLite3 to Mongo on Heroku?

I'm currently using SQLite3 with a simple post and image sharing app, similar to the Rails 3 Hartl tutorial (in terms of db structure). But I'd like to move to Mongo for future scalability/learning.
I'm also hosted on Heroku, and am using a 15 GB shared db. I attempted to install MongoHQ and MongoMapper (as per Heroku's instructions) for the transition and this part according to Heroku's support is set up correctly. However, when I turn off the shared db, the app stops working, rather than running off of Mongo.
I'm not sure what do do next, do I have to rewrite my code in mongo or does mongo mapper solve all that? Do I lose my data if I change, if so, how do I copy?
Could any of you please point me to some resources or help me out? Thank you very much!!
MongoDB is not a drop in replacement for a SQL database. There are a couple of things you need to adapt:
The models' code are to be updated to use MongoDB. I can suggest using Mongoid, an ODM, as it will ease your learning path. Mongoid implements Active Record.
The current data saved in your SQL database needs to be migrated - and this is not automatic – to MongoDB schemas. MongoDB do not support migrations as you are used to in SQL world. You will need to write your own scripts for that.
I suggest you write a simple app from scratch using your MongoDB ODM of choice – MongoMapper or Mongoid – so that you get familiar with the basis of MongoDB before attempting to make a migration.

Ruby on Rails: is it okay to use old school database access without migrations?

I'm switching to RoR from ASP.NET MVC. Yeah, migrations are cool, but I do not need to use different databases in my web applications. Postgresql will do just fine.
So is it okay if I use PGAdmin to create and administer my databases and schema and avoid all these fancy migrate, rake etc?
Update
Thanks everyone! Now I better understand what migrations are, and why I should use them.
I don't think that's what migration means.
Migrations in rails (and in other frameworks) is a method by which you can use to update your database schema when there are multiple versions of the same database running
For example, you may have two databases, one running on your production server, and another running locally for development. After a few days of coding, your local development database may looks a bit different. With migrations, you can simply push your code to the production server and then run the migrations to automatically update your production database so it is up-to-date with the one you use locally for development.
So, to answer your question, Yes it is OK but you might not get a few of the migrations niceties when the time comes that you'll have to maintain multiple versions of your database.
Have to agree with charkit but one (rather two) important note why you should use migrations: Migrations don't make up the model definitions. They are stored seperately in a file schema.rb. This defines the rows and tables of your database. When looking into the file, you find these lines:
This file is auto-generated from the current state of the database. Instead of editing this file, please use the migrations feature of Active Record to incrementally modify your database, and then regenerate this schema definition.
The second reason is for testing: you can easily set up a test database to run all your tests against without the need to touch the "real" database. I know when developing, this is not a big problem but this will get more important after some time.
So, yes, it is possible to use PGAdmin to create all your database related stuff but you should not forget to always keep the schema file up to date and come up with a solution for testing.
With migrations you're able to develop your database schema in Ruby and this is usually database indpendent.
In short, spend the 20 minutes or so to really get migrations and the value they add. Then determine whether or not you want to ditch them. Strangely for me I learned Rails before I started my first MVC project; one of the things I missed most was migrations.
From a technical standpoint you should be fine without them.

Resources