Custom Defined Identities

I love identity columns in database. They make great primary key for almost any table. Only situation where I hate them is when I need to import data with foreign keys. You see, you cannot manually define identity column. Or can you?

Let’s imagine SQL Server table (named SomeTable) with two columns. First one will be Id (int, identity and primary key) and second one will be SomeText (nvarchar). Then let’s try to insert some data:

INSERT INTO SomeTable(Id, SomeText) VALUES(1, 'Some text');
Msg 544, Level 16, State 1, Line 1
Cannot insert explicit value for identity column in table 'LookupDiagnosis' when IDENTITY_INSERT is set to OFF.

Like all good error messages, this one already offers solution. We need to manipulate IDENTITY_INSERT option. Let’s try again:

SET IDENTITY_INSERT SomeTable ON
INSERT INTO SomeTable(Id, SomeText) VALUES(1, 'Some text');
SET IDENTITY_INSERT SomeTable OFF

With this little trick importing data with identity columns becomes much easier.

P.S. Just remember that you cannot have IDENTITY_INSERT set on more than one table at a time.

P.P.S. This option is also great for filling gaps left in identity column by deleted rows or canceled transactions.

Moonlight 4

Illustration

Lately big part of my day is working on Linux. Usually there are no issues. As long as I stick to web applications I am mostly ok.

Here comes the problem. For their PDC conference Microsoft decided to use Silverlight. Since I wanted to check a video or two and Microsoft is proud of Silverlight being supported on every possible platform I thought that everything would work.

Well it doesn’t.

P.S. Should I say that Flash videos work without issues on both Windows and Linux?

Seobiseu 1.10

Illustration

New version is here. Those missing restart option can now rejoice. Restart is available in both context menu on main screen and in notification area.

Download is available here.

P.S. Restart option is not on main menu itself since it makes it a little crowded.

Multi-mercurial

I believe that ideal setup for Mercurial is one project per repository. Most of time this is really good setup but occasionally it will cause problems. Whenever I need to do same thing on multiple repositories (e.g. push) there is an issue. Better said, there was an issue before I created HgAll script.

Script is written in PowerShell and does two things. First it traverses all directories bellow current (or defined) one and finds all mercurial repositories. Then it forwards whatever is on command line as parameter to hg command line utility. In effect it executes same command to each repository it finds.

HgAll [StartingFolder] [commands]

HgAll C:\Source summary
HgAll push all

First example will start search from C:\Source and execute “hg summary” command for any Mercurial folder found. Second example will start in current directory and execute “hg push all”.

You can download script here (or take a peek at source bellow):

$StartupLocation = (Get-Location -PSProvider FileSystem).ProviderPath

$Folder = $Null;
$Command = ""
foreach ($arg in $args) {
    if ($Folder -eq $Null) {
        if ((Test-Path -Path $arg -PathType Container) -eq $True) {
            $Folder = (Resolve-Path -Path $arg).Path
            $Command = ""
        } else {
            $Folder = $StartupLocation
            $Command = $arg
        }
    } else {
        if ($Command.length -ne 0) { $Command += " " }
        $Command += $arg
    }
}

if ($Command.length -eq 0) {
    $Command = "summary"
}

function Traverse([string]$path) {
    Write-Debug $path
    $dirs = Get-ChildItem $path | Where {$_.psIsContainer -eq $true}

    if ($dirs -ne $Null) {
        foreach ($dir in $dirs) {
            $hgsubdir = Join-Path -Path $dir.FullName -ChildPath .hg
            if ((Test-Path -Path $hgsubdir -PathType Container) -eq $True) {
                $hgdir = $dir.FullName
                if ($Command.length -ne 0) {
                    Set-Location $hgdir
                    Write-Host
                    $result = Invoke-Expression "hg $Command 2>&1"
                    if (($LASTEXITCODE -ne 0) -or (!$?)) {
                        Write-Host $dir.Name
                        foreach ($line in $result) {
                            Write-Warning $line
                        }
                    } else {
                        Write-Output $dir.Name
                        foreach ($line in $result) {
                            Write-Host $line
                        }
                    }
                }
            } else {
                Traverse($dir.FullName)
            }
        }
    }
}


try {
    Traverse($Folder)
} finally {
    Set-Location $StartupLocation
}

Running PowerShell

I mostly use PowerShell for my scripting needs. However, I am so used to old command prompt that I type cmd.exe without thinking. So, instead of changing my habits, I have .bat file for each PowerShell script:

powershell script.ps1 %*

First and second parameter determine what to run and third parameter forwards all command line arguments to script.