unable to migrate database on production while running rake task - sql

i am developing a small app which will
download a csv file(from a give url) everyday and inject the data(in csv file) to database and load the same data in the webview. its working perfect in my local system.but when i deployed to heroku database injection from csv file is not working.
here is my code.
downloader.rake file
namespace :downloader do
desc "download a file"
task:downloading => :environment do
Rails.logger.info("message from task")
Download.destroy_all
ActiveRecord::Base.connection.execute("DELETE from sqlite_sequence where name = 'downloads'")
#**********some other code ************
end
end
schedule.rb file
set :environment, 'production'
every 1.minutes do
rake "downloader:downloading"
end
when i run it in production it shows in log($tail -f log/production.log)
D, [2015-07-21T12:17:02.910529 #11740] DEBUG -- : Download Load (0.2ms) SELECT "downloads".* FROM "downloads"
E, [2015-07-21T12:17:02.910635 #11740] ERROR -- : SQLite3::SQLException: no such table: downloads: SELECT "downloads".* FROM "downloads"

Looks like your database is not ready on Heroku.
Please veryify to have installed a mysql database for your application and then run heroku run db:setup to build the schema and seed it.

Related

Laravel3 migration on production server error: SQLSTATE[HY000] [2002] No such file or directory

I tried my database migration in fortrabbit.com through SSH, but I got an error:
SQLSTATE[HY000] [2002] No such file or directory
How to specify database name, password of production database?
How to do Laravel3 migration via SSH access in production server. I have used pagodabox it was working fine but very costly, so I switched to fortrabbit.com.
I tried this http://forums.laravel.io/viewtopic.php?id=6177, but no solution.
We surely will need more info about your problem.
To do a migration on Laravel using ssh, you basically have to go to your app folder and execute php artisan migrate. But I think you know that, right?
To specify your database infor for production,
1.create a ditectory production inside your app/config
2.copy your current app/config/database.php to it
3.edit your new app/config/production/database.php
4.on your app/start/global.php, configure your environment:
$env = $app->detectEnvironment(array(
'development' => array('localhost'),
'production' => array('example.com')
));
But to clarify it even better, you can watch this fresh video from Jeffrey Way on Laracasts:
https://laracasts.com/lessons/from-zero-to-deploy-with-fortrabbit

Puppet download database and import using Tim Kay's aws tool

I've been trying to set up a development environment for my colleage's using vagrant and puppet for provisioning (Vagrant version 1.2.2, precise64 box). I'm currently stuck on downloading and importing a database.
For some context my company has daily database backups that get stored on Amazons S3 service, the dev environments should obtain the latest database backup and import it. We currently use Tim Kays aws tool to fetch the backups from S3.
I've successfully setup the aws tool using the following with puppet (and confirmed it works via 'vagrant ssh'):
file { '/usr/local/bin/aws':
owner => 'root',
group => 'root',
mode => 755,
source => '/puppet-files/aws',
}
file { '/home/vagrant/.awssecret':
owner => 'vagrant',
group => 'vagrant',
mode => 400,
source => '/puppet-files/.awssecret',
}
I've tried using a modified version of a suggestion for 'wget' from a post on google groups with no luck. The following is the configuration for fetching the database.
# Get and import latest db copy
exec { "download-reduced-db" :
cwd => "/tmp",
command => "/usr/local/bin/aws get sqldump.example.com/2013-01-02-db-backup.sql.gz /tmp/2013-01-02-db-backup.sql.gz",
creates => "/tmp/2013-01-02-db-backup.sql.gz",
timeout => 3600,
require => [File["/usr/local/bin/aws"], File["/home/vagrant/.awssecret"]],
}
The (shortened) output from 'vagrant up' is below, which indicates that it completed successfully.
notice: /Stage[main]//File[/home/vagrant/.awssecret]/ensure: defined content as '{md5}a4b7b1ac48eb207d93cb0b1541766718'
notice: /Stage[main]//File[/usr/local/bin/aws]/ensure: defined content as '{md5}92fa9a6d77c8185fdaf970e2c4eb254e'
notice: /Stage[main]//Exec[download-reduced-db]/returns: executed successfully
However when using 'vagrant ssh' and checking the /tmp/ directory the file is not listed. If I execute the command from above by hand it completes successfully and I can see the file listed in /tmp/
Thanks for your time and help.
Try to use
puppet.options = "--verbose --debug"
as explained here http://docs.puppetlabs.com/references/latest/configuration.html
Possibly, try to run exec with the user parameter to make sure it runs as vagrant, but first check the debug output.
Also redirect stdout and stderr of your command to a file, so you can check what's going wrong. In the Bourne shell, used by default by Puppet, this is done as:
command > file.log 2>&1
Let me know how it goes.

How to manage migrations for a rails engine + dummy app

I just joined a project developing a rails engine, that also has a dummy app for testing.
foo/
foo/spec/dummy/
There are identical migrations in
foo/db/migrate/
foo/spec/dummy/db/migrate/
If I rake db:migrate from the dummy app, all is well. If I do the same from the engine (current directory = foo) I get an error about multiple migrations with the same name.
Q1) Are the Rakefiles borked? (should db:migrate recurse down to the dummy app?)
Q2) Should the migrations only be in one directory? If so, which one?
We are using Rails 3.2.9, ruby 1.9.3p194.
Question 1
The Rakefile should have an entry to account for the spec/dummy app. For example,
Bundler::GemHelper.install_tasks
APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
load 'rails/tasks/engine.rake'
Here's more detailed example rakefile, https://github.com/twinge/questionnaire_engine/blob/engine2/Rakefile
Question 2
IMO, the migrations should only exist on the foo/db/migrate folder, and not the foo/spec/dummy/db/migrate. In fact, I don't version control the dummy's db/migrate or the db/schema.
Why? I use the dummy app the make sure a full on install of my engine works 100%. Therefore, if I version controlled the foo/spec/dummy db state, I would be testing as if there was a previous install.
Example Engine
https://github.com/twinge/questionnaire_engine/tree/engine2
For testing the dummy app, you can run your engine migrations for the test ENV using the following:
RAILS_ENV=test rake db:migrate

db:migrate creates the file db/development.sqlite3

db:migrate creates the file db/development.sqlite3 if the rails app uses sqlite. I prefer to use postgresql for development and production before deploying to heroku.
Part of my database.yml file:
development:
adapter: postgresql
encoding: unicode
host: localhost
database: app-development
username: postgres
password: *******
When I run bundle exec rake db:migrate should I be expecting a file such as db/development.postgresql? Because no file is created in the db directory and neither is an error produced.
$ bundle exec rake db:migrate
== CreateUsers: migrating ====================================================
-- create_table(:users)
NOTICE: CREATE TABLE will create implicit sequence "users_id_seq" for serial column "
users.id"
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "users_pkey" for table
"users"
-> 0.2060s
== CreateUsers: migrated (0.2070s) ===========================================
Well, postgresql is very very different from sqlite. Try reading this article so you can spot the main differences:
http://saaientist.blogspot.com.br/2007/07/choice-of-databases-or-postgres-vs.html.
The thing about your question is that sqlite work with a database file inside your application folder, and postgres don't work this way. There are plenty of resources on how postgresql work with Rails, and i recommend you to read some and try to understand.

How do I create a rake task for a Rails engine which is not exposed to the host application?

# lib/tasks/test.rake
task :hello do
puts 'hello'
end
$ rake app:hello
To run the task I need to prefix it with "app:" and it runs in the context of the dummy app. It is also exposed to the host application (i.e when used as a plugin in a parent Rails app) as rake hello.
I want to run a rake task which does not require a Rails environment and runs some command, but it is run from the engine root, not the dummy app root.
I know that's a bit late, but for others here searching for the correct answer, do the following :
Create your task :
# lib/tasks/your_engine_tasks.rake
desc "Explaining what the task does"
task :your_task do
# Task goes here
end
Then go to your engine ./Rakefile and add
load 'lib/tasks/your_engine_tasks.rake'
Here we go, now:
$ rake -T
gives you your task.
Hope I helped.
I want a better answer to this question however I did figure out that you can add tasks in the Rakefile for the engine (so ./Rakefile not in the spec/dummy application) like so:
task :my_task do
puts "hi!"
end
task :default => [:spec, :my_task]
I would prefer to have my task in another file but at least this provides a way to go forward. In my case, I want to run Konacha javascript tests in the dummy application so my Rakefile looks like this:
task :spec_javascript do
exec 'cd spec/dummy && rake konacha:run' # exec passes command return value up stack!
end
task :default => [:spec, :spec_javascript]