PRAGMA foreign_keys=ON;
BEGIN TRANSACTION;
--
CREATE TABLE "Transport" (tpNum INTEGER PRIMARY KEY,
	active INTEGER DEFAULT 1, tpTransport TEXT, tpNexthop TEXT,
	tpMx INTEGER DEFAULT 1, tpPort INTEGER,
	UNIQUE (tpTransport,tpNexthop,tpMx,tpPort));
--
CREATE TABLE "RClass" (rcNum INTEGER PRIMARY KEY,
	rcName TEXT NOT NULL, UNIQUE(rcName));
--
CREATE TABLE "Domain" (domNum INTEGER PRIMARY KEY,
	domName TEXT NOT NULL,
	active INTEGER DEFAULT 1, domClass INTEGER DEFAULT 0,
	domSysUser INTEGER DEFAULT 0, domTpNum INTEGER,
	domRcNum INTEGER DEFAULT 30, UNIQUE (domName),
	FOREIGN KEY(domTpNum) REFERENCES Transport(tpNum)
	FOREIGN KEY(domRcNum) REFERENCES RClass(rcNum));
--
CREATE TABLE "Address" (addNum INTEGER PRIMARY KEY,
	addLocalpart TEXT NOT NULL, addDomNum INTEGER NOT NULL,
	active INTEGER DEFAULT 1, addTpNum INTEGER, addRcNum INTEGER,
	FOREIGN KEY(addDomNum) REFERENCES Domain(domNum),
	FOREIGN KEY(addTpNum) REFERENCES Transport(tpNum),
	FOREIGN KEY(addRcNum) REFERENCES RClass(rcNum),
	UNIQUE (addLocalpart, addDomNum));
--
CREATE TABLE "Alias" (aliasKey INTEGER PRIMARY KEY,
	aliasNum INTEGER NOT NULL,
	active INTEGER DEFAULT 1, aliasTarget TEXT NOT NULL,
	FOREIGN KEY(aliasNum) REFERENCES Address(addNum));
--
CREATE TABLE "VAlias" (vaKey INTEGER PRIMARY KEY,
	vaNum INTEGER NOT NULL,
	active INTEGER DEFAULT 1, vaTargetNum INTEGER NOT NULL,
	vaTargetExt TEXT,
	FOREIGN KEY(vaNum) REFERENCES Address(addNum),
	FOREIGN KEY(vaTargetNum) REFERENCES Address(addNum));
--
CREATE TABLE "VMailbox" (vmNum INTEGER PRIMARY KEY,
	active INTEGER DEFAULT 1, vmPasswd TEXT, vmHome TEXT,
	vmUid INTEGER, vmGid INTEGER,
	FOREIGN KEY(vmNum) REFERENCES Address(addNum));
--
CREATE TABLE "BScat" (bsKey INTEGER PRIMARY KEY,
	bsSender TEXT NOT NULL, bsPriority INTEGER,
	bsTarget TEXT NOT NULL, UNIQUE (bsSender, bsPriority));
--
COMMIT;
