Monthly Archives: December 2013

Functional Geekery

In a previous post I mentioned I had a new project in the works. I was a guest on the Ruby Rogues podcast and made an announcement there, but for those who didn’t catch that episode, I am now announcing Functional Geekery, a podcast about functional programming.

After some issues with getting the hosting setup properly, and working with the hosting provider’s support for a couple of issues, the first episode is ready to go live! I will be working on getting it in the iTunes store, and some of the other podcasting services, but in the meantime, you can find it online.

I am hoping to have a wide range of guests and topics, from Clojure, to Erlang, to JavaScript, to F#, as well as Scala, Haskell, and functional programming in languages like C# and Ruby. If you have any suggestions on shows, topics, or guests, check out the About Page on the site to submit ideas.

–Proctor

Motivational Monday – Dec 16th, 2013

A simple quote today to begin the week with.

Life is not about finding yourself, but creating yourself.
–George Bernard Shaw

When you think about it, George Bernard Shaw had it right. The question is asked, “What do you want to make of yourself?” We don’t think much about creating ourselves, but isn’t make just another word for create? What steps can you take to create yourself this week?

What do you want to create of yourself?
–Proctor

Insert Into… Select … Where …

About a month ago one of my teammates asked to confirm how to do a Insert Statement in SQL. He wanted a quick check about using an Insert-Values style statement, but instead of Insert-Values, I recommended doing a Insert-Select-Where. One of my other teammates asked why I would do it that way and I explained my reasoning to him, and decided it would make a good post.

Let us start with a schema for some_table. (NOTE: I will be using PostgreSQL for these examples, but I originally started off doing this with SQLServer, so the concepts translate across databases although syntax might change.)

proctor=# d some_table
               Table "public.some_table"
 Column  |            Type             |   Modifiers   
---------+-----------------------------+---------------
 name    | character varying(40)       | 
 id      | integer                     | 
 size    | integer                     | 
 modtime | timestamp without time zone | default now()

A number of people would be happy doing an Insert-Values statement to get the data in the table as shown below.

proctor=# INSERT INTO some_table(id, name, size) VALUES(1, 'a', 14);
INSERT 0 1
proctor=# select * from some_table;
 name | id | size |          modtime           
------+----+------+----------------------------
 a    |  1 |   14 | 2013-12-10 22:07:58.706064
(1 row)

That works and I have nothing against that, but I have found myself more fond of doing a Insert-Select-Where style statement.

This stems from doing database migrations on SQLServer for .NET development before there were any real tools to do it. We would have to do the migrations ourselves, later with a homegrown tool to help handle it, but even then our goal was to make our scripts “SQL Safe.” SQL Safe was our term for making any SQL that we ran idempotent, so if you ran a script, it would determine if it should run again. Schema modifications checked the schema to see if the needed modification already existed. Insert statements would check to see if the values to be inserted was already represented in the system.

It started with doing an IF check against the Insert-Values.

proctor=# DO
proctor-# $BODY$
proctor-# BEGIN
proctor-#   IF NOT EXISTS (SELECT 1 from some_table where name = 'a' and size = 14) THEN
proctor-#     INSERT INTO some_table(id, name, size) VALUES(1, 'a', 14);
proctor-#   END IF;
proctor-# END;
proctor-# $BODY$;
DO
proctor=# select * from some_table;
 name | id | size |          modtime           
------+----+------+----------------------------
 a    |  1 |   14 | 2013-12-10 22:07:58.706064
(1 row)

Which is not bad, but it is not great either. We moved to the Insert-Select-Where style, which looks like the following:

proctor=# INSERT INTO some_table(id, name, size)
proctor-# SELECT 1, 'a', 14
proctor-# WHERE NOT EXISTS (SELECT 1 from some_table where name = 'a' and size = 14);
INSERT 0 0
proctor=# select * from some_table;
 name | id | size |          modtime           
------+----+------+----------------------------
 a    |  1 |   14 | 2013-12-10 22:07:58.706064
(1 row)

What is nice about this is:

  • it makes the Insert idempotent;
  • more likely to be cross platform instead of syntax differences in how IF statements are handled, and
  • it becomes easy to craft the statement I need to create the values and run a test SELECT by just commenting out the INSERT INTO portion.
proctor=# --INSERT INTO some_table(id, name, size)
proctor-# SELECT 1, 'q', 37
proctor-# WHERE NOT EXISTS (SELECT 1 from some_table where name = 'q' and size = 37);
 ?column? | ?column? | ?column? 
----------+----------+----------
        1 | q        |       37
(1 row)

I would love to hear your feedback about this.

–Proctor

Motivational Monday – Dec 9th, 2013

I do these Motivational Monday posts to try and give the week a positive outlook. In the field of computer software, there is a lot of negativity; my goal with these posts is to hopefully have some niceness on the internet, and be a reprieve from the trolls and frustrations in our job.

This weeks motivational thought comes from The Iron Giant: You are who you choose to be.

Who you are is up to you, and nobody else. Don’t let someone else tell you that you can’t do something. Don’t let yourself tell you that you aren’t good enough. You have the power to override that negative voice programed into your head; you can choose who you want to be. Find your inner-Superman and choose that.

–Proctor

Chef: knife “FATAL: X nodes found, but does not have the required attribute to establish the connection.”

A couple of weeks ago I was trying to do a chef-deploy using the knife ssh command from Chef and specifying the role, but was getting the following error:

chef-repo proctor$ knife ssh "role:reporting" "sudo chef-deploy"
FATAL: 12 nodes found, but does not have the required attribute to establish the connection. Try setting another attribute to open the connection using --attribute.

When I tried to do a search of nodes using the knife search command, I was getting another error:

chef-repo proctor$ knife search node role:reporting
ERROR: knife encountered an unexpected error
This may be a bug in the 'search' knife command or plugin
Please collect the output of this command with the `-VV` option before filing a bug report.
Exception: NoMethodError: undefined method `name' for #<Hash:0x007fa8d9914f40>

I could do a knife ssh and do a chef-deploy if I did a manual listing of the servers.

chef-repo proctor$ knife ssh -m "reportingserver.qualified.domain" "sudo chef-deploy"

I knew we had the servers setup with the chef gem at version 10.24.0, and had thought I was setup with 10.24.0 locally as well. When I double checked my local version of the chef gem I saw I was actually on 10.20.0 locally. After uninstalling the chef gem, and installing to target 10.24.0 to match the servers, everything started working as expected.

Hopefully, you encounter errors similar to the above this will save you some time, and help you learn from my problem, and serve as a notice to double check the versions of the chef gem between your local environment and your servers.

–Proctor

Motivational Monday – Dec 2nd, 2013

In honor of Thanksgiving this past week, today’s motivational thought comes from Arlo Guthrie’s Alice’s Restaurant.

And three people do it, three, can you imagine, three people walking in
singin a bar of Alice’s Restaurant and walking out. They may think it’s an
organization. And can you, can you imagine fifty people a day, I said
fifty people a day walking in singin a bar of Alice’s Restaurant and
walking out. And friends they may thinks it’s a movement.

And that’s what it is, the Alice’s Restaurant Anti-Massacre Movement, and
all you got to do to join is sing it the next time it come’s around on the
guitar.

With all of the power we have in today’s age, all of the different social networks we all belong to, both digital and in person, imagine what kind of movements we can start. Even if it is something as simple as getting 49 of your friends to join you in waiting to hold the door open for someone with their hands full who is the other side of the parking lot; giving a thank you smile, handshake, or even buying a hot chocolate to the person who is volunteering to spend their time in the cold ringing the bell for The Salvation Army.

What movement can we start to help make this world a better place?

In closing:
You can get anything you want, at Alice’s Restaurant
You can get anything you want, at Alice’s Restaurant
Walk right in it’s around the back
Just a half a mile from the railroad track
You can get anything you want, at Alice’s Restaurant

–Proctor