public function __construct($name, Connection $driver) { $this->name = $name; $this->fields = new Collection(); if ($driver->isConnected()) { try { if ($driver->getDriverName() == "sqlite") { $stmt = $driver->raw("SELECT sql FROM sqlite_master WHERE name='{$name}'"); } else { $stmt = $driver->raw('DESC ' . $name); } if ($stmt != null) { if ($stmt->execute()) { $stmt->setFetchMode(\PDO::FETCH_CLASS, 'Simplified\\Database\\Schema\\TableField'); while ($record = $stmt->fetch()) { if ($name == "user_groups") { var_dump($record); } $this->fields[$record->field] = $record; } } } } catch (\PDOException $e) { throw new DriverException("Unable to fetch table schema: " . $e->getMessage()); } } }
public function build(Connection $conn) { $sql = $this->toSql(); return strlen($sql) > 0 ? $conn->raw($sql) : false; }
protected function execute(InputInterface $input, OutputInterface $output) { $output->writeln('start migrating...'); $migrations_path = APP_PATH . 'database' . DIRECTORY_SEPARATOR . 'migrations'; if (!file_exists($migrations_path)) { // if dir doesn't exists, nothing is to migrate return; } // get (default) database connection $conf = Config::getAll('database'); $default_config = isset($conf['default']) ? $conf['default'] : null; if ($default_config == null) { throw new ConnectionException('No default connection set'); } $conn = new Connection($default_config); // find migrations table. if something is going wrong // a exception is triggered $migrations = $conn->getDatabaseSchema()->table('migrations'); if ($migrations == null) { $migrations = new Blueprint('migrations'); $migrations->increments('id')->string('name')->unique()->timestamps()->primary('id'); // create table migrations // if something is going wrong a exception is triggered $migrations->build($conn); } $files = array(); if ($handle = opendir($migrations_path)) { while (false !== ($entry = readdir($handle))) { if ($entry != "." && $entry != ".." && !is_dir($entry)) { if (endsWith($entry, ".php")) { $basename = basename($entry, '.php'); $ret = $conn->raw("select name from `migrations` where name='{$basename}' limit 1"); if ($ret != null && $ret->rowCount() == 1) { continue; } $files[] = $migrations_path . DIRECTORY_SEPARATOR . $entry; } } } closedir($handle); } else { return; } foreach ($files as $file) { $content = file_get_contents($file); $lines = explode(PHP_EOL, $content); foreach ($lines as $line) { if (preg_match('/class[\\s]+([a-zA-Z]+)/', $line, $matches)) { include $file; $clazz = $matches[1]; if (!class_exists($clazz)) { throw new IllegalArgumentException("Class {$clazz} doesn't exists"); } $instance = new $clazz(); if (!$instance instanceof MigrateInterface) { throw new IllegalArgumentException("Class {$clazz} doesn't implements MigrateInterface"); } $instance->up(); // register migration in database $conn->raw('insert into migrations (name, created_at) VALUES ("' . basename($file, '.php') . '", null)'); $output->writeln('Migrated table class ' . $clazz); } } } $output->writeln('finished migrating...'); }