A pleasant walk through computing

Comment for me? Send an email. I might even update the post!

Clean Up Orphaned SQL Users

This used to be done with sp_change_users_login, but that procedure is being removed in favor of alter user.

--http://ask.sqlservercentral.com/questions/3173/identifying-orphaned-users.html
--http://stackoverflow.com/a/2874493
--Modified by me to use correct search for orphans
DECLARE @Username VARCHAR(100),
        @cmd      VARCHAR(100)

DECLARE userlogin_cursor CURSOR FAST_FORWARD FOR
SELECT UserName = dp.name
--, UserSID = dp.sid
FROM sys.database_principals dp
WHERE dp.type = 'S'
    AND (dp.sid is not null AND dp.sid <> 0x0)
    AND (LEN(dp.sid) <= 16)
    AND SUSER_SNAME(dp.sid) is null
ORDER BY dp.name
FOR READ ONLY

OPEN userlogin_cursor
FETCH NEXT FROM userlogin_cursor INTO @Username

WHILE @@FETCH_STATUS = 0
  BEGIN
      SET @cmd = 'ALTER USER [' + @username + '] WITH LOGIN = [' + @username + ']'
      EXECUTE(@cmd)
      FETCH NEXT FROM userlogin_cursor INTO @Username
  END

CLOSE userlogin_cursor
DEALLOCATE userlogin_cursor 

T-SQL One to One Can't Be Done

I'm preparing a series on Entity Framework, and discovered something yesterday I never knew. In MS SQL Server, you can't successfully model a true one-to-one relationship, where both sides are required. I say "successfully," because you can configure the tables, but they won't work. This is because SQL won't allow deferred constraint resolution, which is fine by me.

The insert statements, below, will fail. Note you have to remove one of the foreign key constraints before you can drop the tables!

create table One (
 OneId int not null identity primary key,
 Name varchar(10) null
)

create table Two (
 OneId int not null primary key references One(OneId),
 Name varchar(10)
)

alter table One add constraint fk1 foreign key (OneId) references Two(OneId)

insert One(Name) values('asdf')
insert Two(Name) values('qwer')

alter table One drop constraint fk1
drop table Two
drop table One

Migrating Google Drive

My wiferecently got her own domain name, and signed up for Google Apps for Business. She's been migrating her email and calendar from previous accounts, including a Google account.
The problem was Drive. She had a mixture of documents in Google Docs format and MS Office. Some items were shared. All in all, it looked like a big mess, complicated by the fact that Google doesn't let you give ownership of your files to someone in another domain. I don't know why. I assume the Ph.Ds who work there didn't properly sacrifice to the Domain Name God.
Also--and this burns me up--Google won't let you download files using their super-secret-must-be-tastier-than-Coke internal format. If you have Drive synchronization set up, you'll see documents with .gdoc extensions, but they're just text files with points to the web site. What this means is that there is no hope of downloading and uploading Google Docs and preserve true fidelity.
Thanks, Google. My files aren't really my files, because you own the format.
So, here's what I did--and what I could have done.
First, I think I could have paid for a tool that would migrate the documents. But I didn't, and I didn't even really look for one. Instead, I used a utility that I like a lot,GDocBackup. It's not only a nice free utility, but the web site has a great picture.

Steps

  1. Go through my wife's old Google Drive account--we'll call it Thing One--and remove me as a shared person.
  2. Go through my Drive and remove her as a shared person.
  3. Use GDocBackup to download all of her files. "Wait!" you interrupt (which is a little rude), "Why didn't you use Drive's ability to download all the files?" Simple. It doesn't preserve folder layout. I'm sure they'd argue that a file can have more than one label, so it's impossible. But then, why does local Drive sync work? Not impressed.
  4. Now here's where I crow about my wife. I told her that I could preserve which files were GDocs by keeping two sets of folders: one with only the Google format docs, and one with everything else. Then, I'd import the first ones with Drive set to convert the files. And she says, "Oh, that's all right. You can just upload them all as Microsoft docs." So, that saves me huge time.
  5. So, in the interests of saving her work, I determine a few files that really must be in Google format, and upload/convert those, then delete them.
  6. I then disconnect my local Drive sync, and reconnect using her new Business account--which we'll call Thing Two.
  7. I copy all of the Thing One files from the GDocBackup folder into the Google Drive folder (Thing Two).
  8. Wait for synchronization to complete.

There. Wasn't that easy?*
I really hope Google makes this process better. What I really don't understand is why they don't allow assigning ownership across domains. I mean, it's probably just a primary key field in a database. But what do I know?
------------------------------
*No, it wasn't.