Programming

Categories

  • No categories

SEARCH

I’m Attending

CodeMash

SQL Server Backup and Restore Across Server Username Fix

Posted in: Programming by Steve on August 22, 2012 | No Comments

I run into this a lot while deploying new fixes. If you have a production and a stage environment, before deploying to production, you should backup and restore your database to the staging database to do all your UAT. However, if your usernames are the same, then you may run into an issue where SQL has an "orphaned" user that cannot be associated to the database (http://msdn.microsoft.com/en-us/library/ms175475.aspx).

USE [database_name]
GO
exec sp_change_users_login "auto_fix", "username";

That should get you back up and running.

LogParser Hits Per Day on Specific Page

Posted in: Programming by Steve on May 10, 2012 | No Comments

I was trying to get hits per week on a specific page, but I had to settle for exporting to a CSV and doing the rest in Excel.

logparser -i:IISW3C -o:CSV "SELECT TO_STRING(TO_LOCALTIME(TO_TIMESTAMP(date, time)), 'yyyy-MM-dd') AS [Day], COUNT(*) AS [Requests] INTO hits_per_day.csv FROM *.log WHERE cs-uri-stem = '/login.aspx' GROUP BY [Day] ORDER BY [Day]"
Reindex All Tables in Database (SQL Server)

Posted in: Programming by Steve on December 7, 2011 | No Comments

Another thing I do a lot and always forget about:

EXEC sp_MSforeachtable @command1="print '?' DBCC DBREINDEX ('?', ' ', 80)"
GO
EXEC sp_updatestats
GO
Building SQL Server T-SQL with Error Handling

Posted in: Programming by Steve on December 1, 2010 | 2 Comments

Today, my manager asked me how I build my T-SQL scripts when I need to update a bunch of data in SQL Server.  I hadn’t really thought much about it, but this is what I normally use.

DECLARE @ErrorCode INT

BEGIN TRANSACTION

	--UPDATE A TABLE OR SOMETHING

	SELECT @ErrorCode = @@ERROR
	IF (@ErrorCode <> 0) GOTO PROBLEM
	
	--DO SOMETHING ELSE
	
	SELECT @ErrorCode = @@ERROR
	IF (@ErrorCode <> 0) GOTO PROBLEM	
	
COMMIT TRANSACTION
	
PROBLEM:
IF (@ErrorCode <> 0)
BEGIN
	PRINT 'Error Executing Script: Error Code: ' + @ErrorCode
	ROLLBACK TRANSACTION
END

Not sure how anyone else does it, but that’s how I roll.  Any suggestions on how this could be improved? 

Issue Installing Gems with Ubuntu 10.04, RVM and zlib

Posted in: Programming by Steve on October 30, 2010 | No Comments

So after writing up the tutorial on getting a Linux VM setup, I came across and issue where I couldn’t install gems. I would get the following error message after trying to install any gem:

ERROR: Loading command: install (LoadError)
  no such file to load – zlib
ERROR: While executing gem … (Name Error)
  uninitialized constant Gem::Commands::InstallCommand

image

I found the answer in the RVM documentation: http://rvm.beginrescueend.com/packages/zlib/

Execute these commands:

rvm package install zlib
rvm remove 1.9.2
rvm install 1.9.2 -C --with-zlib-dir=$HOME/.rvm/usr

This will install zlib in your ~/.rvm/usr directory.

I could then move forward setting up my development environment.

Heroku, Rails 3, memcache and You

Posted in: Programming by Steve on October 29, 2010 | No Comments

Since Codemasher uses the CodeMash REST API, I didn’t want to bog it down with requests from the website. So I looked up how to use caching with Heroku.

Here is how I did it:

First, edit your Gemfile to include dalli as your cache client:

group :production do
  gem 'dalli'
end

Add this line to your environments/production.rb:

config.cache_store = :dalli_store

Finally, if you haven’t enabled the free 5MB memcache addon for Heroku, execute this from your application directory (command line/terminal):

heroku addons:add memcache:5mb
Resolving Tree Conflicts in Subversion

Posted in: Programming by Steve on October 28, 2010 | No Comments

I run across this error way too much.  I try to merge a branch into my working copy or another branch, and I get tree conflicts that I can’t explain.  It’s probably because I’m a VCS dummy or because I was doing it wrong at one point or another.

I found this blog post that described how to do branching & merging and it describes a step to resolve the tree conflicts.

svn resolve -R --accept working *

So now, It’s easy enough to just execute this command and move on.

My First Real Rails App

Posted in: Programming by Steve on October 27, 2010 | No Comments

codemasher As CodeMash approaches, I wanted to build something that would get me more formally introduced to rails 3.  I also wanted to set it up so others could see it and view how I was coding it so I could get some direction.

So I present to you, the Codemasher. It’s a quick and dirty website that returns a random session to attend.  It uses the REST API provided from the CodeMash website and is the simplest site I could come up with.  I have deployed an example up on Heroku at http://codemasher.heroku.com

I hope to have it working where it remembers the sessions that you didn’t like. Another thing is to set it up to look at the sessions that will happen in the next chunk of time slot(s) so you could look at it at the end of a session and try to find another session to go to.

It was inspired by WTFSIMFD (http://whatthefuckshouldimakefordinner.com) but without the curse words.

All of the code is available on github at http://github.com/underwhelmed/codemasher.  Please take a look and see how you like it, and if you would make improvements to it, I’m all ears.

Setting up a Linux Rails Development Virtual Machine

Posted in: Programming by Steve on October 26, 2010 | No Comments

I wanted to do some Rails development on my Windows machine, but I didn’t want to worry about compatibility.  So here are my steps to build up my development environment.

First thing is to download an Ubuntu VMWare Appliance.  I got my machine from http://vmplanet.net/node/114

Second was to rename the machine – I used the tutorial here http://www.liberiangeek.net/2010/06/how-to-quickly-change-computer-name-in-ubuntu-10-04-lucid-lynx/. Since my server/network naming is based on characters from Futurama, I selected Leela as my rails development VM.

Next was to install git – http://book.git-scm.com/2_installing_git.html, curl and the developer tools

sudo apt-get install git-core
sudo apt-get install curl
sudo apt-get install build-essential

Next, time to install the base ruby libraries:

sudo apt-get install irb libopenssl-ruby libreadline-ruby rdoc ri ruby ruby-dev

The next step is to install ruby gems and make sure that you have the latest version:

sudo apt-get install rubygems1.8
sudo gem update –system 

Now, you’re ready to start getting your environment setup for rails.  By default, new rails applications use SQLite3 so we’ll install it and get it ready for use in our applications if we so please.

sudo apt-get install libsqlite3-dev sqlite3 sqlite3-doc

Our next step is to install Ruby Version Manager. This package allows you to set specific versions of ruby (1.8.7 or 1.9.2 are the main versions) and set specific gemsets:

bash < <( curl http://rvm.beginrescueend.com/releases/rvm-install-head )

Then open up your .bashrc file (located in the home directory, ~), and append the following code to the end of the file:

[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"  # This loads RVM into a shell session.

Now you can close your terminal session, open a new one, and RVM will be ready for you to use. 

There are a few editors that you can use (gEdit, Aptana Studio 3 or even JetBrains RubyMine which is a paid for application). I prefer TextMate on OS X so I use gEdit along with a few plugins to get it to look like TextMate.

That should get you started on your way to get developing on a linux VM, or even a regular PC set up with linux on it.

References:

Time to Secure Your Cookies

Posted in: Programming by Steve on October 25, 2010 | No Comments

So I came across this article: http://codebutler.com/firesheep.

Wow, time to secure your sites.