I have the line config.active_record.schema_format = :sql in my application.rb file but I'm still having the problem that when I type rails db:migrate it doesn't output the :sql version. In fact it doesn't output anything. When I type in rails db:structure:dump it doesn't output anything either. However, when I type in rails db:schema:dump it outputs the schema.rb file. I'm using postgresql and rails 5.0.
It seems to me that for some reason rails db:structure:dump is not working but I don't know how to debug this. Any help will be greatly appreciated.
~/main$ rails db:schema:dump --trace
** Invoke db:schema:dump (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:schema:dump
Similar but different question:
In Rails 5, setting config.active_record.schema_format = :sql but still getting schema.rb created on db:migrate
Related
I make an image-upload for a user profile using the activestorage gem built into rails, but it is not working, the intriguing part is that there are no errors, the migration just does not get created.
What is wrong here, any ideas on how I can fix this?
rails version - 6.0
I added require 'active_storage/engine' to the config/application.rb file already
And then ran rails active_storage:install
What is even more intriguing is that I get the same output without adding the require active_storage/engine to application.rb. And there are no errors
output
** Invoke active_storage:install (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute active_storage:install
** Invoke active_storage:install:migrations (first_time)
** Execute active_storage:install:migrations
** Invoke railties:install:migrations (first_time)
** Invoke db:load_config (first_time)
** Invoke environment
** Execute db:load_config
** Execute railties:install:migrations```
I expect the migration file to be created after this but it is not.
I'm trying to get some code running locally. But am running up against a problem when I run:
rake db:create db:migrate db:seed --trace
Using Postgres.
We have the gem scenic included which creates database views using create_view but for some reason when the migration reaches the migration file which creates a view I get the following error:
steve-vmn:ss steve$ rake db:create db:migrate db:seed --trace
RAILS_GROUPS is unset; defaulting to web,worker
** Invoke db:create (first_time)
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:create
Database '22_development' already exists
Database '22_test' already exists
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config
** Execute db:migrate
== 20170816124642 CreateStations: migrating ======================
-- create_view(:stations)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
No such file or directory # rb_sysopen - /Users/steve/Ss/ss/db/views/stations_v01.sql
/Users/steve/.rvm/gems/ruby-2.4.1/gems/scenic-1.5.1/lib/scenic/definition.rb:10:in `read'
/Users/steve/.rvm/gems/ruby-2.4.1/gems/scenic-1.5.1/lib/scenic/definition.rb:10:in `to_sql'
Rake file:
class CreateStations < ActiveRecord::Migration[5.0]
def change
create_view :stations
end
end
I can't seem to find a similar error anywhere.
Based on the error message, and the scenic documentation, you need to define a SQL view in db/views/stations_v01.sql. See the following link for an example.
https://github.com/scenic-views/scenic#great-how-do-i-create-a-view
See docs: https://github.com/scenic-views/scenic/blob/master/README.md
As mentioned in your error logs, there is no file exists: db/views/stations_v01.sql that comes after running the migration.
rails generate scenic:view stations
create db/views/stations_v01.sql
create db/migrate/[TIMESTAMP]_create_stations.rb
After that
Edit the db/views/stations_v01.sql file with the SQL statement that defines your view.
I am using FactoryGirl in my rails application instead of Fixtures.
When i try to use factory girl in my test and create some test data, it shows like
PG:Error relation "users" doesn't exists (i have a model named User)
But when i run rake db:test:clone, and then run the test, the test is passed. The rake db:test:clone command clones all the table structure from development db to test db, and this fixes the issue.
But is there any way for me to not to run rake db:test:clone when using FactoryGirl?
or what am i missing?
Update :
I found out one other thing, i have another application which uses Rspec and FactoryGirl. In that application below are executed when running rake spec --trace command
** Invoke spec (first_time)
** Invoke db:test:clone_structure (first_time)
** Invoke db:structure:dump (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:structure:dump
** Invoke db:test:load_structure (first_time)
** Invoke db:test:purge (first_time)
** Invoke environment
** Invoke db:load_config
** Execute db:test:purge
** Execute db:test:load_structure
** Invoke db:structure:load (first_time)
** Invoke environment
** Invoke db:load_config
** Execute db:structure:load
But in any of the new application i see the below executed when running rake spec --trace
** Invoke spec (first_time)
** Invoke noop (first_time)
** Execute noop
** Execute spec
Please suggest what am i missing?
regards
Balan
Every time you run a db:migrate, run a db:test:prepare as well, so the database changes are mirrored on your test database as well.
I found the solution, we need to create a rake task customrake.rake under /lib/tasks
and add the line task :spec => 'db:test:prepare', this will make sure rake db:test:prepare is executed before running Specs.
Using rails 3.1.1 for windows with railsinstaller
>rake db:migrate
after a pause, brings me right back to the command line. No errors, no messages, just right back to the command line.
I tried
>rake --trace db:migrate
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Invoke rails_env (first_time)
** Execute rails_env
** Execute db:load_config
** Execute db:migrate
** Invoke db:schema:dump (first_time)
** Invoke environment
** Invoke db:load_config
** Execute db:schema:dump
Doesn't look like anything is wrong, but obviously something isn't working right.
Can anyone help?
If there are no migrations to be run, there will be no output. The --trace command outputs the various steps that rake goes through to prepare, execute, and clean up after the migration. However, when it sees that there is no migration to be run, then it doesn't actually make any DB changes. Only DB changes cause additional output.
The only difference between this and a migration is that the changes to the DB will be output to the command line. No changes => no output.
Make sure you are in the right folder (maybe in the right branch, when using git) and make sure that you have created a migration file.
rails generate migration MigrationName
Edit the generated file as you wish.
Just wondering if there's a way to run Rails tests without dropping the database. I'm currently only executing unit tests and am using the following rake command to do so: rake test:units.
Thanks for the help in advance!
Just in case this is relevant:
Rails 3
Ruby 1.8.7 (MRI)
Oracle 11g Database
activerecord-oracle_enhanced-adapter
In Rails 5 (and possibly earlier versions), just comment-out the following line in spec/rails_helper.rb:
ActiveRecord::Migration.maintain_test_schema!
This will prevent rake test or rspec from attempting to drop your test DB. You'll need to run migrations manually as well.
For Rails 5.2 this behaviour can be modified setting maintain_test_schema to false in test/test_helper.rb before importing rails/test_help:
ActiveRecord::Base.maintain_test_schema = false
require "rails/test_help"
rails/test_help will check the value of maintain_test_schema to decide if it has to drop/create/migrate the test database or not.
After doing some research, I have found that there isn't a way to do this. The test rake tasks will always drop the database, even when providing the TEST= option as Bohdan suggests.
By using the --trace option, this can be proven. Here is the output:
$ rake test:units TEST=test/unit/post_test.rb --trace
(in /Users/johnnyicon/Development/ror/test-app)
** Invoke test:units (first_time)
** Invoke test:prepare (first_time)
** Invoke db:test:prepare (first_time)
** Invoke db:abort_if_pending_migrations (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:abort_if_pending_migrations
** Execute db:test:prepare
** Invoke db:test:load (first_time)
** Invoke db:test:purge (first_time)
** Invoke environment
** Execute db:test:purge
** Execute db:test:load
** Invoke db:schema:load (first_time)
** Invoke environment
** Execute db:schema:load
** Execute test:prepare
** Execute test:units
Reading through the Ruby on Rails Guides for Testing, it describes what some of these rake tasks mean. The one to pay particular attention to is the db:test:load task, which you see on the 7th line from the bottom of the output as ** Execute db:test:load. The guides say the following about this task:
Recreate the test database from the
current schema.rb
So even if I were to execute the unit tests one by one as Bohdan suggests, the rake task would still recreate the database. Not the answer I was hoping for, but it isn't a problem anymore.
The reason I was asking to begin with was because I did not have access to another database to use for testing, so I was using my development database for testing as well. But since then, I've been able to get another database dedicated for testing.
Thanks anyway Bohdan! I appreciate the help!
This is a rather old post that does the monkey patching of overriding purge/load tasks:
http://www.pervasivecode.com/blog/2007/09/22/making-rails-raketest-not-drop-your-pgsql-database/
For those looking for a way to skip Rails' default behavior, try adding this to your Rakefile:
Rake::Task["db:test:prepare"].clear
Rake::Task["db:test:load"].clear
Rake::Task["db:test:purge"].clear
Could you not write a custom Rake task that monkey patched the Rake db:test:load task to do nothing?