:VERSION 1

BEGIN TRANSACTION;

CREATE TABLE inventoryfolders (
  "folderID" varchar(36) NOT NULL default '' PRIMARY KEY,
  "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
);


CREATE INDEX owner ON inventoryfolders
(
	"agentID" ASC
);

CREATE INDEX parent ON inventoryfolders
(
	"parentFolderID" ASC
);


CREATE TABLE inventoryitems (
  "inventoryID" varchar(36) NOT NULL default '' Primary Key,
  "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" smallint default NULL,
  "creationDate" int default NULL,
  "groupID" varchar(36) default NULL,
  "groupOwned" boolean default NULL,
  "flags" int default NULL
);


CREATE INDEX ii_owner ON inventoryitems
(
	"avatarID" ASC
);

CREATE INDEX ii_folder ON inventoryitems
(
	"parentFolderID" ASC
);

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 cotext of the database designer.*/
BEGIN TRANSACTION;

CREATE TABLE Tmp_inventoryfolders
	(
	"folderID" uuid NOT NULL DEFAULT ('00000000-0000-0000-0000-000000000000'),
	"agentID" uuid NULL DEFAULT (NULL),
	"parentFolderID" uuid NULL DEFAULT (NULL),
	"folderName" varchar(64) NULL DEFAULT (NULL),
	"type" smallint NOT NULL DEFAULT ((0)),
	"version" int NOT NULL DEFAULT ((0))
	);

 INSERT INTO Tmp_inventoryfolders ("folderID", "agentID", "parentFolderID", "folderName", type, version)
   SELECT cast("folderID" as uuid), cast("agentID" as uuid), cast("parentFolderID" as uuid), "folderName", "type", "version"
   FROM inventoryfolders;

DROP TABLE inventoryfolders;

alter table Tmp_inventoryfolders rename to inventoryfolders;

ALTER TABLE inventoryfolders ADD CONSTRAINT
	PK__inventor__C2FABFB3173876EA PRIMARY KEY 
	(
	"folderID"
	);

CREATE INDEX owner ON inventoryfolders
	(
	"agentID"
	);

CREATE INDEX parent ON inventoryfolders
	(
	"parentFolderID"
	);

COMMIT;


:VERSION 4

BEGIN TRANSACTION;

CREATE TABLE Tmp_inventoryitems
	(
	"inventoryID" uuid NOT NULL DEFAULT ('00000000-0000-0000-0000-000000000000'),
	"assetID" uuid NULL DEFAULT (NULL),
	"assetType" int NULL DEFAULT (NULL),
	"parentFolderID" uuid NULL DEFAULT (NULL),
	"avatarID" uuid 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" uuid NULL DEFAULT (NULL),
	"inventoryBasePermissions" int NOT NULL DEFAULT ((0)),
	"inventoryEveryOnePermissions" int NOT NULL DEFAULT ((0)),
	"salePrice" int NULL DEFAULT (NULL),
	"SaleType" smallint NULL DEFAULT (NULL),
	"creationDate" int NULL DEFAULT (NULL),
	"groupID" uuid NULL DEFAULT (NULL),
	"groupOwned" boolean NULL DEFAULT (NULL),
	"flags" int NULL DEFAULT (NULL),
	"inventoryGroupPermissions" int NOT NULL DEFAULT ((0))
	);


	 INSERT INTO Tmp_inventoryitems ("inventoryID", "assetID", "assetType", "parentFolderID", "avatarID", "inventoryName", "inventoryDescription", "inventoryNextPermissions", "inventoryCurrentPermissions", "invType", "creatorID", "inventoryBasePermissions", "inventoryEveryOnePermissions", "salePrice", "SaleType", "creationDate", "groupID", "groupOwned", "flags", "inventoryGroupPermissions")
		SELECT cast("inventoryID" as uuid), cast("assetID" as uuid), "assetType", cast("parentFolderID" as uuid), cast("avatarID" as uuid), "inventoryName", "inventoryDescription", "inventoryNextPermissions", "inventoryCurrentPermissions", "invType", cast("creatorID" as uuid), "inventoryBasePermissions", "inventoryEveryOnePermissions", "salePrice", "SaleType", "creationDate", cast("groupID" as uuid), "groupOwned", "flags", "inventoryGroupPermissions" 
		FROM inventoryitems ;

DROP TABLE inventoryitems;

alter table Tmp_inventoryitems rename to inventoryitems;

ALTER TABLE inventoryitems ADD CONSTRAINT
	PK__inventor__C4B7BC2220C1E124 PRIMARY KEY 
	(
	"inventoryID"
	);


CREATE INDEX ii2_owner ON inventoryitems
	(
	"avatarID"
	);

CREATE INDEX ii2_folder ON inventoryitems
	(
	"parentFolderID"
	);

COMMIT;

:VERSION 5


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
   alter column "inventoryBasePermissions" set default 0;    
alter table inventoryitems
   alter column "inventoryEveryOnePermissions" set default 0;  
alter table inventoryitems
   alter column "inventoryGroupPermissions" set default 0 ;   
   
COMMIT     ;
   
:VERSION 7

BEGIN TRANSACTION;

-- # "creatorID" goes back to VARCHAR(36) (???)

alter table inventoryitems
  alter column "creatorID" type varchar(36);

COMMIT	;
   
:VERSION 8

ALTER TABLE inventoryitems
 alter column "creatorID" set DEFAULT '00000000-0000-0000-0000-000000000000';
   

:VERSION 9

BEGIN TRANSACTION;

--# "creatorID" goes up to VARCHAR(255)

alter table inventoryitems
  alter column "creatorID" type varchar(255);
  
Commit;

:VERSION 10

BEGIN TRANSACTION;

Alter table inventoryitems Rename Column "SaleType" to "saleType";

Commit;

