I have situation here, I have a database in production environment, and now I have to add few columns in table and drop 1 table from the database. I ran all the migrations such as rails g migration AddDeskToHelper desk:string and similar for dropping a table.
But now I want to upload this new generated schema into existing database without deleting any data from the database.
I'm searching about it and did not find any accurate answer.
For example, here
it says that rake db:schema:load deletes all the data from Database in production environment.
Kindly help me in this case.
If you have created the migrations to add some cols in existing tables or delete some tables, the rake db:migrate RAILS_ENV=production command will perform an "ALTER table..." without destroying your data, except you are dropping a col or table with data content.
But it's always recommended to perform a database backup before altering your production database.
Related
I have added foreign keys to some of the models in a Django application but the changes are not being reflected in the MySQL database even after migrating them. I have tried deleting all the previous migrations and then redoing but had no success. What could be the issue here?
that could have many reasons which i need more details to say exact answer, but i can say my previous experiences that solved migrations error. first if you want to do migrations again you should delete all tables plus your migrations files ( IMPORTANT NOTE: you should not delete migrations folder and you should not delete __ init __.py file in migrations folder which make migrations folder a module if you deleted it just make an empty one yourself) and sometimes because of an error in your models, your migrations does not completely migrate so if you migrate again it say some tables are duplicated so you should fix the errors and bugs, delete all tables and migrations like i just said and migrate again.
Before I was trying to run rake db:migrate I ran into an error called column duplication so I ran rake db:migrate VERSION=0 instead and ran rake db:migrate again.
Basically I was clearing up my previous migration and by running a new migration from VERSION=0, I was finally able to finish my pending migration and had no issue with column duplication again.
But here's the thing, the command I ran actually cleared up all my previous data. Is there a way to retrieve what I had lost? And what is the safest way to run db:migrate if there's a column duplication error. I really don't want to go back to VERSION=0
Is there a way to retrieve what I had lost?
You deleted your database table and re-created it. All data that was stored in that table was also deleted and is lost. The only way to get your data back is to restore from a backup. Do you have a recent database backup?
And what is the safest way to run db:migrate if there's a column duplication error.
If you migration has an error, fix that migration and re-run it. In you example: If your migration fails with an error telling you that there is a duplicate column, just remove the code that tries to add a duplicate column and re-run the migration.
rails migration has a strict process to be followed. Suppose you have duplicate column. You should write another migration file to remove that duplicate column.
When you have ran
rails db:migrate VERSION=0
It is creating your database all again so obvious that data will be lost.
So make sure any correction in Database used in Rails should follow strict process of creating new migration rather than editing the same.
I have deleted the migration files in rails project. Now i want run the rails project in another system for that i need the migration files to create the table. please tell me how to create the migration files for already created tables in rails.
You still have db/schema.rb file containing all tables and their details..you can use it by rake db:schema:load,this will load the schema into the database, which is faster than running all the migrations.
NEVER DELETE YOUR MIGRATION AND MAINTAIN IT IN SUBVERSION/GIT
hi everybody i'm new to Ruby on Rails...
I created a Rails Application for that i created a database called 'cart' in MySQL database by using rake command. but unfortunately my database got crashed. right now there are no databases including older ones also deleted.
please help me, how can i get my all databases from MySQL s/w. is there any command to restore the crashed data.
Thanks
If you don't have backup it could be hard. You can remove database, create another and rake db:migrate but it will only create a structure for you and will not restore data.
You can try to reproduce your records from rails logs but it will be a lot of work
What do you mean by database crashed?
I have created an app using a badly named database, all alterations to important data in the database have been done in fixtures so that I can just drop the database, recreate it and then rake migrate the database tables and fill them with the initial data from the fixtures.
I would like to change the name of my database now, so I updated the database.yml file to reflect another database name. I created the database in mysql and then tried to run the migration and fixtures.
Running the migration with trace shows that it is running commands to create tables etc. However once I am finished I get errors in my application saying that the tables don't exist in the new database.
I go into mysql and check the database and it is completely empty. I have tried wiping everything and running the migrations a few times but nothing changes. Is there something I am missing?
I don't know what commands you used to do the migration, but to migrate a production database (which I infer from the tag, "production-environment"), you have to do:
RAILS_ENV=production rake db:migrate
If that's not the answer, then please provide the command you used to perform the migration, which database you expected to be affected, and the relevant bits of database.yml.