Script to interact with a database

Discussion in 'Ruby' started by Quee WM, Jan 6, 2010.

  1. Quee WM

    Quee WM Guest

    I have an application in rails in which i am adding a new column in one
    of the tables. The value for the new column is a bit.ly link and I need
    to populate this for each record in the table approx. 250 rows will be
    affected.

    Q1: Can this be done outside the application? i.e. write a script that
    can interact with the db and then populate the appropriate column? And
    is this advisable?

    or

    Q2: if the aforementioned idea is not a good way then what will be the
    best way to update the existing records as any code i will write will
    only be used to update the table once.
    --
    Posted via http://www.ruby-forum.com/.
     
    Quee WM, Jan 6, 2010
    #1
    1. Advertising

  2. Quee WM wrote:
    > I have an application in rails in which i am adding a new column in one
    > of the tables. The value for the new column is a bit.ly link and I need
    > to populate this for each record in the table approx. 250 rows will be
    > affected.
    >
    > Q1: Can this be done outside the application? i.e. write a script that
    > can interact with the db and then populate the appropriate column? And
    > is this advisable?
    >
    > or
    >
    > Q2: if the aforementioned idea is not a good way then what will be the
    > best way to update the existing records as any code i will write will
    > only be used to update the table once.


    Sounds like you should be posting this in the Rails forum / newsgroup.
    Also sounds like you should look into migrations.
    --
    Posted via http://www.ruby-forum.com/.
     
    Aldric Giacomoni, Jan 6, 2010
    #2
    1. Advertising

  3. Quee WM

    Quee WM Guest

    Aldric Giacomoni wrote:

    >
    > Sounds like you should be posting this in the Rails forum / newsgroup.
    > Also sounds like you should look into migrations.


    I shall try posting to the rails forum, but i would still very much like
    to learn how a script can interact with a database using active record.
    and any pointers in that directions will be greatly helpful.
    --
    Posted via http://www.ruby-forum.com/.
     
    Quee WM, Jan 6, 2010
    #3
  4. Quee WM wrote:
    > Aldric Giacomoni wrote:
    >
    >>
    >> Sounds like you should be posting this in the Rails forum / newsgroup.
    >> Also sounds like you should look into migrations.

    >
    > I shall try posting to the rails forum, but i would still very much like
    > to learn how a script can interact with a database using active record.
    > and any pointers in that directions will be greatly helpful.


    That can also be better answered in the Rails forum, I think.

    Best,
    -- 
    Marnen Laibow-Koser
    http://www.marnen.org

    --
    Posted via http://www.ruby-forum.com/.
     
    Marnen Laibow-Koser, Jan 6, 2010
    #4
  5. Quee WM wrote:
    > Aldric Giacomoni wrote:
    >
    >>
    >> Sounds like you should be posting this in the Rails forum / newsgroup.
    >> Also sounds like you should look into migrations.

    >
    > I shall try posting to the rails forum, but i would still very much like
    > to learn how a script can interact with a database using active record.
    > and any pointers in that directions will be greatly helpful.


    Migrations, as I mentioned, are excellent for one-time work on tables,
    including addition of columns and data.
    --
    Posted via http://www.ruby-forum.com/.
     
    Aldric Giacomoni, Jan 6, 2010
    #5
  6. Quee WM

    Jim Maher Guest

    Quee WM wrote:
    > I have an application in rails in which i am adding a new column in one
    > of the tables. The value for the new column is a bit.ly link and I need
    > to populate this for each record in the table approx. 250 rows will be
    > affected.
    >
    > Q1: Can this be done outside the application? i.e. write a script that
    > can interact with the db and then populate the appropriate column? And
    > is this advisable?
    >
    > or
    >
    > Q2: if the aforementioned idea is not a good way then what will be the
    > best way to update the existing records as any code i will write will
    > only be used to update the table once.



    Can you update the database outside of your Rails application? Yes.

    Should you? Many would say NO, but in practice I have never yet
    encountered an app that was ALWAYS updated only with the app. The DB
    itself, various utilities, one-time scripts, external apps (e.g., Excel,
    etc.) are often used. Be careful, have a backup, be careful again,
    test, then be VERY careful.

    Using a plain Ruby script to interact with the datavbase is certainly
    possible, and not necessarily using ActiveRecord. You might consider
    using something like Sequel (v3.8.0), which is a database toolkit for
    Ruby (I have NOT used this myself).

    But, again, be VERY careful. Any updates you do outside your app may
    NOT conform to your business rules. That's up to you.

    j
    --
    Posted via http://www.ruby-forum.com/.
     
    Jim Maher, Jan 6, 2010
    #6
  7. On Jan 6, 2010, at 4:51 PM, Quee WM wrote:

    > I have an application in rails in which i am adding a new column in =

    one
    > of the tables. The value for the new column is a bit.ly link and I =

    need
    > to populate this for each record in the table approx. 250 rows will be
    > affected.
    >=20
    > Q1: Can this be done outside the application? i.e. write a script that
    > can interact with the db and then populate the appropriate column? And
    > is this advisable?
    >=20
    > or
    >=20
    > Q2: if the aforementioned idea is not a good way then what will be the
    > best way to update the existing records as any code i will write will
    > only be used to update the table once.


    Sure. If you use the "script/runner"-Script instead of the plain Ruby =
    interpreter, your script will run in the fully loaded application =
    environment. For one-shot scripts that are not exactly migrations, this =
    is an acceptable way to do this. So:

    script/runner -e development path/to/my/script.rb

    Regards,
    Florian=
     
    Florian Gilcher, Jan 6, 2010
    #7
  8. Jim Maher wrote:
    > Quee WM wrote:
    >> I have an application in rails in which i am adding a new column in one
    >> of the tables. The value for the new column is a bit.ly link and I need
    >> to populate this for each record in the table approx. 250 rows will be
    >> affected.
    >>
    >> Q1: Can this be done outside the application? i.e. write a script that
    >> can interact with the db and then populate the appropriate column? And
    >> is this advisable?
    >>
    >> or
    >>
    >> Q2: if the aforementioned idea is not a good way then what will be the
    >> best way to update the existing records as any code i will write will
    >> only be used to update the table once.

    >
    >
    > Can you update the database outside of your Rails application? Yes.
    >
    > Should you? Many would say NO, but in practice I have never yet
    > encountered an app that was ALWAYS updated only with the app.


    You should see my apps, then. Unless the DB is corrupted (which has
    only happened once), I usually use script/console for data tweaking.

    > The DB
    > itself, various utilities, one-time scripts, external apps (e.g., Excel,
    > etc.) are often used. Be careful, have a backup, be careful again,
    > test, then be VERY careful.


    Yes. And this is one reason that I always recommend having data
    integrity constraints in the DB, not just the app.

    >
    > Using a plain Ruby script to interact with the datavbase is certainly
    > possible, and not necessarily using ActiveRecord. You might consider
    > using something like Sequel (v3.8.0), which is a database toolkit for
    > Ruby (I have NOT used this myself).


    I have. But in a Rails app such as the OP has, it is safer not to do
    this.

    >
    > But, again, be VERY careful. Any updates you do outside your app may
    > NOT conform to your business rules. That's up to you.
    >
    > j


    Best,
    -- 
    Marnen Laibow-Koser
    http://www.marnen.org

    --
    Posted via http://www.ruby-forum.com/.
     
    Marnen Laibow-Koser, Jan 6, 2010
    #8
  9. Quee WM

    Josh Cheek Guest

    [Note: parts of this message were removed to make it a legal post.]

    On Wed, Jan 6, 2010 at 9:51 AM, Quee WM <> wrote:

    > I have an application in rails in which i am adding a new column in one
    > of the tables. The value for the new column is a bit.ly link and I need
    > to populate this for each record in the table approx. 250 rows will be
    > affected.
    >
    > Q1: Can this be done outside the application? i.e. write a script that
    > can interact with the db and then populate the appropriate column? And
    > is this advisable?
    >
    > Q2: if the aforementioned idea is not a good way then what will be the
    > best way to update the existing records as any code i will write will
    > only be used to update the table once.
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >

    I'd make a method in your model that gets the address, then have it get that
    address upon creating a record. For all the records currently in there, just
    go to the console, iterate through each, and invoke that method on them.

    To make the method that updates it, you can use the api, or a gem:
    http://code.google.com/p/bitly-api/wiki/ApiDocumentation
    http://gemcutter.org/search?query=bit.ly

    For a 1 time edit with 250 records, I'd use the console script/console , for
    recurring edits, I'd create a rake task
    http://railscasts.com/episodes/66-custom-rake-tasks

    If you have a large number of records, you might need to do something
    different, but I'm not sure how to handle that.

    Sometimes, due to constraints within your app's model, you need to edit the
    db directly, I use sequel pro http://www.sequelpro.com but this really
    almost never occurs (or at least shouldn't).
     
    Josh Cheek, Jan 6, 2010
    #9
  10. Quee WM

    Quee WM Guest

    Thanks a lot for the help. seems the Rails group wins as they made a
    strong point about using the migration to take care of this task. Will
    play with code there and see what happens.

    Once again thanks.

    Q

    Florian Gilcher wrote:
    > On Jan 6, 2010, at 4:51 PM, Quee WM wrote:
    >
    >>
    >> Q2: if the aforementioned idea is not a good way then what will be the
    >> best way to update the existing records as any code i will write will
    >> only be used to update the table once.

    >
    > Sure. If you use the "script/runner"-Script instead of the plain Ruby
    > interpreter, your script will run in the fully loaded application
    > environment. For one-shot scripts that are not exactly migrations, this
    > is an acceptable way to do this. So:
    >
    > script/runner -e development path/to/my/script.rb
    >
    > Regards,
    > Florian


    --
    Posted via http://www.ruby-forum.com/.
     
    Quee WM, Jan 7, 2010
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. KathyB
    Replies:
    0
    Views:
    386
    KathyB
    Jul 4, 2003
  2. Chason Hayes
    Replies:
    2
    Views:
    286
    Méta-MCI
    Mar 20, 2006
  3. Sascha
    Replies:
    4
    Views:
    682
  4. Venks
    Replies:
    9
    Views:
    169
    Thomas Enebo
    Dec 5, 2007
  5. rajendra

    perl script to interact with PC port

    rajendra, Apr 23, 2008, in forum: Perl Misc
    Replies:
    1
    Views:
    95
    RedGrittyBrick
    Apr 23, 2008
Loading...

Share This Page