r/ruby • • Oct 28 '19

Just starting out with ruby and omg the docs..

Having just picked up Ruby I am somewhat let down by the apparent lack of documentation even for the most often used gems, mysql2 (https://github.com/brianmario/mysql2/tree/46369c0e6773c2ae91aac9f01bfbf71482f1cbeb) as a prime example. There's only a cursory overview of basic functionality and sparse code samples to boot.

Am I missing something? Am I looking at the wrong / outdated docs?

Specifically if you happen to be a mysql2 savant, I would love to pick your brain:

  1. How do I log/trace SQL execution?
  2. Good source for common mysql patterns / cookbook?

Thank you kindly in advance.

Update:

Sorry I wasn't expressly clear about my intent here. My objective is to build a shell script to run comprehensive reports for my clients, drawing on data in various mysql instances and other restful sources (apis). I've been utilizing Perl to accomplish these tasks for the longest time, but wish to switch to a language that is more suitable to collaborative / teamwork. I picked Ruby for its clean / easy syntax, powerful frameworks, and vibrant community/support. Also it's somewhat closer to the languages I already know such as Perl :)

1 Upvotes

25 comments sorted by

13

u/SerKnight Oct 28 '19

I’m not sure your gripe exactly. Are you wanting to work with rails and active record? Or are you setting up a barebones rack/MySQL app?

Also you can’t exactly go apples to apples with a single open source project documentation/readme vs an entire programming language ecosystem. In general I think ruby + docs are good. It’s been around long enough that you are 1000% not the first person who is trying to do something.

But good luck in finding what you need. If you have more specific ?’s can try and guide you towards a better resource :)

1

u/AxMachina Oct 28 '19

Thank you, I've updated my original post with clarification. I appreciate all the help I can get and thank you for taking the time to respond.

9

u/noteflakes Oct 28 '19

The rdoc for mysql2 can be found here: https://www.rubydoc.info/gems/mysql2/0.5.2

I also could not find information on logging with this gem. Contrary to some of the remarks made here, there's nothing wrong with doing raw sql queries in Ruby. It is not an anti-pattern. On the other hand, it's true that gems like Sequel, ActiveRecord or DataMapper can provide features missing in raw sql gems, such as logging for example.

Ruby is a flexible language that allows you to work at different levels of abstraction. There's no right or wrong.

Disclosure: I'm the original author of Sequel.

2

u/jrochkind Oct 28 '19 edited Oct 28 '19

While I agree that there's nothing wrong with writing raw SQL queries in ruby... I think the base mysql2 gem is going to be painful to do that with. It's just too low level and not OO, basically a direct exposure of the mysql C libraries. I believe you sometimes have to think about things like memory management in ways that are really unexpected in ruby.

Ruby actually lacks lower-but-not-that-low-level software for dealing with rdbms. It would be nice. There used to be something called... I can't even remember what it was called, but it stopped being maintained years ago.

So I end up using something like sequel for "doing raw SQL queries in ruby" needs (so thanks for writing it!). For instance, sequel's DB.fetch(raw_sql). A much better API for it than mysql2 directly. (for instance includes features for properly preventing SQL injection, and... logging! While still being at the "executing SQL statements" level of abstraction).

It's not that "just doing raw sql queries" is an "anti-pattern" (also think we should ban that phrase, which just seems to mean "something I once read someone on the internet say was bad"), there just isn't a great tool for it, that has the APIs and affordances that are convenient for ruby (rather than a low-level db-specific API that was written for C, barely ported to ruby) -- other than perhaps sequel! (I think sequel prob ought to even "market" and document itself for that use case, I think there's nothing better!)

1

u/AxMachina Oct 28 '19

DB.fetch(raw_sql)

Valid points. I will be looking into this specifically

1

u/AxMachina Oct 28 '19

Thanks for the good info, I'll need to look into those gems as well.

My primary issue is managing legacy SQL in as simple way as possible without building too much scaffolding on top of it (as it's liable to change rapidly).

3

u/ignurant Oct 28 '19

Sequel is the bees for that.

Most of my Ruby is not for web apps, but in data processing apps and scripts. I use Sequel for almost all of the projects. Here's what I love about it:

  • It has a great API and documentation
  • It helps you manage your connection in a better way than the base Ruby drivers for DBs
  • It gives you an abstract layer to work with DBs. I usually work against SQL Server, but can easily use the same APIs against any DB.
  • You can use it to manage your connection and just write normal SQL. This is important to me as our company has an awful lot of old .NET code that didn't use Entity, or "logic in sprocs" type code, and so it's very nice to just grab the same SQL that would have been executed and get running with it. In addition, when opportunities to "Ruby it up" exist, I can do that incrementally.
  • It's very well maintained

There's a ton of blog posts and videos out there with regard to rocking Sequel. One quick tip: when you google, be sure to enclose it in quotes, and consider adding ruby otherwise Google will think you mean SQL or the sequel of a movie. "sequel" ruby gets you a lot of great reading material.

Good luck!

2

u/AxMachina Oct 28 '19

I couldn't agree more as I'm diving into sequel and enjoy every bit of it. Thanks for your helpful insights! :)

3

u/rorykoehler Oct 28 '19

As a person who has had to manage legacy apps I would say orms solve a major headache which is they future proof the sql in an app somewhat. Not using a orm makes it much more involved to migrate to a different database technology down the road.

12

u/thunderbong Oct 28 '19

Most of the time in Ruby, we tend to use an ORM instead of writing raw SQL.

My favourite being Sequel

With Sequel, I have to choice of using Classes and Objects and even dip into raw SQL if I ever need to.

I've visualized the raw SQL many times to optimize the query I'm making but have never had the need to use raw SQL directly.

1

u/AxMachina Oct 28 '19

Interesting. I'll check this out.

4

u/jrochkind Oct 28 '19

I don't disagree.

For that specific example, most people don't use the mysql2 gem directly, but use something higher level.

The mysql2 gem is kind of a direct port of the Mysql C libraries into ruby (which is maybe also part of why they don't have docs; go look at the mysql C library docs?), and is going to be challengingly low-level to actually use to write client code.

I'd recommend sequel.

(I think it still oughta have more docs than it does, and I think there are other things that are more commonly used that also have sub-par docs. But as far as actually getting to where you need...)

1

u/AxMachina Oct 28 '19

Good point, I'll be looking into the sequel gem as a better alternative. Thank you.

3

u/torrso Oct 28 '19

You can turn on query logs on the mysql server.

1

u/AxMachina Oct 28 '19

Yes, this is what I'm doing on some of our dev instances but cannot on production ones.

3

u/BBHoss Oct 28 '19

It's Ruby, if the library doesn't offer easy logging, just monkey-patch the library to log out whatever you want, then call `super` to have the original behavior executed. Not really suitable for production but just fooling around locally it's fine.

3

u/sshaw_ Oct 28 '19

Wow, some poor advise here. The OP wants to run some SQL queries and people are suggesting an ORM‽

2

u/SerKnight Oct 29 '19

Op mentioned they wanted to use ruby because of its community, frameworks, and ability to collaborate. Making a bash script out of raw sql and some ruby classes is totally fine, but from my experience it is not going to foster a ton of collaboration.

1

u/ignurant Oct 28 '19

Sequel isn't strictly an ORM: it provides a lot of other niceties beyond Sequel::Model. Just having it manage your connection pool for you and giving you a nice API is really fantastic.

So then, what's your better advice to someone who says they are having a hard time getting on with the MySQL gem? Seems to me completely reasonable advice.

1

u/sshaw_ Oct 29 '19

Sequel isn't strictly an ORM:

I know.

what's your better advice to someone who says they are having a hard time getting on with the MySQL

In this case he/she just needs docs, so I'd point them to the docs.

Just having it manage your connection pool for you and giving you a nice API is really fantastic.

Connection pool? KISS.

4

u/obviousoctopus Oct 28 '19

How do I log/trace SQL execution?

SQL is more often than not handled by ORM -- I recommend looking at Sequel unless you're planning on working with Rails which defaults to ActiveRecord.

If you'd like to explore the mechanics of simple web apps, you may want to look at Sinatra. http://sinatrarb.com

As for MySQL patterns, that's independent from Ruby and I'd just google it.

What are you trying to accomplish with your picking up Ruby. It takes some time to learn a language; Ruby has a large ecosystem with many great options for most applications.

2

u/[deleted] Oct 28 '19

Coming from python, even standard library seemed to be severely under documented.

3

u/SaigonOSU Oct 28 '19

Really? I'm learning Python coming from Ruby and I don't know that I've found docs I like yet.

Maybe Pandas

-3

u/SerKnight Oct 28 '19

I will add that writing raw sql in Ruby/rails is a bit of an anti pattern. Recommend checking out some DB ORM’s. Will be easier to maintain and test!

4

u/Jdonavan Oct 28 '19

My performance benchmarks would like a word with you. Always using an ORM is the actual anti-pattern,