Rails - Cant run any rake commands db is messed up - ruby-on-rails

Everything was working fine yesterday and all of a sudden when i tried to run my server or run any rake tasks i get this error
Mysql2::Error: Table 'myapp_development.key_value_stores' doesn't exist: SHOW FULL FIELDS FROM `key_value_stores`
I've tried google searching and searching on stackoverflow all morning and cant find a solution to this

Try to rollback your migration file (take backup of the migration file will help while creating new ),
rake db:migrate:down VERSION=migrations's_version
and then try to restart the rails server.If you face same problem then with same migration number search in schema_migrations table in database delete that record and make new migration again.

I was struggling with this for a while but what i ended up doing
update xcode and update command line tools
uninstall then install mysql via homebrew
then i ended up manually removing my database folder in
usr/local/var/mysql/myapp
then i did a
rake:create
and then i just pulled my production database and copied it to my development database
rake db:backup_and_load
then I was up and running again
dont know which one of that fixed it specifically but thats what i did

Related

issues with schema.rb file during rails 5 upgrade

After upgrading to Rails 5, I receive an error message like following every time I try to load from schema (set up a new computer on the app, run rails db:test:prepare before running tests, etc.):
ActiveRecord::StatementInvalid: PG::UndefinedObject: ERROR: type "serial" does not exist
LINE 1: SELECT 'serial'::regtype::oid
Searching around isn't yielding much help. The most relevant thread is this one: https://github.com/rails/rails/issues/30298 but I am not trying to run any new migrations, nor does the schema_plus_indexes gem seem to have anything to do with the issue (the two issues described in that thread).
In our case, we don't keep migration files around after they have been run against all databases. Because of this, when working on the upgrade to rails 5, there were 0 migration files present. The issue, it seems, is that rails will only automatically "fix" your schema.rb file for you if you are actually running a migration file (even trying rails db:migrate with no migration files present won't work).
The solution, for us, was to create a blank migration and run rails db:migrate in order to get the schema.rb file properly formatted.

removing postgres database on ubuntu from commandline

I am working on a rails app. Due to some messing up with the code I deleted the app from my local development machine and cloned the previous commit from the git repo.
Now i want to delete the db file(postgres) of the deleted one from the local machine as well. I had issues in the past that made me unable to access several features due to database conflicts(due to same db name). So i fixed that by removing the database and recreating with the cloned app. I had done it before from commandline but i forget now on how to do it. Could some one tell me where is the postgres database file located in ubuntu or how to remove from commandline? Thanks in advance.
There is a postgresql shell command dropdb which you can use.
After a bit of searching i found that doing this will remove the database. From the commandline just cd into your app directory and run this command
rake RAILS_ENV='development' db:drop
This will remove the database(although am still unable to find the exact location where its stored physically in hard drive). This is the solution if you cant access rails console, or some rake commands.
After that we can just recreate the database using
rake db:create

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

Multiple Migrations whit same Version Number on Deployng Rails to Heroku

Im have this problem many times, i search much and not found any solution to solve, my problem is this:
After the run
git push heroku push master
When i run
heroku run rake db:migrate
i get this error:
Multiple migrations have the version number 20130615132808
im search by this problem and found this:
rails database migration - multiple migrations have the version number x
but when execute git rm appear some options i dont understand much about git so i need solve this problem, in localhost im delete the archives but the problem persists, thanks very much by the help.
Just rename the files with duplicate timestamps (add 1 to the last digit) and then add, commit and push files. When you run heroku run rake db:migrate again all will be dandy.
And for the future remember to not copy and rename migrations by hand (so you don't get repeated version numbers)
This might occur when you copy-paste multiple "rails generate" commands to generate migrations. The migrations generated may have the same time stamp. If you type them in (or copy-paste them) separately, they will have different timestamps.
When this happens, you can simply rename the migration files under db/migrate to contain different timestamps.

rails database migration - multiple migrations have the version number x

Ok, I have stuffed up my migrations. I tried to sort it by deleting duplicates, sorting the schema.rb etc but I don't think I have done it properly.
When I try to deploy to heroku, or rather heroku run rake db:migrate, I get
Multiple migrations have the version number 20130307005437
The migrate works fine on localhost but not heroku.
Unfortunately when I look for migration no 20130307005437, it's not there in my db/migrate.
How can I find it to sort the problem?
While this file might not be visible within your directory listing, I suspect that there might already be a file within your Git repository, which is what is causing this error from appearing on Heroku and not locally.
Please ensure that you've only got one migration inside that Git repo with that number.

Resources