XML Schema (XSD) to Rails ActiveRecord Mapping? - ruby-on-rails

I'm looking for a way to convert an XML Schema definition file into an ActiveRecord modeled database. Does anyone know of a tool that happens to do this?
So far the best way I've found is to first load the XSD into an RDBMS like postgres or mysql and then have rails connect to do a rake db:schema:dump. This however, only leaves me with a database without rails Models.
What would be the best way to import/load this xsd based database into rails?

Did you try gem magic_model_generator to generate the model from the db?
See:
http://magicmodels.rubyforge.org/magic_model_generator/
Also, how did you load the XSD into your RDBMS? There seems to be plenty of discussion here:
How can I create database tables from XSD files?
Convert XSD into SQL relational tables
Update:
I used Xml Spy (30 free trail, on windows, ug) to build tables from the xsd in a mysql db, then ran magic_model_generator on it to create ActiveRecord classes. It appears to have worked as you would expect, generating validators on the fields based on the the db schema.

Related

How to write test for Ruby on Rails API that is built on an existing database?

I am trying to build a read-only Rails API that uses another Rails application's DB as its database.
So far I configured the read-only API's database.yml to point to the existing databases, generated models via rails g model but did not run the migration since I already have the corresponding tables in the existing databases.
I wanted to follow TDD approach and started to write tests but my problem is I cannot create instances of models since I did not create the table for that model.
Example Case
I generated a model named project which exists as a table in the existing DB that I am going to use to read data, but since I did not run the migration for that model it does not exist in the current API's DB.
So, I just dumped the schema.rb from the existing DB, copied it as my read-only API's db/schema.rb and run rake db:schema:load when RAILS_ENV=test.
I wonder if there's a better way to do this? Is my way of writing a read-only API like this is correct? I am open to any suggestions for this topic.
Cheers.

Rails Schema Design Software?

I am presently designing a database schema for use in a Rails 3.1 application.
At the moment, I am using MySQL Workbench to design the schema visually, and then manually translating this to Rails migrations & models.
Can anyone indicate if there are any solutions that will allow a schema to be designed visually and translated automatically (i.e. via script) to Rails?
Thanks!
First off, the "database-first" approach definitely isn't really the preferred way to work with Rails... but if you really want to...
If you generate the the tables from your schema you can configure the Rails app's config/database.yml file to connect to your database, then call rake db:schema:dump which generates the db/schema.rb file from the database. Then you can create a migration and copy the code from db/schema.rb into the change (or self.up) method.
Note that this does not automatically create model classes - you'll have to create these yourself, remembering to --skip migration in the rails generate model, and possibly needing to make liberal use of the set_table_name (to map the model class to the right table name), alias_attribute (to map model attributes to the right columns), and perhaps set_primary_key.
There were some more complete approaches to this sort of thing for older versions of Rails (Magic Model Generator and reverse_scaffold are two that I've found), but I don't know of any that have been upgraded to work with Rails 3.
SQL Editor is a Mac application that allows you to visually design a DB schema and then export it as something you can easily import as a schema in Rails.
You will still have to create the models yourself.
For rails existing applications or the new application, you can use this for db design
https://dbdiagram.io/
I use railshelper.com which helps me in a simplistic manner to generate quickly some common rails cli commands !
I think Mogwai ERD will help you out, there you can design your ERD and convert it in to a DB schema.
And I think there is no software for design Rails schema, it's just that you make your schema adhere to Rails conventions. But of course Rails can be configured to almost any schema.

Rails 3: Migrate DB Schema from SQLite to MongoDB

Is there an easy way to migrate DB Schema from SQLite (Development Env) and PostgreSQL (Heroku Production Env) to MongoDB?
"mongoimport" supports import of data through CSV or JSON format. If you can: export your data in one of those formats.
More complex data structure likely require some kind of migration/import script to be written by you.
I am currently in the process of setting up a new Rails app that will use mongoDB on the backend. I think that migrating your schema would depend on which ODM you choose to use. Personally, I choose Mongoid because it seemed to fit the best between Rails and mongo.
As far how to migrate your schema. I don't have an exact answer for you, but Mongoid seems to really implement the feature of ActiveRecord nicely and uses the same conventions. I would think this is something that you would just want to do manually.

Rails: transfer data from one schema to another

I'm migrating a PHP app to Rails. The new app has a significantly different schema.
Anybody have experience data from one schema to another? Right now, I'm looking at dumping CSV files and writing Ruby scripts to handle the insertion on the other side. I've also considered using Navicat to export/import to a temp database with the new schema (if it's simple enough), then dump the database and insert the values into a new database using db:seed.
I figure this is gonna be a total pain whichever way I go -- just hoping to minimize the angst. Thanks in advance!
UPDATE:
Decided to export from Navicat into XML, then use Nokogiri to create seed files for Seed_fu.
Check out Dr. Nic's Magic Model Gem. http://magicmodels.rubyforge.org/dr_nic_magic_models/
Then use rake tasks to iterate through your CSVs and insert using the newly generated models.

Rails: Proper work flow to shred XML data into relational SQL database

I have a Rails project and I used Migrations to setup the database schema (I'm using sqlite3). I have a XML file that I want to insert into my database. What's the best way to approach this? I'm thinking there's some Ruby script that I can write once and use to parse the XML file and insert it into my database, but intuitively it feels like this is a common problem and should have already been automated in the Ruby/Rails world. I guess some people would call this XML shredding, but querying Google hasn't turned up much for me.
Any thoughts?
db/seeds.rb could be a good place to put the data.

Resources