issues with schema.rb file during rails 5 upgrade - ruby-on-rails

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.

Related

Rails 6, Schema file not matching migration files after switching Git branch

Rails 6, development with sqlite3.
My schema.rb has a file that shouldn't be there: no migration files (On current git branch) says it should be generated. By it's name I can see it's from an earlier branch that I abandoned, and wen't back to try a different approach to building my rails app.
To double check: I get a name error when I try to access the table in the Rails Console, so it's only there in the schema file, but not in the Database itself.
Can I force rails to run and or confirm that current schema is matching the migration files and if not matching, would run the migration?
Edit/Update:
I need to clarify that I have 6 migration files, that I went over to make sure none of them were from the earlier abandoned branch.
(admins is the table at issue)
ActiveRecord::Base.connection.tables
in the rails commandline generates:
["schema_migrations", "ar_internal_metadata", "events", "admins", "details"]
When I do Event I get the columns name and type. But when I do Admin I get
Traceback (most recent call last):
1: from (irb):3
NameError (uninitialized constant Admin)
So the issue is: How do I correctly reset the database to
The schema.rb file is auto-generated from the current state of the database, so just run rails db:migrate to re-generate schema.rb file
running rails db:migrate:reset removed seem to have fixed the issue:
My schema file does not have the 'admins' in it, and it does not show up when running ActiveRecord::Base.connection.tables in rails commandline.
I believe this was caused by Git, as it has the DB in the files: /db/*.sqlite3 and that 'admins' was created on a branch I abandoned earlier and never merged. So it was kept in the database, but the migration files were removed when I went back and created a new branch earlier on the timeline.

Rails 5.1.4 after upgrade DuplicateMigrationNameError

I recently updated a Rails 4.2 app to 5.1.4. After upgrading, whenever I try to run rake db:migrate, I get a DuplicateMigrationNameError. The first few times, the error pointed to a file so I just changed the name of the migration class and the file name. But after 4 such changes, the rake task threw the error for the file I just changed, with the new file name/class name. If I changed it back, it still threw the same error. If I changed it to something totally new the same thing happened; it picked up the new filename and threw a DuplicateMigrationNameError. I do not have a duplicate file, there are no cached files that I can find. I am running the rails app in a vagrant vm running ubuntu 16.04. The migrations all ran fine on rails 4.2.
This can also happen when the class name of the migration is duplicated. Open the migrations and see if the class names are identical.

Ruby on Rails Tutorial: Database will not update

I am working through the Michael Hartl tutorial
I have created a migration file called add_remember_token_to_users and filled it with the contents from listing 8.16 in order to create a remember_token cell, which is indexed, for each user.
I migrated the data, from the command line, which, according to the cmd, ran successfully. However, I am looking at the test.sqlite3 file with a splite browser and it has not updated to include a remember_token cell. What is going on?
I don't see any typos in my migration file and I generated the migration file using rails.
You need to apply the migration to the test database as well, because the migration only updated the development database. You can achieve it using the following command:
rake db:test:clone_structure # Recreate the test databases from the development structure
Note that there are other ways to achieve the same results such as running just the clone if you are wondering:
db:test:clone

Rails - Cant run any rake commands db is messed up

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

Rails: compile migrations into one file

We've been developing an application for a year and there are some errors in the migrations. Is there a way to compile them into one file so that we can eliminate any errors and the like without sorting through hundreds of individual files? Or can we reset the migrations so the version running right now is the first for new installations?
Migrations are already "compile" into one file call schema.rb located into the db folder of your rails application.
You can load the schema from the following rake task:
rake db:schema:load

Resources