Easiest way to port a data model to Rails migration - mysql

I have designed a data model which has almost 24 tables. I have finished specifying the relations and all the data types are finalized. Now, I want to convert it to migrations in Rails.
I have all the scripts ready for it to be created in MySQL. Is there any tool that converts all the table creation queries into a single Rails migration file?
Thanks

I haven't done it myself but according to this post on the ruby forums
rake db:schema:dump
should be sufficient (delete the schema.rb beforehand).
The almost same question here on SO: Ruby / Rails - Reverse Migration - DDL to Ruby Code

Related

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.

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).

How to convert a Database from Sqlite3 to Mysql with Inconsistent Schema using Ruby

I'm working with a project that is transitioning from sqlite to mysql. Unfortunately the schema of individual tables has changed somewhat and a wholesale import won't work. Are there any good tools or solutions to do this?
I'm using ruby on rails 3.0.10 currently. My best guess was going to be initiate a console session, use namespaced active record adapters to connect to both databases, and then copy all the items over in code.
(the original database is fairly small i.e. less than a few thousand rows per table)
What might be easiest is to export the Sqlite database in a format that can be loaded back in to MySQL directly, then create migrations that alter the schema as required, starting with the Sqlite structure and adjusting from there.
One tool you might try is yaml_db to dump the one database into fixture-style YAML files, and then load it back in on the other side.
Remember that your schema.rb file should be sufficiently database agnostic so as to allow you to rake db:create when switching your database.yml adapter definition.
You might want to create a separate environment for the previous structure by adding a new entry to database.yml and config/environments so you can switch back and forth without having to hack at a few different config files all the time.

migration files and schema

So my Mac went down and now trying to build my project on my new Mac. I need to know when running rake db:migrate does it look at the schema.rb file?
As I am getting
Mysql::Error: Table 'myproject_development.users' doesn't exist: SHOW FIELDS FROM `users`
Even when I run rake db:migrate:up VERSION=001 which has no reference to users I get the same error?
If it matters my migrations start like
001_...
002_...
003_...
20100222171241_...
The migration code generally doesn't look at the schema file. It looks at the names of all the migration files and the database table called schema_migrations, and determines which migrations haven't been run yet. (I believe it does dump the schema at the end of running migrations.) Either your missing a migration, or your schema_migrations table is out of sync with your database.
After making a backup, you can start to troubleshoot. Do you have a migration that creates the users table? If not, where did it go? If you have it, why isn't it running?
FYI The migrations that start with numbers (eg. 001) are older versions of migrations. Sometime around 2.2 or 2.3 the names of the migrations were changed to dates, which is what you are seeing in the later migrations.
There may be a problem with the way those first few migrations are named, and they are not being found (I can't remember the migration story when this naming scheme switched). And maybe it tracked the migrations differently back then so there may be some futzing to make it work with the more modern scheme. You can fairly safely rename them following the new scheme, using the datestamp of the file.
Hope this helps.

Export MySQL Workbench data model directly to Schema YML in Propel/ Symfony

Is there any plugin that directly exports MYSQL Workbench data model directly to YML for Propel consumption?
Yes, MySQL Workbench Plugins are available for generating schemas for Propel, Doctrine, Symfony, etc
http://forums.mysql.com/read.php?153,208229
Just an update on this issue. If you are looking for a quick and convenient way to export your database tables to entities and mapping .yml files, there used to be a plugin for Workbench that would do this, but this LUA is not supported any more, unfortunately. It worked great -- too bad because if you use MySQL Workbench, a plugin would ideally be the most convenient and fastest way to export your database as entities into your Symfony project.
So, the next best solution I found is installing a utility that can be executed from Symfony's app/console called "mysqlworkbenchschemaexporter". With this utility, you will have to save your Workbench files (*.wmb) then upload them to a folder, then the following app/console commands are available:
app/console mysqlworkbenchschemaexporter:dump
app/console mysqlworkbenchschemaexporter:withRepository
Without buying an ORM tool like Skipper, which costs over $300, I did find this recently updated solution that is supported at:
https://github.com/turnaev/mysql-workbench-schema-exporter-symfony2-bundle
I hope this helps other Symfony developers save some time with entity creation and ORM mappings!
When using symfony 1.x, personally I prefer the following process:
design the model with workbench
use the "synchronize model" option to apply changes to the DB
run the propel:build-schema task to update the schema.yml
run the propel:build --all-classes task (it implies model, forms and filters)
Please note: step 3 will overwrite your entire schema.yml file. If you need to add special tweaks to it, just add a schema.custom.yml to your project and you're good to go.
If export from MySQL workbench isn't sufficient, you can try our tool Skipper - formerly ORM Designer (I'm chief developer). With Skipper you can define and export also behaviours, Propel specific column/table/... attributes and much more.
http://www.skipper18.com