Starting PostgreSQL database for Rails app - ruby-on-rails

I'm trying to get my Rails app connected to a PostgreSQL database.
I successfully ran rake db:create
Now when I run rake db:migrate I get the following error ...
===============================================================================
HINT:
You might not have the 'pgcrypto' PostgreSQL extension installed.
Install using:
CREATE extension IF NOT EXISTS pgcrypto SCHEMA pg_catalog;
You can also install it in the 'template1' database for future databases.
===============================================================================
rake aborted!
An error has occurred, this and all later migrations canceled:
PG::UndefinedFunction: ERROR: function gen_salt(unknown) does not exist
LINE 3: (SELECT gen_salt('bf') AS salt)
^
I tried installing the pgcrypto extension, but I get the same error.
I also tried running rake db:setup and I get this ...
============================================================
WARNING: Pulse calls are disabled until the application is restarted.
rake aborted!
No such file or directory # rb_sysopen - /Users/kweihe/pmpaware-webapp/db/structure.sql
/Users/kweihe/pmpaware-webapp/lib/tasks/db.rake:8:in `initialize'
/Users/kweihe/pmpaware-webapp/lib/tasks/db.rake:8:in `new'
/Users/kweihe/pmpaware-webapp/lib/tasks/db.rake:8:in `block (3 levels) in <top (required)>'
/Users/kweihe/.rvm/gems/ruby-2.1.6/gems/activerecord-3.2.22/lib/active_record/railties/databases.rake:484:in `block (3 levels) in <top (required)>'
/Users/kweihe/.rvm/gems/ruby-2.1.6/bin/ruby_executable_hooks:15:in `eval'
/Users/kweihe/.rvm/gems/ruby-2.1.6/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => db:structure:load => db:structure:pg_crypto_preflight
(See full trace by running task with --trace)
FIXED
I installed the extension in the wrong db. Went through PgAdmin and installed in correct db, now migrate runs successfully

I installed the extension in the wrong db. Went through PgAdmin and installed in correct db, now migrate runs successfully
Installed extension by running this in the query field:
CREATE extension IF NOT EXISTS pgcrypto SCHEMA pg_catalog;

Related

Pre-existing database with new rails application

I have an pre-existing database named exam_prep in postgresql, which i am using in flask.
I have created a new Rails application and i want to use that database with it.
So far I have run rake db:structure:dump, which created a structure.sql under the db folder.
After that I ran db:structure:load, but got the following error:
psql:/Users/namangupta/Desktop/app/db/structure.sql:49: ERROR: type "batch_types_enum" already exists
rake aborted!
failed to execute:
psql -v ON_ERROR_STOP=1 -q -f /Users/namangupta/Desktop/app/db/structure.sql exam_prep
Please check the output above for any errors and make sure that `psql` is installed in your PATH and has proper permissions.
/Users/namangupta/.rvm/gems/ruby-2.3.4/gems/activerecord-5.1.4/lib/active_record/tasks/postgresql_database_tasks.rb:108:in `run_cmd'
/Users/namangupta/.rvm/gems/ruby-2.3.4/gems/activerecord-5.1.4/lib/active_record/tasks/postgresql_database_tasks.rb:80:in `structure_load'
/Users/namangupta/.rvm/gems/ruby-2.3.4/gems/activerecord-5.1.4/lib/active_record/tasks/database_tasks.rb:223:in `structure_load'
/Users/namangupta/.rvm/gems/ruby-2.3.4/gems/activerecord-5.1.4/lib/active_record/tasks/database_tasks.rb:236:in `load_schema'
/Users/namangupta/.rvm/gems/ruby-2.3.4/gems/activerecord-5.1.4/lib/active_record/tasks/database_tasks.rb:255:in `block in load_schema_current'
/Users/namangupta/.rvm/gems/ruby-2.3.4/gems/activerecord-5.1.4/lib/active_record/tasks/database_tasks.rb:304:in `block in each_current_configuration'
/Users/namangupta/.rvm/gems/ruby-2.3.4/gems/activerecord-5.1.4/lib/active_record/tasks/database_tasks.rb:303:in `each'
/Users/namangupta/.rvm/gems/ruby-2.3.4/gems/activerecord-5.1.4/lib/active_record/tasks/database_tasks.rb:303:in `each_current_configuration'
/Users/namangupta/.rvm/gems/ruby-2.3.4/gems/activerecord-5.1.4/lib/active_record/tasks/database_tasks.rb:254:in `load_schema_current'
/Users/namangupta/.rvm/gems/ruby-2.3.4/gems/activerecord-5.1.4/lib/active_record/railties/databases.rake:290:in `block (3 levels) in <top (required)>'
/Users/namangupta/.rvm/gems/ruby-2.3.4/gems/rake-12.3.0/exe/rake:27:in `<top (required)>'
/Users/namangupta/.rvm/gems/ruby-2.3.4/bin/ruby_executable_hooks:15:in `eval'
/Users/namangupta/.rvm/gems/ruby-2.3.4/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => db:structure:load
(See full trace by running task with --trace)
I want Rails to use the database and make a models within this. Any ideas?
The db:structure:load rake task creates a tables and columns from the structure.sql or schema.rb. You shouldn't need to run it, as your existing database already has all the tables you need.
Your next step would be to go about creating your model files and pointing them at the appropriate tables.

Issue regarding running rake db:seed task

I have an existing rails project and I am using postgres database and after successful execution of bundle exec rake db:migrate command when I am going to execute the bundle exec rake db:seed command then it throws me the following error.
rake aborted!
PG::DuplicateTable: ERROR: relation "schema_migrations" already exists
I have commented out the lines where the definition of the schema_migration table creation is stored such as in the development_structure.sql and test_structure.sql files. But it is still throwing me the error.
I don't know from where the schema_migration table is being created. There is no schema_migration.rb file in my project.
Pleas help me to fix this. Thanks in advance!
This is my error log.
rake aborted!
PG::DuplicateTable: ERROR: relation "schema_migrations" already exists
: CREATE TABLE "schema_migrations" ("version" character varying(255) NOT NULL)
/home/bit/.rvm/gems/ruby-2.1.5/gems/activerecord-2.3.18/lib/active_record/connection_adapters/abstract_adapter.rb:227:in `rescue in log'
/home/bit/.rvm/gems/ruby-2.1.5/gems/activerecord-2.3.18/lib/active_record/connection_adapters/abstract_adapter.rb:204:in `log'
/home/bit/Development/app2/application/lib/core_extensions/postgresql_adapter/reconnect.rb:26:in `execute'
/home/bit/.rvm/gems/ruby-2.1.5/gems/activerecord-2.3.18/lib/active_record/connection_adapters/abstract/schema_statements.rb:109:in `create_table'
/home/bit/.rvm/gems/ruby-2.1.5/gems/activerecord-2.3.18/lib/active_record/connection_adapters/abstract/schema_statements.rb:371:in `initialize_schema_migrations_table'
/home/bit/.rvm/gems/ruby-2.1.5/gems/activerecord-2.3.18/lib/active_record/migration.rb:441:in `initialize'
/home/bit/.rvm/gems/ruby-2.1.5/gems/rails-2.3.18/lib/tasks/databases.rake:193:in `new'
/home/bit/.rvm/gems/ruby-2.1.5/gems/rails-2.3.18/lib/tasks/databases.rake:193:in `block (2 levels) in <top (required)>'
/home/bit/.rvm/gems/ruby-2.1.5/bin/ruby_executable_hooks:15:in `eval'
/home/bit/.rvm/gems/ruby-2.1.5/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => db:seed => db:seed:common => db:seed:original => db:abort_if_pending_migrations
(See full trace by running task with --trace)
If it's OK to drop your data locally, try
rake db:drop db:create db:migrate
and then seed again. I've had similar issues in the past where I deleted a migration without properly rolling it back and this was the solution.
If you need to keep it, open the rails database (rails db from terminal) and try
drop table schema_migrations
(Run the migrations with db:migrate again after this)
Edit: This has not solved the issue yet, but it seems there may be other issues that need to be factored in. OP cannot launch rails console and receives
undefined local variable or method `vars' for #<Rails::Generator::Commands::Create:0x00000001ca2578>
as the error message each time - if anyone has ideas here, please add an answer!

DEPRECATION WARNING while using bundle install

So I am trying to do bundle install . But it is throwing the below errors. What is the problem here?
bundle exec rake db:create:all
DEPRECATION WARNING: String based terminators are deprecated, please use a lambda. (called from included at /home/.gem/ruby/2.1.3/bundler/gems/authlogic-09163c7d2a9b/lib/authlogic/session/callbacks.rb:66)
DEPRECATION WARNING: String based terminators are deprecated, please use a lambda. (called from included at /home/.gem/ruby/2.1.3/bundler/gems/authlogic-09163c7d2a9b/lib/authlogic/session/callbacks.rb:67)
rake aborted!
Cannot load `Rails.application.database_configuration`:
Could not load database configuration. No such file -
/home/.gem/ruby/2.1.3/gems/railties-4.1.5/lib/rails/application/configuration.rb:105:in `database_configuration'
/home/.gem/ruby/2.1.3/gems/activerecord-4.1.5/lib/active_record/railtie.rb:43:in `block (3 levels) in <class:Railtie>'
Could not load database configuration. No such file -
/home/.gem/ruby/2.1.3/gems/railties-4.1.5/lib/rails/application/configuration.rb:105:in `database_configuration'
/home/.gem/ruby/2.1.3/gems/activerecord-4.1.5/lib/active_record/railtie.rb:43:in `block (3 levels) in <class:Railtie>'
Tasks: TOP => db:create:all => db:load_config
(See full trace by running task with --trace)
Earlier I had tried the below:
rake db:create:all
rake aborted!
Gem::LoadError: You have already activated rake 10.4.2, but your Gemfile requires rake 10.3.2. Prepending `bundle exec` to your command may solve this.
/home/.gem/ruby/2.1.3/gems/bundler-1.7.12/lib/bundler/runtime.rb:34:in `block in setup'
/home/.gem/ruby/2.1.3/gems/bundler-1.7.12/lib/bundler/runtime.rb:19:in `setup'
/home/.gem/ruby/2.1.3/gems/bundler-1.7.12/lib/bundler.rb:122:in `setup'
/home/.gem/ruby/2.1.3/gems/bundler-1.7.12/lib/bundler/setup.rb:7:in `<top (required)>'
/home/project/config/boot.rb:4:in `<top (required)>'
/home/project/config/application.rb:1:in `<top (required)>'
/home/project/Rakefile:4:in `<top (required)>'
LoadError: cannot load such file -- bundler/setup
/home/project/config/boot.rb:4:in `<top (required)>'
/home/project/config/application.rb:1:in `<top (required)>'
/home/project/Rakefile:4:in `<top (required)>'
(See full trace by running task with --trace)
$ bundle exec rake db:create:all
DEPRECATION WARNING: String based terminators are deprecated, please use a lambda. (called from included at /home/.gem/ruby/2.1.3/bundler/gems/authlogic-09163c7d2a9b/lib/authlogic/session/callbacks.rb:66)
DEPRECATION WARNING: String based terminators are deprecated, please use a lambda. (called from included at /home/.gem/ruby/2.1.3/bundler/gems/authlogic-09163c7d2a9b/lib/authlogic/session/callbacks.rb:67)
rake aborted!
Cannot load `Rails.application.database_configuration`:
Could not load database configuration. No such file -
/home/.gem/ruby/2.1.3/gems/railties-4.1.5/lib/rails/application/configuration.rb:105:in `database_configuration'
/home/.gem/ruby/2.1.3/gems/activerecord-4.1.5/lib/active_record/railtie.rb:43:in `block (3 levels) in <class:Railtie>'
Could not load database configuration. No such file -
/home/.gem/ruby/2.1.3/gems/railties-4.1.5/lib/rails/application/configuration.rb:105:in `database_configuration'
/home/.gem/ruby/2.1.3/gems/activerecord-4.1.5/lib/active_record/railtie.rb:43:in `block (3 levels) in <class:Railtie>'
Tasks: TOP => db:create:all => db:load_config
(See full trace by running task with --trace)
You are trying to run "rake db:create:all" which would create your database but as error states - it seems you don't have your config/database.yml file. It's a configuration file that tells rails how to connect to your database. You can read more about it here.
If you really want to run bundle install, just use "bundle install" command instead. This will install all gems listed in your Gemfile.
If you will get problems with rake version conflicts, check out this stack overflow question.
It seems you have no config/database.yml file in your project's directory.
Why I think so? Follow me ...
Could not load database configuration. No such file -
/home/.gem/ruby/2.1.3/gems/railties-4.1.5/lib/rails/application/configuration.rb:105:in `database_configuration'
Ok, let's take a look at this code where we have error:
yaml = Pathname.new(paths["config/database"].existent.first || "")
config = if yaml.exist?
require "yaml"
require "erb"
YAML.load(ERB.new(yaml.read).result) || {}
elsif ENV['DATABASE_URL']
# Value from ENV['DATABASE_URL'] is set to default database connection
# by Active Record.
{}
else
raise "Could not load database configuration. No such file - #{yaml}"
end
As you can see - if no such file or ENV variable then rails throws error.

Rake Aborted due to invalid sequence

I am updating one of rails app on my server with new content. It was working fine until the previous day. Now when I try to migrate my db using rake or use any rake commands, I get the following errors:
rake aborted!
rake aborted!
invalid byte sequence in US-ASCII
/var/lib/gems/1.9.1/gems/rake-10.0.4/lib/rake/trace_output.rb:16:in `block in trace_on'
/var/lib/gems/1.9.1/gems/rake-10.0.4/lib/rake/trace_output.rb:14:in `map'
/var/lib/gems/1.9.1/gems/rake-10.0.4/lib/rake/trace_output.rb:14:in `trace_on'
/var/lib/gems/1.9.1/gems/rake-10.0.4/lib/rake/application.rb:328:in `trace'
/var/lib/gems/1.9.1/gems/rake-10.0.4/lib/rake/application.rb:183:in `display_error_message'
/var/lib/gems/1.9.1/gems/rake-10.0.4/lib/rake/application.rb:169:in `rescue in standard_exception_handling'
/var/lib/gems/1.9.1/gems/rake-10.0.4/lib/rake/application.rb:159:in `standard_exception_handling'
/var/lib/gems/1.9.1/gems/rake-10.0.4/lib/rake/application.rb:88:in `load_rakefile'
/var/lib/gems/1.9.1/gems/rake-10.0.4/lib/rake/application.rb:72:in `block in run'
/var/lib/gems/1.9.1/gems/rake-10.0.4/lib/rake/application.rb:160:in `standard_exception_handling'
/var/lib/gems/1.9.1/gems/rake-10.0.4/lib/rake/application.rb:70:in `run'
(See full trace by running task with --trace)
My local machine ruby version is: 2.0.0p0
And my server ruby version is: ruby 1.9.3p194
Is it because I need to update my ruby version on my server to 2.0.0p0
Thanks
Add
# encoding: utf-8
to the top of your migration file.
this is one of the changes between ruby 1.9.3 and 2.0.0 - default utf-8 encoding, you should always try to develop, test and deploy on the same version of ruby (you can/should test on more rubies).
Add a system environment in the
~/.bash_profile file.
export RUBYOPT='-KU'
in my host ,it works well.

Rake test fails with LoadError, looking in home directory

When I try to run "rake test" on my newly generated rails application I receive the following error
Computer:heroku user$ rake test:units
/Users/user
file doesnt exist
/Users/user/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:240:in `require': cannot load such file -- /Users/user/test/unit/helpers/things_helper_test.rb (LoadError)
from /Users/user/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:240:in `block in require'
from /Users/user/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:225:in `load_dependency'
from /Users/user/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:240:in `require'
from /Users/user/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/rake_test_loader.rb:10:in `block (2 levels) in <main>'
from /Users/user/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/rake_test_loader.rb:9:in `each'
from /Users/user/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/rake_test_loader.rb:9:in `block in <main>'
from /Users/user/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/rake_test_loader.rb:4:in `select'
from /Users/user/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/rake_test_loader.rb:4:in `<main>'
rake aborted!
Command failed with status (1): [/Users/user/.rvm/rubies/ruby-1.9.3-p0/b...]
Tasks: TOP => test:units
(See full trace by running task with --trace)
I am guessing that this is related to my setup somehow, as it seems as if rake is looking for the generated files in my home directory instead of the rails application directory. Could you help direct me to what I am doing wrong?
I am using Mac OS X Lion, have two installed ruby versions (1.8.7 come with lion, 1.9.3 which I am using for the application has been installed using rvm).
The application works if I run "rails s".
Update: If I create a new rails application from scratch, adding only a generated scaffold, the rake test command works. Ergo, it might be a incompatibility issue as the failing rails application was created on another computer, uploaded to heroku and then cloned from there to a new computer.
Funny story. I'd accidentally put some test code in one of the files, among other containing:
Dir.chdir("../..")
I'm guessing that when I run rake test the file got executed to load the class contained in the file, and caused problems when looking for the next file, when the current directory had suddenly changed.

Resources