-- zlucenie tabuliek doc_tree a doc

BEGIN;

-- 1. doc_tree je dost podobna tomu co chceme vo vysledku dosiahnut, takze ju budeme ohybat
ALTER TABLE doc_tree ADD COLUMN text TEXT;
ALTER TABLE doc_tree ADD COLUMN labels VARCHAR;
ALTER TABLE doc_tree ADD COLUMN status SMALLINT NOT NULL DEFAULT 0;

COMMENT ON COLUMN doc_tree.text IS 'telo clanku';
COMMENT ON COLUMN doc_tree.labels IS 'stitky oddelene ciarkami';
COMMENT ON COLUMN doc_tree.status IS 'stav dokoncenia 0 az 100';

UPDATE doc_tree AS dt SET text=(SELECT d.text FROM doc d WHERE d.id=dt.doc) WHERE dt.doc IS NOT NULL;
ALTER TABLE doc_tree DROP COLUMN doc;

-- 2. potom zmenime vazbu v doc_file (zatial tam nic nie je, mozme rovno drop)
ALTER TABLE doc_file DROP COLUMN doc;
ALTER TABLE doc_file ADD COLUMN doc INT NOT NULL REFERENCES doc_tree (id) ON UPDATE CASCADE ON DELETE CASCADE;

-- 3. zhodime doc
DROP TABLE doc;

-- 4. premenujeme doc_tree na doc
ALTER TABLE doc_tree RENAME TO doc;

-- 5. este databazovy index
CREATE INDEX doc_file_doc ON doc_file (doc);

-- 6. a polozka so SEO URL "index" uz nie je validna
UPDATE doc SET url='manual' WHERE url='index';

COMMIT;
