In trying to automate our DB development, we decided to use ant and Jenkins to run our SQL scripts for a deploy. We had a hard time getting ant to work and after much trial and error (and cussing and fussing) we finally got it to work. The key really was the addsourcefile=”false” attribute and the redirector. Since mysql can run sql from a file using redirection (< ) you have to use a redirector (which is poorly documented). The ant apply task can apply an executable to every file in a fileset, so this will use the mysql executable to exec every *.sql file it finds. Continue reading
Tag Archives: coding
Using the Sonar REST API for weekly emails
At work, we wanted to create an automated weekly email to the team that shows how we’re doing on new code. Sonar (now SonarQube) has a Sonar REST API and a Sonar Java WS client wrapper for it. Here’s how I did:
First, you need to round up some jars. You need:
- commons-logging
- httpclient
- httpcore
- json-simple
- sonar-ws-client
Unit testing JavaBeans
I always just pretty much skipped unit testing JavaBeans since the work involved didn’t seem worth it. Today though, I was troubled by the lack of coverage on the Javabeans in our Emma code coverage reports so I googled and found this bit of unit testing brilliance: Unit Testing Javabeans. It’s a simple class that uses introspection to test the bean and works great!
Twitter search archiver
With Teenormous, we started giving away free tees to people that tweet a link to us. Problem is, we couldn’t find an easy solution to archiving/looking back on tweets for the month. Twitter keeps the tweets seachable for a very short time. We found a few web apps/services that did some similar things but not exactly what we wanted. We even tried Google Reader by putting the search RSS feed into it. (That worked, but trying to scroll and count to #57 is a royal pain and that method just doesn’t scale.) So, some poking around led me to the excellent Ruby library for Twitter by John Nunemaker which uses the Twitter APIs.
It’s so easy to use it’s laughable. A few hours later and I have an app/script written that stores the results of any twitter query (like the one we care about) into a static HTML file with them in order from oldest to newest and each tweet numbered for easy random picking of winners. It also automatically creates a new file each month. I used the Twitter CSS so it looks nice and clean. Slap it in a cron job and we have auto-archiving twitter searches. Plus – it’s in HTML format in case we want to put them online for some reason.
It’s nice sometimes to do a little coding project that’s not exactly what you typically do. Oh – and Ruby rocks.
How to get Rails to talk to Oracle
How to get Rails working on Windows with a pre-existing oracle DB…..
I wanted to try and hookup Rails to our existing oracle DB at work. I had a rough time figuring this out, so here it is in case anyone else wants/needs it. In my case, I did not (nor did I want to) have a full Oracle client installed on my Windows box. trying to get this to work was not easy. I found 5 or 6 different pages that told different ways to get it to work, but none worked for me.
Most of this info was compiled from Oracle’s Rails FAQ and a few other pages on the net. These instructions assume you don’t have the Oracle client installed already. If you do, some of these steps may not be necessary(1, 3, 4)
- if you don’t have the Oracle client installed already, install the Oracle Instant Client (I installed the Basic, Lite version) – this zip contains some DLLs and jars. Extract them somewhere and note the location. Let’s call it [ORACLE_CLIENT_DIR]
- install the Oracle driver for Rails (I’m assuming you already have Rails installed). Details here.
- create a tnsnames.ora file in [ORACLE_CLIENT_DIR]/network/admin/. Put the text below in the file. Replace host, port, SID, and name (first line – MYORACLEDB in the example below) as necessary. If the DB uses a service_name instead of SID, replace SID in the file with SERVICE_NAME.
MYORACLEDB =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = some.server.com)(PORT = 1521))
)
(CONNECT_DATA =
(SID = mysidinfo)
)
)
- create Windows environment variables like so:
ORACLE_HOME = [ORACLE_CLIENT_DIR]
TNS_ADMIN = [ORACLE_CLIENT_DIR]/network/admin
you can create these globally in Windows or just set them up before running the server each time - setup the database connection in your app as shown here, but replace
host: ORCL
with the name you used in your tnsnames.ora file (MYORACLEDB was used in the example above) - if you are connecting to an existing DB that did not follow the standard pluralization conventions Rails likes (i.e. your table names are not pluralized), you need to tell Rails not to do pluralization (which it does by default). Open [yourapp]\config\environment.rb and put this at the bottom of the file:
ActiveRecord::Base.pluralize_table_names = false
That’s it! Now, you can generate models, controllers, etc. and they should hookup to your Oracle DB as you would expect. Hopefully I didn’t miss any steps. I wrote this after I had it working, not while I was trying to get it working. If you are in a similar situation and these steps don’t work, feel free to leave a comment and I’ll see if I can help.