Subakva Teknika

MySQL Mass Destruction With Xargs

| Comments

Yesterday, I needed to clobber a bunch of similarly-named database schemas on my development machine. I could have selected and deleted 40 times in Navicat, but I thought I’d try to write a one-liner instead:

`

mysql -u root -e “show databases;” | grep _3_6 | xargs -IDB_NAME mysql -u root -e “drop database DB_NAME;”
`

The first part pulls the database names all the database names. The second part culls any schema names that don’t match “_3_6”, and the last part executes a DROP DATABASE command for each matching schema name.

The clever bit is the “-I” flag to xargs, which sets a token to be replaced with the argument in the command string. In this case, the schema name (“pp_development_3_6”) is dropped in wherever xargs sees the string “DB_NAME” in the command.

_Wikipedia attributes the xargs utility to PWB/UNIX. It may have been written by Dick Haight, who is, apparently, the 10 of Clubs in the Unix 25th Anniversary Playing Card deck, which, apparently, exists.
_

ShareableRouteSet - Gist

| Comments

ShareableRouteSet - Gist

This is a class to allow the creation of Rails route helpers for external applications without interfering with the application routes.

This is useful when you have a Rails application that needs to have some knowledge about the routes in another application. The routes could be published by the source application in a plugin/gem to avoid duplication of routing information.

Find and Concatenate Files With Spaces

| Comments

Normally, xargs will interpret the spaces in the filenames as separate files. The -print0 argument to find paired with the -0 argument to xargs will use some other magical delimiter so that the spacey filenames remain intact as you pipe them around.

`

find ./spacey_files -name Report -type f -print0 | xargs -0 cat > all_reports.csv
`

Remove an Environment Variable in Bash

| Comments

export -n RAILS_ENV

Setting an environment variable to nothing is not the same as removing it entirely. If really need it to go away completely, use the -n flag on export.

Behold:

<code>
jason@idaho:~/Code/subakva$ env | grep RAILS_ENV
jason@idaho:~/Code/subakva$ export RAILS_ENV=development
jason@idaho:~/Code/subakva$ env | grep RAILS_ENV
RAILS_ENV=development
jason@idaho:~/Code/subakva$ export RAILS_ENV=
jason@idaho:~/Code/subakva$ env | grep RAILS_ENV
RAILS_ENV=
jason@idaho:~/Code/subakva$ export -n RAILS_ENV
jason@idaho:~/Code/subakva$ env | grep RAILS_ENV
jason@idaho:~/Code/subakva$
</code>

RunCodeRun

| Comments

For a personal project, I’m trying out RunCodeRun, a service for doing automated builds for projects on github.

I think the idea is a good one. For gems or simple web apps without complicated integration tests, getting an automated build up and running is dead simple. It’s just a matter of adding a post-commit hook to your github project to notify runcoderun that the code has been updated.

I’ve run into a few issues with missing gems on the build machine, but they’ve been incredibly responsive. After posting a support request to install a gem, they not installed the gem I requested; they also installed a gem I didn’t know was missing, forked my project to fix a gem dependency problem and a database configuration problem, and sent me a pull request. This is all within 24 hours, and for a service I’m not even paying for!

The service is free for Open Source code, but they have a paid build option for private projects with dedicated build resources. I haven’t used the paid service, so I’m not sure to what extent they support more complex requirements like multiple database connections, system services (Sphinx), and distributed systems.

Hopefully, if Relevance is dedicating the resources to provide such great support, they’ll also be improving the flexibility of the service for more complex builds. I doubt it would work in its current state for my current professional work, but I’d love to be able to easily outsource my builds.

Git Cannot Merge? Try Committing First.

| Comments

I ran into this git error again today:

<code>
Entry 'blah/blah.rb' would be overwritten by merge. Cannot merge.
</code>

After messing around with it for a while, I remembered the last time this happened. The solution is to commit your current conflicting changes, then merge afterwards.

URI.join v. File.join

| Comments

URI.join does not work quite the same way as File.join in Ruby. The aim of URI.join is more to calculate a relative path (as a browser would) than to simply concatenate path elements cleanly. The irb output below demonstrates this:

`

irb> require 'uri' #=> true
irb> host = 'http://www.example.com' #=> "http://www.example.com"
irb> prefix = 'prefix' #=> "prefix"
irb> file = 'file.html' #=> "file.html"
irb> URI.join(host, prefix, file).to_s #=> "http://www.example.com/file.html"
irb> URI.join(host, prefix+ '/', file).to_s #=> "http://www.example.com/prefix/file.html"

`