-- groups
CREATE TABLE
	groups(
		"id" SERIAL,
		"name" VARCHAR(63) NOT NULL,
		"label" VARCHAR(255) NOT NULL,
		UNIQUE ("name"),
		UNIQUE ("id")
	)
;

-- user_groups
CREATE TABLE
	user_groups(
		"id" SERIAL,
		"user_id" INTEGER NOT NULL,
		"group_id" INTEGER NOT NULL,
		FOREIGN KEY ("user_id") REFERENCES "users"("id"),
		FOREIGN KEY ("group_id") REFERENCES "groups"("id"),
		UNIQUE ("user_id","group_id"),
		UNIQUE ("id")
	)
;

-- calendar_access_attributed_group
CREATE TABLE
	calendar_access_attributed_group(
		"id" SERIAL,
		"calendar_id" INTEGER NOT NULL,
		"group_id" INTEGER NOT NULL,
		"level" INTEGER NOT NULL,
		FOREIGN KEY ("calendar_id") REFERENCES "calendars"("id"),
		FOREIGN KEY ("group_id") REFERENCES "groups"("id"),
		UNIQUE ("calendar_id","group_id"),
		UNIQUE ("id")
	)
;
COMMENT ON COLUMN calendar_access_attributed_group.level IS '0:none | 1:view | 2:edit | 3:admin';

-- calendar_access_attributed_user
ALTER TABLE
	calendar_access_attributed
	RENAME TO
		calendar_access_attributed_user
;

-- meta
UPDATE _meta SET revision = 'r6';
