how to create migration for already exist table in rails - mysql

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

Related

Typeorm: Is it possible to delete all migrations and recreate just one with my current entities?

I have a problem with TypeORM migrations telling me that some fields don't exist or there are duplicate keys when I try to run them in a new deployment.
Is it possible to delete all migrations and recreate just one with my current entities?
Stack:
TypeORM
NestJS
MySQL
Yes, it is possible to delete all migrations and recreate one.
Remove all migration files from your src/migrations folder.
Don't forget to delete data from migration table in your database. In typeorm, typically the migration table is called as typeorm_migrations.
Regenerate the migration file for your current entities. Use typeorm command npx typeorm migration:generate -n NameOfYourMigration.
After generating migration file, apply the changes to your database with npx typeorm migration:run

Use MySQL workbench to create tables and data for rails app

Can I use MySQL Workbench to create the tables and add data then import / connect that into my Rails app? My Rails app is already connected to MySQL. I am just wondering if I create tables in MySQL Workbench through the app_development schema if that will sync over to my Rails project or not and if there's a way to check to see if it worked?
You can use
rake db:schema:dump
to generate schema.rb from an existing database.
Having schema.rb, you can generate an empty initial migration and put create_table and other statements from generated schema there. Doing this will allow you to use all the relevant rake tasks just as if you developed your database "the Rails way" — via a series of migrations.

The Model tables in Django got deleted

The database table created automatically by django from model definition,i kind of deleted it using mysql command line.Now when i'm running a migration it says table doesn't exist .I used makemigrations,syncdb ,nothing works.How do i make django to create that table with those columns again..without me have to creating them manually.
Ok I resolved it, i deleted the migration files from app->migrations folder ,deleted the database from "mysql" .Now i created new migration using "makemigrations" command, and then applied it to my db using "migrate" command.

loading new schema in database without deleting existing data rails

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.

how to load and make app scaffold from database in rails

I am using mysql database with following tables and columns:
table masterProduct: (id,name,description, image).
everything is already set&ready in database.yml(connected with database) file, and in Gemfile (gem 'mysql').
When i do :rails new Product , i would like to load all the info i have in database but as scaffold, so i can edit/delete items which are already there or add new ones.
I've been looking on some tutorial like, http://www.tutorialspoint.com/ruby-on-rails/rails-scaffolding.htm but it didnt help me a lot, since he created a database manually, and i used PHPMADMIN.
Any sugestions?
Thanks,
Michael.
Manage your database manually is very bad idea. Rails has awesome features to do this like migrations, seed, etc. When you need update your db schema, you create a new migration to do that. This is the best and easy way. But...
Dynamic scaffold was removed in Rails 2. However you have some options:
1) You can try the gem activescaffold. Other gems like rails_admin or activeadmin are awesome to generate admin views.
2) When you execute
rails generate scaffold Product id:integer name:string description:text image:string
the generator will generate the scaffold files with a migration to create your table. So you can execute rake db:migrate to update your database schema. You can try execute this generate without execute the rake command once your table already exists. This will not create the file db/schema.rb and you will need to manager database manually (bad idea).