DROP TABLE IF EXISTS zetteln;
DROP TABLE IF EXISTS zettle_relationships;
DROP INDEX IF EXISTS zetteln_ids;
DROP TABLE IF EXISTS favorites;

CREATE TABLE zetteln (
    id TEXT NOT NULL PRIMARY KEY,
    content TEXT NOT NULL,
    kind TEXT NOT NULL,
	location INTEGER NOT NULL,
	creation_date DATETIME NOT NULL,
	updated_date DATETIME NOT NULL,
	lastview_date DATETIME NOT NULL,
	deleted_date DATETIME NULL
);

CREATE INDEX zettle_ids ON zetteln (id);

CREATE TABLE favorites (
	id TEXT NOT NULL,
    location INTEGER NOT NULL,
    FOREIGN KEY (id) REFERENCES zetteln (id) ON DELETE CASCADE
);

CREATE TABLE zettle_relationships (
    zettle_id TEXT NOT NULL,
    parent_id TEXT NOT NULL,
    location INTEGER NOT NULL,
    kind TEXT NOT NULL,
	-- If either zettle disappears, we want all the edges to disappear as well.
    FOREIGN KEY (zettle_id) REFERENCES zetteln (id) ON DELETE CASCADE,
    FOREIGN KEY (parent_id) REFERENCES zetteln (id) ON DELETE CASCADE
);

