Database Migrations Converting DbScriptomate to Ruby - Command Line Arguments

August 11, 2014

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:

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

To generate a new migration script DbScriptomate will need:

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:

generate -h 127.0.0.1 -n test -u admin -p password -o 14332 -t ddl -i dh -d description

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.

comments powered by Disqus