diff --git a/.gitmodules b/.gitmodules index 2e44f28..4339170 100644 --- a/.gitmodules +++ b/.gitmodules @@ -34,3 +34,6 @@ [submodule "modules/unittest"] path = modules/unittest url = git@github.com:kohana/unittest.git +[submodule "modules/migrations"] + path = modules/migrations + url = git@github.com:kohana-minion/tasks-migrations.git diff --git a/application/bootstrap.php b/application/bootstrap.php index 1b99441..5769a98 100644 --- a/application/bootstrap.php +++ b/application/bootstrap.php @@ -116,6 +116,7 @@ Kohana::modules(array( 'minion' => MODPATH.'minion', // CLI framework 'config-writer' => MODPATH.'config-writer', // Write to PHP configs 'cache' => MODPATH.'cache', // Object caching + 'migrations' => MODPATH.'migrations', // SQL migrations )); /** diff --git a/application/migrations/kangana/20140205200807.php b/application/migrations/kangana/20140205200807.php new file mode 100644 index 0000000..78c0cc2 --- /dev/null +++ b/application/migrations/kangana/20140205200807.php @@ -0,0 +1,141 @@ +query(NULL, " +CREATE TABLE IF NOT EXISTS `clients` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `email` varchar(255) NOT NULL, + `name` varchar(255) NOT NULL, + `token` varchar(64) NOT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB;"); + + $db->query(NULL, " +CREATE TABLE IF NOT EXISTS `clients_courses` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `client_id` int(11) DEFAULT NULL, + `course_id` int(11) DEFAULT NULL, + PRIMARY KEY (`id`), + KEY `fk_clients_courses_1_idx` (`client_id`), + KEY `fk_clients_courses_2_idx` (`course_id`) +) ENGINE=InnoDB;"); + + $db->query(NULL, " +CREATE TABLE IF NOT EXISTS `letters` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `text` longtext, + `course_id` int(11) DEFAULT NULL, + `order` int(11) DEFAULT NULL, + `subject` varchar(128) NOT NULL, + PRIMARY KEY (`id`), + KEY `fk_letters_1_idx` (`course_id`) +) ENGINE=InnoDB;"); + + $db->query(NULL, " +CREATE TABLE IF NOT EXISTS `pages` ( + `id` int(11) unsigned NOT NULL AUTO_INCREMENT, + `name` text NOT NULL, + `content` longtext NOT NULL, + `is_draft` int(1) NOT NULL DEFAULT '0', + `posted_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + PRIMARY KEY (`id`) +) ENGINE=InnoDB;"); + + $db->query(NULL, " +CREATE TABLE IF NOT EXISTS `courses` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `title` varchar(100) DEFAULT NULL, + `price` decimal(10,2) DEFAULT '0.00', + `period` int(11) DEFAULT NULL COMMENT 'period in days (1 = daily, 7 = weekly etc.)', + `description` tinytext, + PRIMARY KEY (`id`) +) ENGINE=InnoDB;"); + + $db->query(NULL, " +CREATE TABLE IF NOT EXISTS `tasks` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `letter_id` int(11) DEFAULT NULL, + `client_id` int(11) DEFAULT NULL, + `date` date DEFAULT NULL, + `status` int(11) DEFAULT NULL COMMENT 'status flag (see php model)', + PRIMARY KEY (`id`), + KEY `fk_mail_queue_1_idx` (`client_id`), + KEY `fk_mail_queue_2_idx` (`letter_id`) +) ENGINE=InnoDB;"); + + $db->query(NULL, " +CREATE TABLE IF NOT EXISTS `subscriptions` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `title` varchar(100) DEFAULT NULL, + `price` decimal(10,2) DEFAULT NULL, + `description` tinytext, + PRIMARY KEY (`id`) +) ENGINE=InnoDB;"); + + $db->query(NULL, " +CREATE TABLE IF NOT EXISTS `clients_subscriptions` ( + `id` int(11) NOT NULL, + `client_id` int(11) NOT NULL, + `subscription_id` int(11) NOT NULL, + PRIMARY KEY (`id`), + KEY `fk_table1_2_idx` (`client_id`), + KEY `fk_table1_1_idx` (`subscription_id`), + CONSTRAINT `fk_table1_1` FOREIGN KEY (`subscription_id`) REFERENCES `subscriptions` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION, + CONSTRAINT `fk_table1_2` FOREIGN KEY (`client_id`) REFERENCES `clients` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION +) ENGINE=InnoDB;"); + + $db->query(NULL, " +CREATE TABLE IF NOT EXISTS `instants` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `subject` varchar(128) DEFAULT NULL, + `text` longtext, + `subscription_id` int(11) DEFAULT NULL, + `is_draft` int(11) DEFAULT '0', + `timestamp` timestamp NULL DEFAULT CURRENT_TIMESTAMP, + PRIMARY KEY (`id`), + KEY `fk_instants_1_idx` (`subscription_id`), + CONSTRAINT `fk_instants_1` FOREIGN KEY (`subscription_id`) REFERENCES `subscriptions` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION +) ENGINE=InnoDB;"); + + $db->query(NULL, " +ALTER TABLE `clients_courses` + ADD CONSTRAINT `fk_clients_courses_1` FOREIGN KEY (`client_id`) REFERENCES `clients` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION, + ADD CONSTRAINT `fk_clients_courses_2` FOREIGN KEY (`course_id`) REFERENCES `courses` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION;"); + + $db->query(NULL, " +ALTER TABLE `letters` + ADD CONSTRAINT `fk_letters_1` FOREIGN KEY (`course_id`) REFERENCES `courses` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION;"); + + $db->query(NULL, " +ALTER TABLE `tasks` + ADD CONSTRAINT `fk_mail_queue_1` FOREIGN KEY (`client_id`) REFERENCES `clients` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION, + ADD CONSTRAINT `fk_mail_queue_2` FOREIGN KEY (`letter_id`) REFERENCES `letters` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION;"); + } + + /** + * Run queries needed to remove this migration + * + * @param Kohana_Database $db Database connection + */ + public function down(Kohana_Database $db) + { + $db->query(NULL, 'DROP TABLE IF EXISTS `instants`;'); + $db->query(NULL, 'DROP TABLE IF EXISTS `clients_courses`;'); + $db->query(NULL, 'DROP TABLE IF EXISTS `clients_subscriptions`;'); + $db->query(NULL, 'DROP TABLE IF EXISTS `tasks`;'); + $db->query(NULL, 'DROP TABLE IF EXISTS `letters`;'); + $db->query(NULL, 'DROP TABLE IF EXISTS `pages`;'); + $db->query(NULL, 'DROP TABLE IF EXISTS `courses`;'); + $db->query(NULL, 'DROP TABLE IF EXISTS `subscriptions`;'); + $db->query(NULL, 'DROP TABLE IF EXISTS `clients`;'); + } + +} diff --git a/modules/migrations b/modules/migrations new file mode 160000 index 0000000..5f58475 --- /dev/null +++ b/modules/migrations @@ -0,0 +1 @@ +Subproject commit 5f584751d91f0348e96a989ed2b8d0ed399dbc1f