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.
Related
I have a strange issue with migrations. The last migration file is: 20190826113704_add_percentage_account_to_contacts.rb.
The timestamp in schema.rb is ActiveRecord::Schema.define(version: 2019_08_26_113704).
So you would say everything is up to date. When I start the server and go to the site I get the Migrations are pending error. So when I run rails db:migrate I get an error relation "study_agreements" already exists which is correct, there are no migrations pending.
So how can I solve this?
The issue is your database reflects that migration is loaded but somehow the entry in schema_migrations either got deleted(accidentally or through migration rollback).
Steps to solve this issue:
Identify the migration(migration number) from db/migrations where study_agreements relation was introduced. Let's say it is 1234
Now manually create an entry in schema_migrations table in your DB. For example in MySQL you can do "INSERT INTO schema_migrations (version) values(1234)".
Another solution is: Run rake db:migrate after commenting the change or up method of your migration in which study_agreements relation was introduced.
It seems you have already have a table in DB and you have down migrated file in your migrate folder. you can do 2 things here:
Run rails db:schema:load
OR
If you don't have data in your db then run rails db:reset
Heroku is missing the last 4 migrations for a RoR app. The files exist in the git repository and are named/numbered accordingly. I tried manually forcing that version to run, but it simply can't find it. I tried this:
heroku run rake db:migrate:up VERSION=20190328183515
and the result was
ActiveRecord::UnknownMigrationVersionError: No migration with version number 20190328183515
Trying to redeploy from local to heroku stage does nothing since it states everything is up to date. I tried touching the files in case they weren't included in the deployed files, but looking at the git repository confirmed that's not the case.
Any ideas on what's going on here and how to get heroku to recognize that it is missing 4 migration files that still need to be processed? Resetting the database is not an option. I tried rolling back the database and running the migrations again but it stops on the migration file for 3/27/2019
Still a newbie. I'm working on a new feature for a RoR app. I created a local branch and generated a migration. Unfortunately I didn't save my changes to the migration file and then ran db:migrate. Wanting to start over, I switched back to master and repulled from my git and did a hard reset with the following commands (I never committed the files in the branch either locally or remotely):
git fetch --all
git reset --hard origin/master
I then remade a local branch, recreated the migration (correctly this time) and ran db:migrate. I get an error that the table already exists in the database, however, when I look in schema.db the table isn't there.
All I want is to go back to where I was based on the remote git. For what it's worth, I'm using Cloud9 on AWS for development. Thanks!
There is nothing to do with database when you make changes regarding git. Once you run rake task like rake db:migrate to make database changes, it will get reverted automatically once you change branch, You have to prepare rollback steps. (As down methods in migrations are run conventionally)
Your old migration version was different than new recreated migration so application tried to run migration file without checking whether table exists.
Whenever you run rake db:migrate in for particular database, it store migration version in your schema_migrations table in db. So calling again and again same rake will not try to create table with same name. In above case you have different migration files to create same table and schema_migration table do not know whether you deleted branch with old migration file or whether table already exists
So run following in your rails console,
ActiveRecord::Migration.drop_table :table_name
And then run your rake db:migrate
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.
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.