WordPress on Windows 2008 R2

The idea was to look how easy would be to install and configure WordPress on Windows Server 2008 R2 (Web Server edition). Everything is out-of-box. And, here we starting…

1. I created new virtual machine on Oracle Virtual Box 4.1, 40GB disk, 4GB memory, 4 processors, installed and activated Windows Web Server 2008 R2 SP1 64-bit. The network adapter is set to “Not attached” to prevent installers talking to the web.

2. Added Web Server (IIS) Role. Here is nothing unexpected. However the issue was in configuring it for upcoming PHP component. I desided to work with PHP via FastCGI (I really do not know how it now works with ISAPI, since there is no php5isapi.dll in the distribution), so the only one new Role Service is needed – CGI. So, it is installed OK.

Continue reading

Job out of msdb

Well, I deleted job from the server. Complicated job. Pain to recreate. But I had backup of the msdb database and quickly managed to restore it to msdb_old. Good.

For obvious reasons I did not want to replace me current msdb database. Not so obvious? Well, if something goes wrong with restore, then I would have to do recovery – hassle I really do not need. And I do not need to restore everything, just one job, for Pete’s sake.

OK, this task should not be this complex. The whole job definition sits in the foursystem tables: sysjobs, sysjobsteps, sysjobhistory and sysjobschedules. Rest is rather trivial:

DECLARE @JobID UNIQUEIDENTIFIER
SELECT @JobID = job_id FROM msdb_old.dbo.sysjobs WHERE NAME='My Lost Job'

INSERT msdb.dbo.sysjobs
SELECT * FROM msdb_old.dbo.sysjobs
WHERE job_id=@JobID

INSERT msdb.dbo.sysjobsteps
SELECT * FROM msdb_old.dbo.sysjobsteps
WHERE job_id=@JobID

SET IDENTITY_INSERT msdb.dbo.sysjobhistory ON
INSERT msdb.dbo.sysjobhistory
(instance_id,job_id,step_id,step_name,sql_message_id,sql_severity,
[message],run_status,run_date,run_time,run_duration,operator_id_emailed,
operator_id_netsent,operator_id_paged,retries_attempted,[server])
SELECT
instance_id,job_id,step_id,step_name,sql_message_id,sql_severity,
[message],run_status,run_date,run_time,run_duration,operator_id_emailed,
operator_id_netsent,operator_id_paged,retries_attempted,[server]
FROM msdb_old.dbo.sysjobhistory
WHERE job_id=@JobID
SET IDENTITY_INSERT msdb.dbo.sysjobhistory OFF

INSERT msdb.dbo.sysjobschedules
SELECT * FROM msdb_old.dbo.sysjobschedules
WHERE job_id=@JobID

RowNumber()

Today I faced interesting issue: in the table, containing customer contact information, I needed to create a primary key out of two fields – CustomerID and ContactID. CustomerID already existed, I needed somehow generate ContactID, that would start with 1 for each CustomerID and increment by 1 while CustomerID remains the same.

I remembered OK how to dynamically generate identity field:
SELECT number=IDENTITY(int,1,1), * INTO t_CustomerContacts FROM temp_CustomerContacts ORDER BY CustomerID

However this elegant approach in my case will not work – there is no way to reset the identity counter each time the CustomerID will change. However, a very elegant solution exists for my case as well:

SELECT ROW_NUMBER() OVER ( PARTITION BY CustomerID ORDER BY CustomerID) ContactID, * INTO t_CustomerContacts FROM temp_CustomerContacts

Powershell Here

This is a handy Powershell shortcut that allows to start Powershell in current directory. It adds a menu item to the Windows Explorer right-click popup menu, “PowerShell Here“.

Unfortunately this registry entry does not allow anything like %SystemRoot%, so the suggested solution below needs to be tweaked accordingly. The registry (.reg) file is a UCS-2 Little Endian (or, as UltraEdit notes, U-DOS) encoded text file.

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\shell\powershell]
@="PowerShell Here"

[HKEY_CLASSES_ROOT\Directory\shell\powershell\command]
@="C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\powershell.exe -NoExit -Command Set-Location -LiteralPath '%L'"

Continue reading

Notepad++ and Ruby

Notepad++ is an excellent code editor for Ruby as it is, out of box. The remaining thing is to properly configure it, so it will be able not just run the code, but sometimes display the output of such.

There is an elegant way to make it work involves a bit more than simply typing ruby "$(FULL_CURRENT_PATH)" in the run command window. This, of course, will 100% work; however it runs and dismisses the output window unless the whole system is configured not to dismiss the command window automatically. Do not ask me how to configure this – I forgot such nonsense as I could not imagine why one would ever need even to consider this option.

Continue reading

First Cool Thing About Ruby

Well, the first coll thing about Ruby is that it is Japanese in the origin. Cool. But the more I look into it, the more I like it.

Is if it was not enough that everything in Ruby is an object, it was fun to learn that everything in Ruby is a reference. Well, this may be confusing. Let’s consider code:

irb(main):001:0> x = "Hello, world!"
=> "Hello, world!"
irb(main):002:0> y = x
=> "Hello, world!"
irb(main):003:0> x.chop!
=> "Hello, world"
irb(main):004:0> puts x
Hello, world
=> nil

OK, x now holds "Hello, world". However, what y holds? It would be sane to suggest, that y holds "Hello, world!", the original value with exclamation sign. We first assigned x to y and only after that we chopped the exclamation sign off the string. Why on the earth it should affect y? Let’s see:

irb(main):005:0> puts y
Hello, world
=> nil

This is really cool. It appears that variables in Ruby hold references, not values. There are no such things as primitive types. And the question of reference or value types just simply does not exists: everything is a reference type.

Hg rollback

In my attempt to keep my repository clean, very attractive looked the command hg rollback, that allows revert to the repository state before last commit. It seems to be very good, especially when commit is happened by mistake and the code is in very intermediate stage. But there are hidden concerns. They appear when the commit is already made to remote repository.

Continue reading

Batch Rename

If we need to change extension to all the files with certain extension in all subfolders of the current folder, the task may appear rather trivial; the example below changes extensions of the files in the current folder and subfolders form .TAB to .sql
ls -re | ?{ $_.Extension -eq ".TAB" } | %{ Rename-Item $_.FullName $_.Name.Replace(".TAB", ".sql") }

Keep in mind, that the code above will do extension replacement in a case-sensitive manner, i.e. will replace capitalized .TAB. Also it may replace a bit more than just extension, if .TAB occurs some place else in the name.

So, how to make the code more full proof, case-insensitive, more universal and keep it in one line?

Continue reading

Powershell Version

Not so obvious. Powershell does not recognize ver, version and so on commands. But desire to know the version sometimes just burning inside because of eternal question: “Is it just me, or it is the old version of this?”

This code will resolve such dilemma: Get-Host

Search Stored Procedures

How do I find all stored procedures that contain specific text? The SQL Server Management Studio does not offer such an option. The solution must be fail-proof, otherwise it can cause system failure due to missing reference.

Procedure sp_depends fails to return references to statements within sp_executesql, and this is bad enough. Of course it is absolutely unnecessary to cover cases where the text in the command is split in parts due to some strange developer’s caprice, but otherwise fail-proof solution must be found.

So, if we searching for Hello, the solution is:
SELECT name FROM sys.procedures WHERE OBJECT_DEFINITION(object_id) LIKE '%Hello%'

Manipulating Services

Sometimes I want to stop services I do not need to run permanently. Real-time antivirus become annoyance when compiling a project that is larger than typical “Hello, World!” Real annoyance. There is a quite elegant way to quickly stop them, in one line.

Get-Service @('tmlisten', 'ntrtscan', 'aexnsclient') | Where-Object{$_.Status -eq 'Running'} | ForEach-Object{$_.Stop()}

Continue reading