Example #1
0
 public function run($options = array())
 {
     Yentu::greet();
     $this->db = DatabaseManipulator::create();
     $this->initializeCodeWriter();
     $files = scandir(Yentu::getPath("migrations"));
     if (count($files) > 2) {
         throw new \yentu\exceptions\CommandException("Cannot run imports. Your migrations directory is not empty");
     }
     $description = $this->db->getDescription();
     $this->code->add('begin()');
     if (isset($description['schemata'])) {
         $this->importSchemata($description['schemata']);
     }
     if (isset($description['tables'])) {
         $this->importTables($description['tables']);
     }
     if (isset($description['views'])) {
         $this->importViews($description['views']);
     }
     $this->importForeignKeys();
     $this->code->add('->end();');
     $this->newVersion = date('YmdHis', time());
     $path = Yentu::getPath("migrations/{$this->newVersion}_import.php");
     file_put_contents($path, $this->code);
     \clearice\ClearIce::output("Created `{$path}`\n");
     if (!$this->db->getAssertor()->doesTableExist('yentu_history')) {
         $this->db->createHistory();
     }
     $this->db->setVersion($this->newVersion);
     $this->db->disconnect();
     return $description;
 }
Example #2
0
 public function testOptionGroups()
 {
     ClearIce::addGroups(['group' => 'location', 'help' => 'Location options'], ['group' => 'testing', 'help' => 'Some testing options']);
     ClearIce::addOptions(array('short' => 'i', 'long' => 'input', 'has_value' => true, 'group' => 'location', 'help' => "specifies where the input files for the wiki are found."), array('short' => 'o', 'long' => 'output', 'has_value' => true, 'group' => 'location', "help" => "specifies where the wiki should be written to"), array('short' => 'v', 'long' => 'verbose', "help" => "displays detailed information about everything that happens"), array('short' => 'x', 'long' => 'create-default-index', 'has_value' => false, "help" => "creates a default index page which lists all the wiki pages in a sorted order"), array('short' => 'd', 'long' => 'some-very-long-option-indeed', 'has_value' => false, 'group' => 'testing', "help" => "an uneccesarily long option which is meant to to see if the wrapping of help lines actually works."), array('short' => 's', 'has_value' => false, 'group' => 'testing', "help" => "a short option only"), array('long' => 'lone-long-option', 'has_value' => false, 'group' => 'testing', "help" => "a long option only"));
     $helpMessage = ClearIce::getHelpMessage();
     $this->assertEquals(file_get_contents('tests/data/help_groups.txt'), $helpMessage);
 }
Example #3
0
 public function run($options = array())
 {
     Yentu::greet();
     $db = DatabaseManipulator::create();
     DatabaseItem::setDriver($db);
     ChangeReverser::setDriver($db);
     $previousMigration = '';
     if (isset($options['default-schema'])) {
         $this->schemaCondition = "default_schema = ?";
         $this->schemaConditionData[] = $options['default-schema'];
     }
     if (empty($options)) {
         $session = $db->getLastSession();
         $operations = $db->query("SELECT id, method, arguments, migration, default_schema FROM yentu_history WHERE {$this->schemaCondition} session = ? ORDER BY id DESC", $this->schemaConditionData + [$session]);
     } else {
         $operations = [];
         foreach ($options['stand_alones'] as $set) {
             $operations += $this->getOperations($db, $set);
         }
     }
     foreach ($operations as $operation) {
         if ($previousMigration !== $operation['migration']) {
             ClearIce::output("Rolling back '{$operation['migration']}' migration" . ($operation['default_schema'] != '' ? " on `{$operation['default_schema']}` schema." : ".") . "\n");
             $previousMigration = $operation['migration'];
         }
         ChangeReverser::call($operation['method'], json_decode($operation['arguments'], true));
         $db->query('DELETE FROM yentu_history WHERE id = ?', array($operation['id']));
     }
 }
Example #4
0
 public function testDefaultValues()
 {
     global $argv;
     $argv = array("test");
     $values = \clearice\ClearIce::parse();
     $this->assertEquals(['has-default' => 'def', 'another-default' => 'def2'], $values);
 }
Example #5
0
 private function displayMigrations($descriptions)
 {
     foreach ($descriptions as $description) {
         if (trim($description) == '') {
             continue;
         }
         ClearIce::output("    {$description}\n");
     }
 }
Example #6
0
 public function testLevelThreshold()
 {
     ClearIce::setOutputLevel(ClearIce::OUTPUT_LEVEL_0);
     ClearIce::output("Hello");
     $this->assertFileNotExists($this->stdout);
     ClearIce::setOutputLevel(ClearIce::OUTPUT_LEVEL_1);
     ClearIce::output("Hello\n");
     $this->assertStringEqualsFile($this->stdout, "Hello\n");
 }
 public function testStringCommandDeclaration()
 {
     global $argv;
     $argv = ['test', 'mycommand'];
     ClearIce::reset();
     ClearIce::setStreamUrl('output', vfsStream::url('std/output'));
     ClearIce::addCommands('mycommand');
     $options = ClearIce::parse();
     $this->assertEquals('mycommand', $options['__command__']);
 }
Example #8
0
 public function testPushPopLevel()
 {
     $this->assertEquals(ClearIce::OUTPUT_LEVEL_1, ClearIce::getOutputLevel());
     ClearIce::pushOutputLevel(ClearIce::OUTPUT_LEVEL_3);
     $this->assertEquals(ClearIce::OUTPUT_LEVEL_3, ClearIce::getOutputLevel());
     ClearIce::popOutputLevel(ClearIce::OUTPUT_LEVEL_1);
     ClearIce::pushOutputLevel(ClearIce::OUTPUT_LEVEL_3);
     ClearIce::pushOutputLevel(ClearIce::OUTPUT_LEVEL_2);
     ClearIce::resetOutputLevel();
     $this->assertEquals(ClearIce::OUTPUT_LEVEL_1, ClearIce::getOutputLevel());
 }
Example #9
0
File: Init.php Project: ntentan/dev
 public function runTask($options)
 {
     ClearIce::output("Setting up app namespace {$options['namespace']} ... ");
     Filesystem::createDirectoryStructure(['src' => ['modules' => ['home'], 'lib'], 'config', 'logs', 'views' => ['default', 'home']], './');
     $data = ['namespace' => $options['namespace'], 'date' => date("Y-m-d H:i:s")];
     $this->writeFile(__DIR__ . "/../../code_templates/misc/htaccess.template", $data, '.htaccess');
     $this->writeFile(__DIR__ . "/../../code_templates/php/index.php.template", $data, 'index.php');
     $this->writeFile(__DIR__ . "/../../code_templates/php/HomeController.php.template", $data, 'src/modules/home/HomeController.php');
     $this->writeFile(__DIR__ . "/../../code_templates/php/home_index.tpl.php.template", $data, 'views/default/home_index.tpl.php');
     $this->writeFile(__DIR__ . "/../../code_templates/php/main.tpl.php.template", $data, 'views/default/main.tpl.php');
     ClearIce::output("OK\n");
 }
 function testHelpCommandUsage()
 {
     global $argv;
     $argv = array('test', 'help', 'generate');
     vfsStream::setup('std');
     $stdout = vfsStream::url('std/output');
     ClearIce::setStreamUrl('output', $stdout);
     ClearIce::setUsage("[input] [options]..");
     ClearIce::setDescription("Simple Wiki version 1.0\nA sample or should I say dummy wiki app to help explain ClearIce. This app practically does nothing.");
     ClearIce::setFootnote("Hope you had a nice time learning about ClearIce. We're pretty sure your cli apps would no longer be boring to work with.\n\nReport bugs to bugs@clearice.tld");
     ClearIce::addHelp();
     ClearIce::parse();
     $this->assertFileEquals('tests/data/help_for_command_usage.txt', vfsStream::url('std/output'));
 }
Example #11
0
 public function createFile($name)
 {
     $this->checkExisting($name);
     $this->checkName($name);
     $this->checkPermission();
     $timestamp = date('YmdHis', time());
     $code = new \yentu\CodeWriter();
     $code->add('');
     $code->add('begin()');
     $code->add('');
     $code->add('->end();');
     $path = Yentu::getPath("migrations/{$timestamp}_{$name}.php");
     file_put_contents($path, $code);
     \clearice\ClearIce::output("Added {$path} for new migration.\n");
 }
Example #12
0
 public function run($options)
 {
     ClearIce::output("Stopping ajumamoro daemon ... ");
     $options = Configuration::init($options);
     $pidFile = Configuration::get('pid_file', './ajumamoro.pid');
     if (file_exists($pidFile) && is_readable($pidFile)) {
         $pid = file_get_contents($pidFile);
         if (posix_kill($pid, SIGTERM)) {
             unlink($pidFile);
             ClearIce::output("OK\n");
         } else {
             ClearIce::output("Failed\nCould not kill running instance.\n");
         }
     } else {
         ClearIce::output("Failed\nNo instances detected.\n");
     }
 }
Example #13
0
 public function run($options)
 {
     if (isset($options['config'])) {
         Config::readPath($options['config'], 'ajumamoro');
     }
     if ($options['daemon'] === true) {
         ClearIce::output("Starting ajumamoro daemon ... ");
         Logger::init(Config::get('ajumamoro:log_file', './ajumamoro.log'), 'ajumamoro');
         if ($this->checkExistingInstance() === false) {
             $pid = $this->startDaemon($options);
             ClearIce::output($pid > 0 ? "OK [PID:{$pid}]\n" : "Failed\n");
         } else {
             ClearIce::output("Failed\nAn instance already exists.\n");
         }
     } else {
         Logger::init('php://output', 'ajumamoro');
         Runner::mainLoop();
     }
 }
Example #14
0
 public function run($options = array())
 {
     Yentu::greet();
     if (file_exists(Yentu::getPath(''))) {
         throw new CommandException("Could not initialize yentu. Your project has already been initialized with yentu.");
     } else {
         if (!is_writable(dirname(Yentu::getPath('')))) {
             throw new CommandException("Your current directory is not writable.");
         }
     }
     $params = $this->getParams($options);
     if (count($params) == 0 && defined('STDOUT')) {
         global $argv;
         throw new CommandException("You didn't provide any parameters for initialization. Please execute " . "`{$argv[0]} init -i` to initialize yentu interractively. " . "You can also try `{$argv[0]} init --help` for more information.");
     }
     $this->createConfigFile($params);
     Config::readPath(Yentu::getPath('config'), 'yentu');
     $db = \yentu\DatabaseManipulator::create($params);
     if ($db->getAssertor()->doesTableExist('yentu_history')) {
         throw new CommandException("Could not initialize yentu. Your database has already been initialized with yentu.");
     }
     $db->createHistory();
     $db->disconnect();
     ClearIce::output("Yentu successfully initialized.\n");
 }
Example #15
0
    /**
     * Display the greeting for the CLI user interface.
     */
    public static function greet()
    {
        $version = Yentu::getVersion();
        $welcome = <<<WELCOME
Yentu Database Migration Tool
Version {$version}


WELCOME;
        ClearIce::output($welcome);
    }
Example #16
0
 public function run($options)
 {
     \clearice\ClearIce::output(json_encode($options));
 }
Example #17
0
 public function createHistory()
 {
     try {
         $this->connection->describeTable('yentu_history');
     } catch (\ntentan\atiaa\exceptions\TableNotFoundException $e) {
         ClearIce::pushOutputLevel(ClearIce::OUTPUT_LEVEL_0);
         $this->addTable(array('schema' => '', 'name' => 'yentu_history'));
         $this->addColumn(array('default' => null, 'schema' => '', 'nulls' => true, 'length' => null, 'table' => 'yentu_history', 'name' => 'session', 'type' => 'string'));
         $this->addColumn(array('default' => null, 'schema' => '', 'nulls' => false, 'length' => null, 'table' => 'yentu_history', 'name' => 'version', 'type' => 'string'));
         $this->addColumn(array('default' => null, 'schema' => '', 'nulls' => true, 'length' => null, 'table' => 'yentu_history', 'name' => 'method', 'type' => 'string'));
         $this->addColumn(array('default' => null, 'schema' => '', 'nulls' => true, 'length' => null, 'table' => 'yentu_history', 'name' => 'arguments', 'type' => 'text'));
         $this->addColumn(array('default' => null, 'schema' => '', 'nulls' => true, 'length' => null, 'table' => 'yentu_history', 'name' => 'migration', 'type' => 'string'));
         $this->addColumn(array('default' => null, 'schema' => '', 'nulls' => true, 'length' => null, 'table' => 'yentu_history', 'name' => 'default_schema', 'type' => 'string'));
         $this->addColumn(array('default' => null, 'schema' => '', 'nulls' => true, 'length' => null, 'table' => 'yentu_history', 'name' => 'id', 'type' => 'integer'));
         $this->addPrimaryKey(array('schema' => '', 'table' => 'yentu_history', 'name' => 'yentu_history_pk', 'columns' => array('id')));
         $this->addAutoPrimaryKey(array('schema' => '', 'table' => 'yentu_history', 'column' => 'id'));
         ClearIce::popOutputLevel();
     }
 }
Example #18
0
 public function reverse()
 {
     if ($this->driver === null) {
         return;
     }
     ClearIce::output("Attempting to reverse all changes ... ");
     if ($this->getChanges() > 0) {
         ClearIce::pushOutputLevel(0);
         $rollback = new \yentu\commands\Rollback();
         $rollback->run(array());
         ClearIce::popOutputLevel();
     }
     ClearIce::output("OK\n");
 }
Example #19
0
 /**
  * @expectedException \yentu\exceptions\CommandException
  */
 public function testUnwritable()
 {
     vfsStream::setup('home', 0444);
     $initCommand = new \yentu\commands\Init();
     \yentu\Yentu::setDefaultHome(vfsStream::url("home/yentu"));
     ClearIce::setOutputLevel(ClearIce::OUTPUT_LEVEL_0);
     $initCommand->run(array('driver' => 'postgresql', 'host' => $GLOBALS['DB_HOST'], 'dbname' => $GLOBALS['DB_NAME'], 'user' => $GLOBALS['DB_USER'], 'password' => $GLOBALS['DB_PASSWORD']));
 }
Example #20
0
 public function testMultiOptions()
 {
     global $argv;
     $argv = array("test.php", "--some-multi-option=one", "--some-multi-option=two", "-mthree");
     ClearIce::addOptions(array('long' => 'some-multi-option', 'short' => 'm', 'multi' => true, 'has_value' => true));
     $this->assertEquals(array('some-multi-option' => array('one', 'two', 'three')), ClearIce::parse());
 }
 public function testSkipping2()
 {
     global $argv;
     $argv = array('wiki', '--output', '-i', '/var/input');
     $this->assertEquals(array('output' => true, 'input' => '/var/input'), ClearIce::parse());
 }
Example #22
0
 protected function setupStreams()
 {
     vfsStream::setup('home');
     \yentu\Yentu::setDefaultHome(vfsStream::url('home/yentu'));
     \clearice\ClearIce::setStreamUrl('output', vfsStream::url('home/output.txt'));
 }
Example #23
0
 public function testRequiredDefault()
 {
     file_put_contents($this->stdin, "\n");
     $this->assertEquals('def', ClearIce::getResponse('Fails first', array('required' => true, 'default' => 'def')));
     $this->assertStringEqualsFile($this->stdout, 'Fails first [def]: ');
 }
Example #24
0
File: cli.php Project: codogh/yentu
        $class = "\\yentu\\commands\\" . ucfirst($options['__command__']);
        unset($options['__command__']);
        Config::readPath(Yentu::getPath('config'), 'yentu');
        $command = new $class();
        $command->run($options);
    } else {
        ClearIce::output(ClearIce::getHelpMessage());
    }
} catch (\yentu\exceptions\CommandException $e) {
    ClearIce::resetOutputLevel();
    ClearIce::error("Error! " . $e->getMessage() . "\n");
} catch (\ntentan\atiaa\exceptions\DatabaseDriverException $e) {
    ClearIce::resetOutputLevel();
    ClearIce::error("Database driver failed: " . $e->getMessage() . "\n");
    Yentu::reverseCommand($command);
} catch (\yentu\exceptions\DatabaseManipulatorException $e) {
    ClearIce::resetOutputLevel();
    ClearIce::error("Failed to perform database action: " . $e->getMessage() . "\n");
    Yentu::reverseCommand($command);
} catch (\ntentan\atiaa\DescriptionException $e) {
    ClearIce::resetOutputLevel();
    ClearIce::error("Failed to perform database action: " . $e->getMessage() . "\n");
    Yentu::reverseCommand($command);
} catch (\yentu\exceptions\SyntaxErrorException $e) {
    ClearIce::resetOutputLevel();
    ClearIce::error("Error found in syntax: {$e->getMessage()}\n");
    Yentu::reverseCommand($command);
} catch (\PDOException $e) {
    ClearIce::resetOutputLevel();
    ClearIce::error("Failed to connect to database: {$e->getMessage()}\n");
}
Example #25
0
 public function outputProgress()
 {
     if ($this->expectedOperations > 0) {
         if ($this->operations % 74 === 0) {
             ClearIce::output(sprintf("%4d%%\n", $this->operations / $this->expectedOperations * 100));
         } else {
             ClearIce::output(sprintf("%4d%%\n", $this->operations / $this->expectedOperations * 100), ClearIce::OUTPUT_LEVEL_2);
         }
     }
 }