Insert records to Heroku database - ruby-on-rails

How can I insert new records to Heroku database. I have successfully uploaded my application and database structure. Now I need to insert some records so it could work. I tried to use seeds.rb but without any success. db:push and db:seed don't give any success. The database is always empty. Is there any heroku tool available which I could use to see database structure and insert data?
Thank you.

You have a few options. You can run heroku run rake db:seed.
Or assuming Postgres, try
heroku pg:psql
And run your SQL commands from there. See here for more.
I was also able to connect my IDE to my databases on Heroku. I am using RubyMine, so it was a matter of using the right JDBC URL to make that happen.

You can use below command for insert record to Heroku server
heroku run rails c
Example:-
suppose you have one table is employee and field are first_name.
Now open a command line and call this command
heroku run rails c
After call insert command
Employee.create(first_name: "admin")

Related

Every time I take a database dump from production server and try to import and run on local, the migration is failing

Recently I implemented friendy_id on my rails application. So I generate new column called slug in my existing ActiveRecord model called Category. Everything is working fine. Both my local code and production have same tables and columns. The same code is running on production.
Every now and then I take the mysql database dump from the production server and try to import it to the source code on my local machine. Before I run rake db:migrate I always do rake db:drop and rake db:create.
So After importing the dump I do drop and create and then run rake db:migrate. It fails. Saying Duplicate column slug from categories
So what I've been doing so far is, every time I import dump from production, I am supposed to drop the slug column by doing
ALTER TABLE categories DROP COLUMN slug;
Then When I run rake db:migrate. It runs the last migrations which is created slug for categories. And then the migration ends successfully.
How can I overcome this issue ? I am forced to this every time I want to import the production db to local.
Sounds to me like your schema_migrations tables are no synced between production and development. So rails tries to migrate the last migration (adding slug) which is already added in the database, hence the error.

How do you access your heroku database (specifically postgres)?

When I am developing locally, I'll just run a
rails c
and I can fire of queries at the console.
However, how can I query and access my postgres database that is up on heroku?
You can run heroku run rails c --app <appname>. I will also add, if you want to view your database, you can make use of PG Commander https://eggerapps.at/pgcommander/ which actually automatically parses all the credentials out for you.
If you want to do this, run heroku config --app <appname> and then copy the DATABASE_URL. After you've done this, create a new favourite in your PG Commander and you'll see that it has already automatically filled in all the credentials based on what was in your clipboard.

Finding Rails DB In Sqlite3

So I created a db with 'sqlite development'. I then ran 'rake db:setup' and it seemed to execute without error. However when I launch the sqlite console, I can't seem to find any of the tables within the development database. Maybe I'm not accessing the database correctly? Any ideas?
Have you done rake db:create? I using that command to create the databases, not manually add the databases.
copied from comment section

What is the best practice for adding records to a production DB once deployed to a VPS?

I just deployed my rails app to a Linode VPS, and was wondering what would be the best way of adding records to the DB.
I have tables such as Categories, which I'd like to populate.
I thought of the Taps gem, using a csv, or an sql dump file.
I'd like to know if there are any tools out there for this?
Thanks
For this puporse there are the so called seed file which is default in:
db/seeds.rb
You can add entries here ( there is an example in the seed file ), which you can generate after deployment with a rake task:
rake db:seed
You probably are using bundler as well, so use:
bundle exec rake db:seed
In case of large number of seeds you can always create multiple files, see this blogpost about handling large seed files.
However, if you are in a state, where the already existing data in the app is crucial and you are changing servers or database drivers you wanna take a look on yaml_db gem which gives a nice method to abstract the existing data away from your actual db driver and export it into a .yaml file which you can import later back e.g.: after deploying on a new server.
See Railscast - #179 about seeding.
The rails way would be to use seed data in db/seeds.rb and then populate it by using rake db:seed.
You could also use a sql dump file and restore by issueing mysql -u <user> -p <database_name> < <mysql_dump_file>
The Easy Reference Data gem is similar to db:seed, but will update records if entries already exist. It also has easy integration with Capistrano.
Full disclosure: The company I work for developed the gem.

How do I check the records of my heroku database?

I've just deployed my application to heroku and pointed my custom domain to the heroku servers. How can I check the records in my heroku database?
You can use heroku run rails console and look at your records with Model.all or any other method.
If you want to backup the database look at heroku PG backups, you then can import your database on your local machine and look at it there. Depending on your db adapter you could use sqlite browser for sqlite3 or phpmyadmin for MySQL.
I found a similar question like this and here is what #Chowlett says:
"You could run heroku pg:psql to fire up a Postgres console, then issue \d to see all tables, and \d tablename to see details for a particular table."
You can also type select * from tablename; to view the table contents.
How to view current database schema for Heroku app in Terminal?
heroku db:pull to pull your production DB locally to take a peek in it.
I'll give the method for connecting via a GUI tool
Run the following command to get the database credentials from Heroku:
heroku pg:credentials DATABASE_URL
Then you can use a GUI tool like PG Commander or PGAdmin to connect to the db
Heroku now has an add-on named PostgreSQL Studio (currently free & in beta) that would let you access your database from within the browser, without having to use CLI, much like PHP MyAdmin.
To attach this add-on to your application,
heroku addons:create pgstudio
Then go to the list of add-ons on Heroku, select PostgreSQL Studio, authorize it, select the database to connect with from the dropdown list of all databases and it will take you to the web-based interface to handle your selected database.
You may refer to this official article on Heroku:
https://devcenter.heroku.com/articles/pgstudio
The easy answer is:
heroku pg:info
You can also download a client side Postgres, like Postico, and using the information provided in that URL to enter password and database name etc, then you can create locally, just like phpMyAdmin.
I use the admin_data gem, works well in Heroku.
You can use heroku dataclips that allows to run queries online. Here you can find documentation https://devcenter.heroku.com/articles/dataclips.
Connect to the database using Sequel Pro. You can find your ClearDB url using heroku config command. The structure for connecting is as follows:
CLEARDB_DATABASE_URL => mysql://[username]:[password]#[host]/[database name]?reconnect=true

Resources