Migrations doesn't affect database - ruby-on-rails

I am new to Rails and I am facing the following problem. There were different tables in the existing project schema such as questionnaire, test, answers, questions, etc. But they did not have the columns of the creation date and the update date, and there were no models and views yet. So I decided to delete them and recreate them. Unfortunately I did it directly through the pgAdmin. Then I recreated each model with a scaffold generator. Everything was going fine until I got to the Questions model. For this model, I forgot to delete the table from the database, which I decided to do. But when I returned to the terminal and tried again to migrate the creation of the question table, I came across the same error message that the table already exists. Moreover, in the schema, I found that all tables manually removed from the database are still present.
What I tried:
I wrote migrations to drop tables. Migrations go through, tables disappear from the schema. Then I tried to write again table creation migrations, they are also started and tables are added to the schema, but they are not created in the database itself. But the application is still accessing the database, retrieving data, inserting it (into other tables that I have not touched on).
How can I return everything to the original? Migrations do not work in the project and I do not understand how to fix it. And I would not really like to dump the database, since it contains a lot of data.
Also in the project there are old migrations of test creation, test results, etc., which I deleted manually through the PG Admin. Should I delete them?
db/migrate

I found a bug. The problem was that the project turns out to be using docker (did not fully understand what it is). But as I understand it, for any update of the project, it must be reloaded through 4 commands in the terminal. Therefore, migrations did not go through the terminal. they had to be done by the docker himself. I expected to see the error logs right in the terminal, and they were issued by docker. I should have connected to the docker logs and read errors from there. There, he swore in plain text about the impossibility of migrating due to the lack of tables. I rolled back migrations to the state before the problem, removed migrations conflicting with the current state of the database and created new ones, according to the rails conventions. In the end, everything worked.

Related

RAILS/DOCKER - Schema keeps reappearing after deleting the migration

my issue appears to pertain to mainly docker.
I got frustrated with a table that I had so I deleted it plus all associated migrations from my rails migrations.
I did make a zip file of repository before I did this so that I wouldn't lose all my work, I'm not sure if this can be affecting anything but I thought I would put it out there just in case because all of those files do technically exist somewhere else.
When I do rails db:migrate:status those migrations no longer appear on the list, but they still exist in schema.rb
What I have tried:
I deleted my branch that the original migration is in and started in a new one.
I tried just deleting it from schema.rb because it didn't exist anymore, but when I tried to create a table with the same name (because I need the table to be named that) it says the migration failed because that table already exists.
So then I restarted my docker container with docker restart <container name*> and tried running the migration again... same error.
Next I rebuilt my database, same error.
After that failed I deleted the docker container and rebuilt it from scratch.. same error.
I then deleted the repository from my computer, deleted the docker image, shut down and restarted my computer. Re-cloned the repository, rebuilt the container and still the same error.
It just keeps reappearing in the schema.
Sure I could create a new table with a different name, but I don't want the schema for a table that doesn't exist.
What should my next move be?
If I understood correctly, you had run migrations to update your schema.rb file, then you removed (dropped) a database table, maybe using your database software directly, and then you deleted the related migration files.
I don't think this has anything to do with Docker but everything with how Rails works with databases.
First, Rails will not know if you remove tables or make any other changes to your database directly using your database software and SQL (DDL), so the schema file is not updated based on it.
Second, Rails will not know about removing any migration files from the disk, and the schema file is not updated based on your changes.
You will always have to use migrations to keep your database structure up-to-date.
Your options
All of the below options only operate on your local database.
Option 1: Resetting and recreating your local database using Rails
Assumptions: you can delete your local database
You could try resetting your database with
bin/rails db:reset
that basically runs bin/rails db:drop db:setup that is described as:
The bin/rails db:setup command will create the database, load the schema, and initialize it with the seed data.
This should recreate the database structure using your schema.rb and it should bring you back the frustrating table.
Option 2: Resetting and recreating your local database by restoring it from a backup
Assumptions: you have a backup, you can delete your local database
You could try restoring or importing the latest backup using your database software. Most likely, you would need to drop the database, first, but it depends on the tools used and their options available.
This should result in the structure you have in your schema.rb and it should give you back the frustrating table with any data you had in the backup snapshot.
Option 3: Recreating only the missing table in your local database by importing it from a backup
Assumptions: you have a backup, you do not want to delete your local database
You could try restoring or importing only the missing table using your database software.
This should result in the structure you have in your schema.rb and it should give you back the frustrating table. It will also import any data for that table you had in your backup snapshot.
Depending on your table definition(s), you may need to update something in the data, though, especially if other tables reference this one or this one has references to others. The data you imported might be wrong so you may want to set some references to NULL, for example, or fix them, whichever suits you better.
Option 4: Recreating only the missing table in your local database manually
Assumptions: you do not have a backup, you do not want to delete your local database
You could try recreating just the frustrating table manually without any data.
You could do this based on your schema.rb by translating it to SQL DDL yourself. A faster approach would be to ask one of your team members or colleagues, if any, who has an up-to-date version of the database structure to share you the SQL DDL for that table so that you could run it locally using your database software, for example.
This would result in the structure you have in your schema.rb and it should give you back the frustrating table.
Next steps: Finally, no matter which option you decided to implement to restore your database structure, you should create a new migration in which you call drop_table to remove (drop) the frustrating table correctly. That will remove the table definition from the schema.rb as well so that you can later create a new migration to recreate the table with the same name.

Skip Current Migrations but Apply Future Ones

QUESTION:
Is there any way I can avoid running current migrations on a remote database already configured correctly, while ALSO being allowed to apply future migrations to it?
Context:
I have an existing rails app with plenty of migrations, that, up until now, has been using a local postgres database. A remote postgres database which SHOULD mostly match up to the structure of my local database exists.
When I try to connect to it, I get a "pending migrations" error. Attempting to run my migrations gets errors about tables already existing.
I want to skip all current migrations, but UNLIKE a lot of similar questions I'm seeing, I want to make sure my future migrations will work on this remote database.
Edit:
I followed this answer:
Rails 4 how to ignore pending migrations
And inserted my own current scheme version number into the remove database's schema_migrations table. But the pending migration remains.
I can confirm trying to run the migration gets me an error of a table already existing. This table is the FIRST migration past the remote databases previously most recent version in the schema table. It appears to be ignoring my inserted current most recent version.
You have to add all migration version numbers that you want to ignore to schema_migrations
Other technique that may be also applicable - migration squashing with something like squasher or manually. Point is in combining all old migrations that are not to be rolled back into one.

How to completly remove migrations?

For some reason my project keeps generating a __migrationhistory table and I'm trying to get rid of that.
We used Migrations, but we want to remove them now. I already removed the Migration directory along with all migrations and configurations. I also dropped the whole schema and let it recreate using an Initializer.
I have no idea why this __migrationhistory is still created, I don't find anything related to Migrations in the project anymore.

Code Migrations is skipping initial code migration

Using Entity Framework 5, we're using Code-First Migrations in our application. Every developer has his own database on which they work.
I have accidently emptied mine: There is nothing in there anymore, no tables, not even the migration history table.
So, I've tried to update the database again through the PM console by executing update-database. It immediatley gives me an error that a table does not exist, while it should be created in my inital code migration.
What is interesting is that the PM console also shows what migrations are being applied, which does not contain the inital create code migration, thus not creating any tables at all, and ofcourse failing at later migrations.
I tried executing update-database -targetmigration:initialcreate which gives me the message that that code migration does not exist, while it is a direct copy/paste from the cs file, so the id must be correct (note: this works for other migrations).
I also tried update-database -targetmigration:0 and update-database -targetmigration: $InitialDatabase which both give me 'Target database is already at version 0'.
I've also tried deleting the database altogether and let EF create it for, didnt work either, it keeps skipping the initialcreate migration.
So how do I get Code Migrations to execute my initalCreate code migration?
Working with EF migrations in a team scenario is not ideal to say the least. The best practice my team follows is to never commit migrations. Migrations are personal and apply only to your particular database instance. If everyone commits their own migrations you end up with a mess, quick.
While not directly related to your question, some may wonder how you deal with production migrations. Simply, you don't. Your Release Manager, or whoever will actually push the release live, should generate SQL to apply all the changes at once, and then hand this off to your DBA, or whoever manages the production database.
That said, in the scenario that you describe, where you database has been emptied. The best fix is to delete all migrations in your Migrations folder. Even and especially, the initial migration (they don't matter, anyways, because you shouldn't be holding on to them outside of your personal codebase). Then generate a new migration, which will trigger EF to compare your current database state (empty) with the state of the app and essentially create a new initial migration based on the current state of your app. Then, you can apply this migration.

Entity Framework 4 code-based migrations don´t work after calling CreateDatabaseIfNotExist

I have a MVC 3 project were I use code based migrations together mith automatic migrations(this works).
When I install this project on a new server the database is created by CreateDatabaseIfNotExist initializer, cause I´m using the seed method of this. After executing this I have a __MigrationHistory table with one entry. The model hash of this entry is exactly the same like the last one from my developement server. On my development server I have an entry for each of my code based migration in the __MigrationHistory table.
Now the problem is that when I try to run the migrations on the new server, I expected them to say to me "nothing to do, cause model hash is same", but instead of this the migrations seems to look only for the MigrationId in the database and try to execute every migration whose MigrationId is missing. Of course this leads to Exceptions, cause the migration tries to add database structures already there.
I think this should be a very common scenario, so is there a kind of workaround for this? My workaround for the moment is to copy all contents from the __MigrationHistory of the development system to the new server, but this is very tricky, due to the dealing with the modelhash as varbinary. Is there a better solution or did I understand some logical things wrong?

Resources