1CREATE_MANY2MANY_TABLES = """
2CREATE TABLE `autotests_dependency_labels` (
3    `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
4    `test_id` integer NOT NULL REFERENCES `autotests` (`id`),
5    `label_id` integer NOT NULL REFERENCES `labels` (`id`),
6    UNIQUE (`test_id`, `label_id`)
7);
8CREATE TABLE `jobs_dependency_labels` (
9    `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
10    `job_id` integer NOT NULL REFERENCES `jobs` (`id`),
11    `label_id` integer NOT NULL REFERENCES `labels` (`id`),
12    UNIQUE (`job_id`, `label_id`)
13);
14"""
15
16def migrate_up(manager):
17    manager.execute('ALTER TABLE labels '
18                    'ADD COLUMN only_if_needed bool NOT NULL')
19    manager.execute_script(CREATE_MANY2MANY_TABLES)
20
21def migrate_down(manager):
22    manager.execute('ALTER TABLE labels DROP COLUMN only_if_needed')
23    manager.execute('DROP TABLE IF EXISTS `autotests_dependency_labels`')
24    manager.execute('DROP TABLE IF EXISTS `jobs_dependency_labels`')
25