How manage multiple DB with MongoLab and Heroku? - heroku

I have a project hosted on Heroku using Mongolab for storing data (MongoDB).
I need to create 2 different DB instances: one for prod, one for dev.
Any idea how can I do and how I can tell my code which one to use?
Thanks

You should use a config variable (https://devcenter.heroku.com/articles/config-vars) to set the database url and then use the environment variable in your mongo initializer. Set one app to use the production url and a dev app to use the developer database url.

Related

Data does not persist when dyno is restarted in Heroku?

I have deployed Strapi headless CMS on Heroku free tier and tried to use it both with MongoDB and Postgres databases, whenever I restart the dyno e.g. during deployment - all the data created thus far is not persisted?
I tried rebuilding Strapi locally and I cannot reproduce the behaviour.
I am using free tier for hosting of Strapi as well as free tier of Heroku Postgres.
Most likely you created your project with --quickstart which is not Postgres, it is SQLite. Can you please check your config/environments/*/database.json files and ensure you have PostgreSQL setup?
All model configs are stored in files meaning you will not be able to create, edit, or delete new models, fields, components while using Heroku. All data (content) is saved to the database.
https://strapi.io/documentation/3.0.0-beta.x/guides/deployment.html#heroku

During the deployment Heroku automatically adds postgress Database

I have an application that I am trying to deploy to Heroku. I am facing an interesting situation, where a postgres database instance is getting created automatically without me provisioning one. In addition, env variable with DATABASE_URL is getting created automatically at the same time.
It is not a problem as for now, but later we would like to connect to existing DB. We are not able to modify config variable DATABASE_URL
Is there a way to disable automatic creation of Postgres DB

How to deploy a Flask REST API containing MySQLdb code on Heroku?

Most of the tutorials only show how to deploy a simple Flask hello world app on Heroku. But I have a Flask app which contains URLs with both GET and POST requests and they use MySQLdb library for fetching data from database.
How to set up such apps on Heroku? Currently I have a MySQL database on my local machine which is used by the code to fetch data. The Flask code contains many functions which are invoked by API calls. For example:
#app.route('/display_table', methods=['POST'])
def display_webstats():
db = MySQLdb.connect("localhost", "root", "root", "db_name")
cursor = db.cursor()
cursor.execute("select * from table_name")
ws = cursor.fetchall()
return jsonify(ws), 200
How to deploy such apps on Heroku?
Make sure your dependencies are listed in your requirements.txt or Pipfile and Pipfile.lock.
Select a MySQL addon and provision it, e.g.
heroku addons:create cleardb:ignite
Update your code to connect to the database provided by whatever environment variables the addon provides, e.g. CLEARDB_DATABASE_URL. You can use os.getenv() with a default argument to fall back in your development environment:
import os
database_url = os.getenv(
'CLEARDB_DATABASE_URL',
default='mysql://root:root#localhost/db_name', # For local development
)
It's probably also a good idea to centralize your database connection logic so it's not done in every controller. Something like Flask-SQLAlchemy might be helpful to simplify connecting and querying your database. It also provides an ORM if you want one of those.
Deploy.
Assuming you have them, run your migrations on Heroku via heroku run. If you're not using a migrations library I urge you to start. Flask-Migrate might be a good fit.
The alternative is manually creating and maintaining your schema across environments, and that's time-consuming, error prone, and frustrating.

How can I change the database name according to the database credentials provided by heroku during production?

Heroku provides its own database name and other credentials, but my local database name is different.How can I change the database name according to the database credentials provided by heroku during production?
Use a package like dotenv. dotenv and variants of it likely exist for whatever language you're using.
Basically, you want to use environment variables instead of hard coding values into your code. So, instead of writing something like this:
my_database_connect('my_username', 'abc123')
You'd write:
my_database_connect(process.env.DB_USERNAME, process.env.DB_PASSWORD)
Heroku will already have these environment variables set on the "config" tab of your app. Then for local development, you'll create a file called .env and have this text in it:
DB_USERNAME=my_username
DB_PASSWORD=abc123
Don't commit .env to your git repository – it should only live on your machine where you develop. Now your code will run locally as well as on Heroku, and connect to the proper database depending on the environment it's running in.
Here's an article that explains this more thoroughly for node.js, although this is basically the best practice for general development: https://medium.com/#rafaelvidaurre/managing-environment-variables-in-node-js-2cb45a55195f
First I created an application name on Heroku. Then I deployed my app to heroku by connecting to github.
Heroku provides the database credentials after we deploy our applications. Then I redeployed the app through github by changing the configuration in application.properties file as follows:
#localhost configuration
SPRING_DATASOURCE_DRIVER_CLASS_NAME=org.postgresql.Driver
SPRING_DATASOURCE_URL=jdbc:postgresql://localhost/transactions?useSSL=false
SPRING_DATASOURCE_USER=postgres
SPRING_DATASOURCE_PASSWORD=some_pass
#server database configuration
SPRING_DATASOURCE_DRIVER_CLASS_NAME=org.postgresql.Driver
SPRING_DATASOURCE_URL=jdbc:postgresql://ec2-23-23-247-222.compute-1.amazonaws.com/d6kk9c4s7onnu?useSSL=false
SPRING_DATASOURCE_USER=rimjvlxrdswwou
SPRING_DATASOURCE_PASSWORD=dd903753bc0adffb96ce541b1d55fb043472e32e28031ddc334175066aa42f69
Then you have to edit the config vars according to your application.properties files as shown in the figure below
config_var.png

How to query heroku database from salesforce (eg. via vf page)

can anyone help me on this? How can I achieve the following:
I need to set up a prototype integration with heroku from salesforce application. The need is to store a huge volume of data in a single heroku table and accessble from salesforce - eg: have a vf page in salesforce which queries the data in realtime
We're doing this. We use Heroku connect. We created a salesforce custom object and bidirectionally synched it using Heroku connect. You can then update the data on either side (in salesforce or in Heroku:PG).
But Heroku connect is expensive & priced on a # of synchs per month.
https://www.heroku.com/connect
Firstly, heroku is not a database. It is a PAAS.
My approach would have been :
- Create a heroku app(this is a link to create a node.js app) link
- After you create the app choose one of the datastore addons -> link if you want a SQL/Column oriented Database choose heroku postgres or ClearDB Mysql
- Secondly you actually need a webserver that exposes this database to you. I am thinking you would make this a webservice of some sort. You can build that in node.js. Here is something that will get you started or give you an idea - link

Resources