Monday, December 15, 2008

data migration from YAML using ruby

When I was given a task to implement YAML, I did not know how to implement and even did not get any clue to proceed. The prime concern was to give our successful first release. At last I have only a weekend and my lead has postpone this YAML implementation to the next build and told me to implement this concept with an demo application which is to be given with the release.

She even not expecting this to be implemented successful but the expectation was that I should be answerable on this topic at the time of demo.


Well I had to work whole weekend night and had to spend complete two days alone in the office, even my team was not aware about this implementation.


Monday I told to my lead and she was so surprised about you not even implemented the YAML successfully but you have made changes to the others 25 module.


The reaction from the lead was made me happy and encouraged a lot.

I just want to share some little concept what I have done with YAML.


YAML is a straightforward machine parsable data serialization format designed for human readability and interaction with scripting languages such as Perl and Python.


Early in its development, YAML was said to mean "Yet Another Markup Language", but was retronymed to distinguish its purpose as data-centric, rather than document markup.


So the task is to make a data structure in the YAML which returns an array of hash.



Data.yml

-

suite_file: "test_status_codes_test_suite.rb"

flag: "0"

-

suite_file: "testing_types_test_suite.rb"



The Most important this is to take care of spaces. YAML file does not support tabs. And you need to follow the same space convention through out the file.


‘-‘ creates an array element.

I have given the two white spaces to create an hash key-value within the array element.


Now you want to remove the iteration from your ruby code so you want a hash instead of array.



Code.yml



TCM_SS_MTSC_1:

desc: "Hi vaibhav."

TCM_SS_MTSC_3:

desc: "Good luck team!."

TCM_SS_MTSC_2:

desc: "done a good job."

TCM_SS_MTSC_4:

desc: "you are owe-some"

flag: "0"



Above is the hash of hash, you will get it like:


{: TCM_SS_MTSC_1 => { :desc => "Hi vaibhav."}, : TCM_SS_MTSC_3 => { :desc =>"Good luck team!."}}


Again take care of spaces otherwise it will give you an unidentifiable error at run time.

Finally the question is how to load the YAML File and use the data


J

So friends lets have a look:



$Data = YAML::load(File.read('config/Data.yml'))


Now whatever I have specified in the data.yml file I will get as in a data structure.

You should prefer to use YAML file when you need to migrate data into database. So dear enjoy playing with YAML.


J

Good luck

3 comments:

  1. Hey Vaibhav..
    I have gone through the things u mention over here. Its awesome. In just a couple of steps you provided the things with whole concept.

    It would be better if you provide eith some demo code fragments and highlight some of important thing that one should consider playing with the those concepts.

    IN all that a gud post.. I m really impressed

    "Mahesh Chand Yadav"

    ReplyDelete
  2. Hey nice peace of work, I will use them. you are just superb with these concept


    keep up the good work

    -shikha agarwal

    ReplyDelete
  3. I believe that this is one of the best materials ever i had gone through for automation perspective. Special tip provided by you are outstanding and will be helping anyone if s/he goes through this blog.

    Keep on with these kind of most precious stuffs about automation and other things.

    * Ashish Kumar Shriwastav *

    ReplyDelete