Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

translate string to array #13

Closed
guruward opened this issue Nov 16, 2012 · 16 comments
Closed

translate string to array #13

guruward opened this issue Nov 16, 2012 · 16 comments

Comments

@guruward
Copy link

in the mysql db I have an array stored as a string example "[3.3,3.2,3,2.8]"
i would like to translate that so its stored just as an array {myArray: [3.3,3.2,3,2.8] }

is this possible ?

@anlek
Copy link
Owner

anlek commented Nov 16, 2012

Yes, you can do this using the before_save. Example:

table "user" do                                     
  column "id", :key
  column "first_name", :string
  column "last_name", :string
  column "created_at", :datetime
  column "updated_at", :datetime
  column "post_ids", :string          # Post_ids looking like [1,2,8,19]
  before_save do |row|                                    
    row.post_ids = eval(row.delete('post_ids')) unless row['post_ids'].blank?
  end
end              

that should change the post_ids from a string to an array in MongoDB.

However you're currently locked into that table, I'm planning to rewrite Mongify and might allow you to do something like:

#NOT YET POSSIBLE
  before_save do |row|                                    
    return if row['post_ids'].blank?
    ids = eval(row.delete('post_ids'))
    ids.each do |id|
      self.db['posts'].insert(id: id) #using mongo-ruby-driver DSL
    end
  end

Let me know if this helps.

@guruward
Copy link
Author

thanks though I must be doing something wrong here is the snippet from the translation file

table "isodesign_product_data" do
column "id", :key, :as => :integer
column "isodesign_product_id", :integer, :references => "isodesign_products"
column "thickness", :string
column "prepreg", :string
column "resin", :integer
column "dk", :string
before_save do |row|
row['dk'] = eval(row.delete('dk')) unless row['dk'].blank?
end
column "df", :string
column "p_name", :string
end

and here is the error
mongify process translation_file.rb -c database.config
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/mongify-0.3/lib/mongify/database/data_row.rb:62:in method_missing': undefined method[]' for #Mongify::Database::DataRow:0x007f96944c64b8 (NoMethodError)
from (eval):10:in block (2 levels) in parse' from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/mongify-0.3/lib/mongify/database/table.rb:182:incall'
from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/mongify-0.3/lib/mongify/database/table.rb:182:in run_before_save' from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/mongify-0.3/lib/mongify/database/table.rb:131:intranslate'
from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/mongify-0.3/lib/mongify/translation/process.rb:47:in block (2 levels) in copy_data' from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/mongify-0.3/lib/mongify/translation/process.rb:46:ineach'
from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/mongify-0.3/lib/mongify/translation/process.rb:46:in block in copy_data' from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/mongify-0.3/lib/mongify/translation/process.rb:43:ineach'
from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/mongify-0.3/lib/mongify/translation/process.rb:43:in copy_data' from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/mongify-0.3/lib/mongify/translation/process.rb:22:inprocess'
from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/mongify-0.3/lib/mongify/cli/worker_command.rb:67:in execute' from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/mongify-0.3/lib/mongify/cli/application.rb:28:inexecute!'
from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/mongify-0.3/bin/mongify:14:in <top (required)>' from /usr/local/rvm/gems/ruby-1.9.3-p194/bin/mongify:19:inload'
from /usr/local/rvm/gems/ruby-1.9.3-p194/bin/mongify:19:in <main>' from /usr/local/rvm/gems/ruby-1.9.3-p194/bin/ruby_noexec_wrapper:14:ineval'

On Nov 16, 2012, at 3:59 PM, Andrew Kalek <[email protected]mailto:[email protected]>
wrote:

Yes, you can do this using the before_save. Example:

table "user" do
column "id", :key
column "first_name", :string
column "last_name", :string
column "created_at", :datetime
column "updated_at", :datetime
column "post_ids", :string # Post_ids looking like [1,2,8,19]
before_save do |row|
row.post_ids = eval(row.delete('post_ids')) unless row['post_ids'].blank?
end
end

that should change the post_ids from a string to an array in MongoDB.

However you're currently locked into that table, I'm planning to rewrite Mongify and might allow you to do something like:

#NOT YET POSSIBLE
before_save do |row|
return if row['post_ids'].blank?
ids = eval(row.delete('post_ids'))
ids.each do |id|
self.db['posts'].insert(id: id) #using mongo-ruby-driver DSL
end
end

Let me know if this helps.


Reply to this email directly or view it on GitHubhttps://github.com//issues/13#issuecomment-10465665.

@anlek
Copy link
Owner

anlek commented Nov 17, 2012

Sorry, It should be a . (dot) not []. So you should be doing:

row.dk = eval(row.delete('dk')) unless row.dk.blank?

Hope that helps

@guruward
Copy link
Author

Thanks

Sent from my iPhone

On Nov 16, 2012, at 6:01 PM, "Andrew Kalek" <[email protected]mailto:[email protected]> wrote:

Sorry, It should be a . (dot) not []. So you should be doing:

row.dkhttp://row.dk = eval(row.delete('dk')) unless row.dk.blank?

Hope that helps


Reply to this email directly or view it on GitHubhttps://github.com//issues/13#issuecomment-10468549.

@guruward
Copy link
Author

Worked !! Thanks for mongify nice product

Sent from my iPhone

On Nov 16, 2012, at 6:01 PM, "Andrew Kalek" <[email protected]mailto:[email protected]> wrote:

Sorry, It should be a . (dot) not []. So you should be doing:

row.dkhttp://row.dk = eval(row.delete('dk')) unless row.dk.blank?

Hope that helps


Reply to this email directly or view it on GitHubhttps://github.com//issues/13#issuecomment-10468549.

@anlek
Copy link
Owner

anlek commented Nov 18, 2012

I'm glad it helped and thanks for the kind words!
Spread the word!

@anlek anlek closed this as completed Nov 18, 2012
@guruward
Copy link
Author

Will do ! I'm holding a mongo user group in jan here in Phoenix

Sent from my iPhone

On Nov 18, 2012, at 11:51 AM, "Andrew Kalek" <[email protected]mailto:[email protected]> wrote:

I'm glad it helped and thanks for the kind words!
Spread the word!


Reply to this email directly or view it on GitHubhttps://github.com//issues/13#issuecomment-10489681.

@guruward
Copy link
Author

Hi Andrew , I am spreading the word ! Im having a MongoDB user group meeting Jan. 10 here in Tempe Arizona.
If you have anything in particular you want me to show let me know.

By the way I see your in Toronto , That is my home town . I was the Ward in Ward Consulting.


From: Andrew Kalek [[email protected]]
Sent: Sunday, November 18, 2012 11:50 AM
To: anlek/mongify
Cc: Steve Ward
Subject: Re: [mongify] translate string to array (#13)

I'm glad it helped and thanks for the kind words!
Spread the word!


Reply to this email directly or view it on GitHubhttps://github.com//issues/13#issuecomment-10489681.

@anlek
Copy link
Owner

anlek commented Dec 10, 2012

Hey Stephen,
Thank you for doing that, it would be awesome!

As for what to show, I think it's cool to show real data being moved, however it's hard to find data that's safe to show off. So you can check out the source code, under the tests, there is a small DB setup (or it might be being generated via code) of a user, preferences, posts and comments. You can try showing off that database.

I might still have the files from what I did the 5 minute lighting talk with, if I find them, I'll provide you with a download link.

Thanks again

Andrew

@guruward
Copy link
Author

I can show real data I've got a project of my own ill use.

Sent from my iPhone

On Dec 10, 2012, at 9:21 AM, "Andrew Kalek" <[email protected]mailto:[email protected]> wrote:

Hey Stephen,
Thank you for doing that, it would be awesome!

As for what to show, I think it's cool to show real data being moved, however it's hard to find data that's safe to show off. So you can check out the source code, under the tests, there is a small DB setup (or it might be being generated via code) of a user, preferences, posts and comments. You can try showing off that database.

I might still have the files from what I did the 5 minute lighting talk with, if I find them, I'll provide you with a download link.

Thanks again

Andrew


Reply to this email directly or view it on GitHubhttps://github.com//issues/13#issuecomment-11203444.

@guruward
Copy link
Author

guruward commented Jan 4, 2013

Andrew the Phoenix MongoDB user group is Thursday next week. please send me any material you want me to present ASAP

Thanks

On Dec 10, 2012, at 9:21 AM, Andrew Kalek <[email protected]mailto:[email protected]> wrote:

Hey Stephen,
Thank you for doing that, it would be awesome!

As for what to show, I think it's cool to show real data being moved, however it's hard to find data that's safe to show off. So you can check out the source code, under the tests, there is a small DB setup (or it might be being generated via code) of a user, preferences, posts and comments. You can try showing off that database.

I might still have the files from what I did the 5 minute lighting talk with, if I find them, I'll provide you with a download link.

Thanks again

Andrew


Reply to this email directly or view it on GitHubhttps://github.com//issues/13#issuecomment-11203444.

@anlek
Copy link
Owner

anlek commented Jan 6, 2013

Hey, I've reviewed my stuff and I don't have anything left from that video,
but it shouldn't be too hard for you to mock up a db to show the features,
you can try to duplicate what I have in the video. Let me know if you have
any issues and sorry I couldn't help any more. :(

Andrew Kalek
Anlek Consulting
CAN: 647.833.3852
CAN: 289.999.5516
US: 909.ANLEK.IT (909.265.3548)
http://www.anlek.com

On Fri, Jan 4, 2013 at 3:57 PM, Stephen Ward [email protected]:

Andrew the Phoenix MongoDB user group is Thursday next week. please send
me any material you want me to present ASAP

Thanks

On Dec 10, 2012, at 9:21 AM, Andrew Kalek <[email protected]
mailto:[email protected]> wrote:

Hey Stephen,
Thank you for doing that, it would be awesome!

As for what to show, I think it's cool to show real data being moved,
however it's hard to find data that's safe to show off. So you can check
out the source code, under the tests, there is a small DB setup (or it
might be being generated via code) of a user, preferences, posts and
comments. You can try showing off that database.

I might still have the files from what I did the 5 minute lighting talk
with, if I find them, I'll provide you with a download link.

Thanks again

Andrew


Reply to this email directly or view it on GitHub<
https://github.com/anlek/mongify/issues/13#issuecomment-11203444>.


Reply to this email directly or view it on GitHubhttps://github.com//issues/13#issuecomment-11899469.

@Komalil
Copy link

Komalil commented Mar 24, 2017

Hello Anlek,

Is it possible to migrate whole table with out specifying column names in translation.rb file?
tried by keeping the below command in translation.rb file. but it is not inserting none of the records from mysql to mongo.

table "Test".
Would you help me to resolve the issue?

@anlek
Copy link
Owner

anlek commented Mar 27, 2017

Hey @Komalil,
It is not an issue but a feature. Mongify ignores any columns you don't specify in the translation (as it wouldn't know how to translate them).
I'm not exactly sure why you don't want to use a translation, you can generate the translation table automatically via the mongify translation database.config > translation.rb command.

Good luck,
Andrew

@Komalil
Copy link

Komalil commented Mar 27, 2017 via email

@anlek
Copy link
Owner

anlek commented Mar 27, 2017

I'm going to reply to this in #133 as it's more relevant there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants