The first part of our conversion of DbScriptomate to Ruby is to accept command line arguments. Any useful console based application accepts parameters passed to it via the command line. Accepting command line parameters will also make it possible to run our application via scripts as part of a build system.

Based on the command the application will need different parameters; the commands necessary in DbScriptomate are:

setupdb - this will setup the database for initial use

migrate - will run all the migration files

generate - will generate a new migration script

To setup the database for the first time and to run the migration scripts some parameters necessary are:

Host - the name or IP address of the machine where our SQL database is running

Database Name - the database against which we need to run the scripts

Username - the user that has the correct permissions to run these scripts

Password - the password of the user

Port - the port used to connect to the database

To generate a new migration script DbScriptomate will need:

Type - is this a DDL or DML script (Stored Procedure or CRUD)

Your initials - the initials of the current user

Description - a description or summary of what this migration script will do.

We are at a good place to start. Before we do anything else, we need to setup our file structure. Create a new folder called dbscriptomate, within the folder create a bin, lib and test folder. A good place to start is testing that our application can indeed accept command line arguments. Create a file within the test folder called commands_tests.rb.

Now we can write our first test. The first requirement is that the application accept the commands setupdb, migrate and generate. We are going to need these commands to invoke the different operations, and a class called Command seems like a good choice to me. We can start off by mixing our tests and implementation in one code file and refactor as we go along. Let’s write a test.

We need to make sure we can parse the command so lets start there. To call DbScriptomate we are going to pass the arguments as follow:

The first argument is the actual command, the rest of the arguments are for connecting to the database. Here is the first test to ensure we cover the generate, migrate and setupdb commands.

The code to make this pass is quite simple:

We now have just enough code to make this pass. We can now parse the rest of the arguments. Here comes the awesome bit, Ruby has the OptionParser class. The OptionParser allows for easy parsing of command line arguments. We are going to use OptionParser to get the other parameters that can be passed in, here are our tests to prove that the parsing works:

We will modify the initialize method to setup our OptionParser:

Let’s step through this code to detail what is going on. We have a variable called parameters which will expose our parsed command line arguments. We pull out the operation and store that for later use. The OptionParser class takes in a block that will firstly define a few details that will be displayed when calling help, there after we have a few more blocks that set our parameters Hash. Finally, we call the parse! (pronounced parse bang) method that will execute all the blocks from above thereby parsing the arguments passed in.

This is just enough code to get us going on this journey and it is also enough code to show how awesome Ruby is. Tomorrow we are going to write some code that uses the operation and parameters.

Discussion, links, and tweets

My name is Deon Heyns and I am a developer learning things and documenting them in realtime. Python, Ruby, Scala, .NET, and Groovy are all languages I have written code in. I appeared in the New York Post once. I host my code up at GitHub and Bitbucket so have a look at my code, fork it and send those pull requests.