Rake aborting, when a create_view migration file reached - ruby-on-rails

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.

Related

How to debug rails db:structure:dump?

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

Issue with Rails migrations Postgresql

I have a strange problem with rails migrations.
My db/migrate folder contains migration files and all worked fine.
But a few moment ago, I created a new file using rails g migration MigrationName, it generated a new file. then when i had runned rake db:migrate it rollbacks all and my schema version became 0.
Now when i run rake db:migrate it does nothing whereas db/migrate contains all my migrations.
i tried rake db:reset db:drop db:create db:migrate but no migrations was performed. However it says "migrations are pending run rake db:migrate RAILS_ENV=development" what i've done in vain.
I'm confused. Is anyone ever had this problem?
i've just tried RAILS_ENV=development rake db:migrate --trace and it returns:
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:migrate
** Invoke db:_dump (first_time)
** Execute db:_dump
** Invoke db:schema:dump (first_time)
** Invoke environment
** Invoke db:load_config
** Execute db:schema:dump
Running:
rake db:migrate:status
gives me:
database: database_development
Status Migration ID Migration Name
--------------------------------------------------
up 000 ********** NO FILE **********
down 20150613120151 Devise create users
down 20150613120734 Devise create admins
down 20150614114949 Create gender
down 20150614115642 Create events
down 20150614142610 Create multi events
I tried so many things:
- i dropped my database, restarted postgresql and run again rake db:setup db:migrate. rake db:migrate:status tells me that migrations are pending but it wont migrate.
it makes me crazy...
EDIT
I manually updated schema_migrations in database adding the timestamps
schema:load worked, but when i run rake db:migrate to check if its all good, it rolledback..
EDIT
if i manually fill schema_migrations version in database with timestamps, when i do rake db:schema:status they are all to up and my new migration to down, but if i do rake db:migrate it tries to revert as if i wanted to run rake db:rollback
So i fixed my issue.
In fact, i'm using dotenv to manage environment vars for development, and in my .env file i defined a var called VERSION to describe the API version...
That is the bug !
When i removed it, rake was able to migrate as expected.
I am not sure this will work for, but can you please try the following things,
First, drop tables
rake db:drop
Second, Delete/Remove db/schema.rb file
Third, create database
rake db:create
Fourth, run migration again
rake db:migrate
Please let me know if this help you a bit, good luck :)

Rake db:migration against an existing DB

I have a new rails app that wil consume from an existing DB (created by another ruby application).
To do so, I created a model for an already existing database table, but now rails gives me the error message that I have to run
rake db:migration
But if I try to do so, I get an error because the table already exists.
Is there any way to perform the migration and ignore existing tables? The table is correct, should be there and is filled with data coming for another application. I would like to have this application to consume the information.
Thanks
Edit:
The DB settings are fine because I was able to perform db:migrations before. I created the model using
rails g model fundo
(fundo is the name of the model and fundoS is the name of the table)
the model has no property yet, but the table has columns
Edit 2:
These are the output if I run with --trace
$ rake 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
$ rake db:migrate --trace
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:migrate
== CreateFundos: migrating ===================================================
-- create_table(:fundos) rake aborted! An error has occurred, this and all later migrations canceled: PG::DuplicateTable: ERROR: relation
"fundos" already exists CREATE TABLE "fundos" ("id" serial primary
key, "created_at" timestamp, "updated_at" timestamp)
It seems that rails is trying to recreate the tables. But I just want them to sync because the table is already there!
If you create models for already existing tables using rails g model, just delete the migration file that is created.
The table schema will be dumped correctly in schema.rb, so can always be created from scratch on other machines with rake db:setup, even without the migration file.
You can use rake db:schema:dump to update schema.rb manually.
Are there a lot of migrate file you want to perform? If not too many, you can perform specific version of migrate by
rake db:migrate:redo VERSION=version
If the migrate files to create table not too many, maybe you can edit the migrate file by adding:
if ActiveRecord::Base.connection.table_exists?(table_name)
before create the table.
In your local environment, maybe you could just delete the unnecessary files.
cd db/migrate/
ls | cut -d '_' f1 | while read line; do bundle exec rake db:migrate:up VERSION=$line; done
runs all migrations in file
-- create_table(:fundos) rake aborted! An error has occurred, this and all later >migrations canceled: PG::DuplicateTable: ERROR: relation "fundos" already exists >CREATE TABLE "fundos" ("id" serial primary key, "created_at" timestamp, >"updated_at" timestamp)
What I would do is go to db/migrate and go to that migration file where create_table(:fundos) happens. Comment that line out. Try it again, if it throws an error again, check the error and find the offending code. Then comment it out and keep doing that till it goes thru. Once it goes thru, un-comment everything.

stack level too deep - when using Rake

When i try and preform a rake I get the following. I've tried a few fixed like imploding and reinstalling my rvm - ruby (v1.9.3-p125) but nothing seems to be working. Any ideas?
rake db:seed --trace
** Invoke db:seed (first_time)
** Execute db:seed
** Invoke db:abort_if_pending_migrations (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:abort_if_pending_migrations
rake aborted!
stack level too deep
/Users/rm/.rvm/gems/ruby-1.9.3-p125#global/gems/rake-0.9.2.2/lib/rake/task.rb:162
Tasks: TOP => db:seed
if I try and run a rake db:reset --trace
-- initialize_schema_migrations_table()
-> 0.0025s
-- assume_migrated_upto_version(20120803181844, ["/Users/rossmcnairn/rails/search/db/migrate"])
-> 0.0218s
** Invoke db:structure:load_if_sql (first_time)
** Invoke db:create
** Execute db:structure:load_if_sql
** Invoke db:seed (first_time)
** Execute db:seed
** Invoke db:abort_if_pending_migrations (first_time)
** Invoke environment
** Execute db:abort_if_pending_migrations
rake aborted!
stack level too deep
/Users/rossmcnairn/.rvm/gems/ruby-1.9.3-p125#global/gems/rake-0.9.2.2/lib/rake/task.rb:162
Tasks: TOP => db:setup => db:seed
Ive checked that my bundle exec version and my regular rake versions match.
rake --version
rake, version 0.9.2.2
bundle exec rake --version
rake, version 0.9.2.2
Any help would be greatly appreciated!
Updating OSX is known to give issues in rubies, you should recompile all your rubies:
chown -R $USER: $rvm_path
rvm get head
rvm reinstall 1.9.3-p125
rvm all-gesmsets do rvm gemset pristine
Also note that any other compiled stuff like libraries needs recompilation, this includes homebrew, macports, rvm pkg and any packages you have compiled manually.
** Execute db:abort_if_pending_migrations
rake aborted!
Did you try to run your migrations?
This may be obvious but it took me a few hours. I unwittingly created a class method for the model called create, which is the same as ActiveRecord create. So my seed file has circular reference.
I just kept staring at the seed file and wonder why I get the error while I was able to run it twice before (before adding the class method). Moral of the story: naming is important.

rake db:migrate doesn't work and brings me back to the command line rails 3.1.1

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.

Resources