:VERSION 1

BEGIN TRANSACTION

CREATE TABLE [inventoryfolders] (
  [folderID] [varchar](36) NOT NULL default '',
  [agentID] [varchar](36) default NULL,
  [parentFolderID] [varchar](36) default NULL,
  [folderName] [varchar](64) default NULL,
  [type] [smallint] NOT NULL default 0,
  [version] [int] NOT NULL default 0,  
  PRIMARY KEY CLUSTERED
(
	[folderID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

CREATE NONCLUSTERED INDEX [owner] ON [inventoryfolders]
(
	[agentID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]

CREATE NONCLUSTERED INDEX [parent] ON [inventoryfolders]
(
	[parentFolderID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]


CREATE TABLE [inventoryitems] (
  [inventoryID] [varchar](36) NOT NULL default '',
  [assetID] [varchar](36) default NULL,
  [assetType] [int] default NULL,
  [parentFolderID] [varchar](36) default NULL,
  [avatarID] [varchar](36) default NULL,
  [inventoryName] [varchar](64) default NULL,
  [inventoryDescription] [varchar](128) default NULL,
  [inventoryNextPermissions] [int] default NULL,
  [inventoryCurrentPermissions] [int] default NULL,
  [invType] [int] default NULL,
  [creatorID] [varchar](36) default NULL,
  [inventoryBasePermissions] [int] NOT NULL default 0,
  [inventoryEveryOnePermissions] [int] NOT NULL default 0,
  [salePrice] [int] default NULL,
  [saleType] [tinyint] default NULL,
  [creationDate] [int] default NULL,
  [groupID] [varchar](36) default NULL,
  [groupOwned] [bit] default NULL,
  [flags] [int] default NULL,
  PRIMARY KEY CLUSTERED
(
	[inventoryID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]


CREATE NONCLUSTERED INDEX [owner] ON [inventoryitems]
(
	[avatarID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]

CREATE NONCLUSTERED INDEX [folder] ON [inventoryitems]
(
	[parentFolderID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]

COMMIT


:VERSION 2

BEGIN TRANSACTION

ALTER TABLE inventoryitems ADD inventoryGroupPermissions INTEGER NOT NULL default 0

COMMIT

:VERSION 3

/* To prevent any potential data loss issues, you should review this script in detail before running it outside the context of the database designer.*/
BEGIN TRANSACTION

CREATE TABLE dbo.Tmp_inventoryfolders
	(
	folderID uniqueidentifier NOT NULL DEFAULT ('00000000-0000-0000-0000-000000000000'),
	agentID uniqueidentifier NULL DEFAULT (NULL),
	parentFolderID uniqueidentifier NULL DEFAULT (NULL),
	folderName varchar(64) NULL DEFAULT (NULL),
	type smallint NOT NULL DEFAULT ((0)),
	version int NOT NULL DEFAULT ((0))
	)  ON [PRIMARY]

IF EXISTS(SELECT * FROM dbo.inventoryfolders)
	 EXEC('INSERT INTO dbo.Tmp_inventoryfolders (folderID, agentID, parentFolderID, folderName, type, version)
		SELECT CONVERT(uniqueidentifier, folderID), CONVERT(uniqueidentifier, agentID), CONVERT(uniqueidentifier, parentFolderID), folderName, type, version FROM dbo.inventoryfolders WITH (HOLDLOCK TABLOCKX)')

DROP TABLE dbo.inventoryfolders

EXECUTE sp_rename N'dbo.Tmp_inventoryfolders', N'inventoryfolders', 'OBJECT' 

ALTER TABLE dbo.inventoryfolders ADD CONSTRAINT
	PK__inventor__C2FABFB3173876EA PRIMARY KEY CLUSTERED 
	(
	folderID
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

CREATE NONCLUSTERED INDEX owner ON dbo.inventoryfolders
	(
	agentID
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

CREATE NONCLUSTERED INDEX parent ON dbo.inventoryfolders
	(
	parentFolderID
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

COMMIT


:VERSION 4

BEGIN TRANSACTION

CREATE TABLE dbo.Tmp_inventoryitems
	(
	inventoryID uniqueidentifier NOT NULL DEFAULT ('00000000-0000-0000-0000-000000000000'),
	assetID uniqueidentifier NULL DEFAULT (NULL),
	assetType int NULL DEFAULT (NULL),
	parentFolderID uniqueidentifier NULL DEFAULT (NULL),
	avatarID uniqueidentifier NULL DEFAULT (NULL),
	inventoryName varchar(64) NULL DEFAULT (NULL),
	inventoryDescription varchar(128) NULL DEFAULT (NULL),
	inventoryNextPermissions int NULL DEFAULT (NULL),
	inventoryCurrentPermissions int NULL DEFAULT (NULL),
	invType int NULL DEFAULT (NULL),
	creatorID uniqueidentifier NULL DEFAULT (NULL),
	inventoryBasePermissions int NOT NULL DEFAULT ((0)),
	inventoryEveryOnePermissions int NOT NULL DEFAULT ((0)),
	salePrice int NULL DEFAULT (NULL),
	saleType tinyint NULL DEFAULT (NULL),
	creationDate int NULL DEFAULT (NULL),
	groupID uniqueidentifier NULL DEFAULT (NULL),
	groupOwned bit NULL DEFAULT (NULL),
	flags int NULL DEFAULT (NULL),
	inventoryGroupPermissions int NOT NULL DEFAULT ((0))
	)  ON [PRIMARY]

IF EXISTS(SELECT * FROM dbo.inventoryitems)
	 EXEC('INSERT INTO dbo.Tmp_inventoryitems (inventoryID, assetID, assetType, parentFolderID, avatarID, inventoryName, inventoryDescription, inventoryNextPermissions, inventoryCurrentPermissions, invType, creatorID, inventoryBasePermissions, inventoryEveryOnePermissions, salePrice, saleType, creationDate, groupID, groupOwned, flags, inventoryGroupPermissions)
		SELECT CONVERT(uniqueidentifier, inventoryID), CONVERT(uniqueidentifier, assetID), assetType, CONVERT(uniqueidentifier, parentFolderID), CONVERT(uniqueidentifier, avatarID), inventoryName, inventoryDescription, inventoryNextPermissions, inventoryCurrentPermissions, invType, CONVERT(uniqueidentifier, creatorID), inventoryBasePermissions, inventoryEveryOnePermissions, salePrice, saleType, creationDate, CONVERT(uniqueidentifier, groupID), groupOwned, flags, inventoryGroupPermissions FROM dbo.inventoryitems WITH (HOLDLOCK TABLOCKX)')

DROP TABLE dbo.inventoryitems

EXECUTE sp_rename N'dbo.Tmp_inventoryitems', N'inventoryitems', 'OBJECT' 

ALTER TABLE dbo.inventoryitems ADD CONSTRAINT
	PK__inventor__C4B7BC2220C1E124 PRIMARY KEY CLUSTERED 
	(
	inventoryID
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]


CREATE NONCLUSTERED INDEX owner ON dbo.inventoryitems
	(
	avatarID
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

CREATE NONCLUSTERED INDEX folder ON dbo.inventoryitems
	(
	parentFolderID
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

COMMIT

:VERSION 5

# It would be totally crazy to have to recreate the whole table just to change one column type,
# just because MS SQL treats each DEFAULT as a constraint object that must be dropped
# before anything can be done to the column.  Since all defaults here are unnamed, there is
# no easy way to drop them!  The hairy piece of code below removes all DEFAULT constraints
# from InventoryItems.

# SO: anything that's NULLable is by default NULL, so please don't declare DEFAULT(NULL), 
# they do nothing but prevent changes to the columns. If you really
# need to have DEFAULTs or other constraints, give them names so they can be dropped when needed!

BEGIN TRANSACTION
DECLARE @nm varchar(80);
DECLARE x CURSOR LOCAL FORWARD_ONLY READ_ONLY
  FOR SELECT name FROM sys.default_constraints where parent_object_id = OBJECT_ID('inventoryitems');  
OPEN x;
FETCH NEXT FROM x INTO @nm;
WHILE @@FETCH_STATUS = 0
BEGIN
  EXEC('alter table inventoryitems drop ' + @nm);
  FETCH NEXT FROM x INTO @nm;
END
CLOSE x
DEALLOCATE x 
COMMIT     

# all DEFAULTs dropped!

:GO

BEGIN TRANSACTION

# Restoring defaults:   
# NOTE: [inventoryID] does NOT need one: it's NOT NULL PK and a unique Guid must be provided every time anyway!
 
alter table inventoryitems
   add constraint def_baseperm default 0 for inventoryBasePermissions    
alter table inventoryitems
   add constraint def_allperm default 0 for inventoryEveryOnePermissions    
alter table inventoryitems
   add constraint def_grpperm default 0 for inventoryGroupPermissions    
   
COMMIT     
   
:VERSION 7

BEGIN TRANSACTION

# CreatorID goes back to VARCHAR(36) (???)

exec sp_rename 'inventoryitems.CreatorID', 'cr_old', 'COLUMN'

:GO

alter table inventoryitems
   add creatorID varchar(36) NULL
   
:GO   
   
update inventoryitems set creatorID = CONVERT(VARCHAR(36), cr_old)

alter table inventoryitems
   drop column cr_old

COMMIT	
   
:VERSION 8

ALTER TABLE inventoryitems
ADD CONSTRAINT DF_inventoryitems_creatorID 
DEFAULT '00000000-0000-0000-0000-000000000000' FOR creatorID
   
:GO

:VERSION 9

BEGIN TRANSACTION

# CreatorID goes up to VARCHAR(255)

exec sp_rename 'inventoryitems.CreatorID', 'cr_old', 'COLUMN'

:GO

alter table inventoryitems
   add creatorID varchar(255) NULL
   
:GO   
   
update inventoryitems set creatorID = cr_old

alter table inventoryitems
drop CONSTRAINT DF_inventoryitems_creatorID
:GO   

alter table inventoryitems
   drop column cr_old
   :GO   
COMMIT	

ALTER TABLE inventoryitems
ADD CONSTRAINT DF_inventoryitems_creatorID 
DEFAULT '00000000-0000-0000-0000-000000000000' FOR creatorID
   
:GO