public static function generateSchemaFiles() { foreach (ActiveRecord::connections() as $connectionName => $cdata) { if (!isset($cdata['username']) || !isset($cdata['password']) || !isset($cdata['database'])) { continue; } else { try { ActiveRecord::setConnection($connectionName); $connection = ActiveRecord::connection(); $dbname = $connection->selectValue("SELECT DATABASE()"); $tables = $connection->selectValues(sprintf('SHOW TABLES FROM `%s`', $dbname)); } catch (Throwable $e) { continue; } catch (\Exception $e) { continue; } if (!$tables) { throw new Exception\RuntimeException(sprintf('Couldn\'t retrieve table information for connection %s', $connectionName)); } foreach ($tables as $table_name) { $class = 'Rails\\ActiveRecord\\Adapter\\' . $connection->adapterName() . '\\Table'; $data = $class::fetchSchema($connection, $table_name); $path = \Rails::root() . '/db/table_schema/' . $connection->name(); if (!is_dir($path)) { mkdir($path, 0777, true); } $file = $path . '/' . $table_name . '.php'; $contents = "<?php\nreturn "; $contents .= var_export($data, true); $contents .= "\n;"; file_put_contents($file, $contents); } } } }
public function __construct(\Rails\ActiveRecord\Connection $connection = null) { if (!$connection) { $connection = ActiveRecord::connection(Rails::env()); } $this->connection = $connection; $this->buildZfAdapter(); }
public function run() { switch ($this->mainArgv) { case 'generate': $gen = new Generators\Generator($this); $gen->parseCmd(); break; case 'assets': $rules = ['assets' => '', 'action' => '']; $opts = new Zend\Console\Getopt($rules); $argv = $opts->getArguments(); if (empty($argv[1])) { $this->terminate("Missing argument 2"); } \Rails::resetConfig('production'); switch ($argv[1]) { case 'compile:all': \Rails::assets()->setConsole($this); \Rails::assets()->compileAll(); break; case strpos($argv[1], 'compile:') === 0: $parts = explode(':', $argv[1]); if (empty($parts[1])) { $this->terminate("Missing asset name to compile"); } \Rails::assets()->setConsole($this); \Rails::assets()->compileFile($parts[1]); break; default: $this->terminate("Unknown action for assets"); break; } break; case 'routes': $routes = $this->createRoutes(); $rules = ['routes' => '', 'f-s' => '']; $opts = new Zend\Console\Getopt($rules); if ($filename = $opts->getOption('f')) { if (true === $filename) { $logFile = \Rails::config()->paths->log->concat('routes.log'); } else { $logFile = \Rails::root() . '/' . $filename; } file_put_contents($logFile, $routes); } $this->write($routes); break; /** * Install database. */ /** * Install database. */ case 'db:create': $m = new \Rails\ActiveRecord\Migration\Migrator(); $m->loadSchema(); break; /** * Run all/pending migrations. * Creates migrations table as well. */ /** * Run all/pending migrations. * Creates migrations table as well. */ case 'db:migrate': $m = new \Rails\ActiveRecord\Migration\Migrator(); $m->run(); break; /** * Runs seeds. */ /** * Runs seeds. */ case 'db:seed': $m = new \Rails\ActiveRecord\Migration\Migrator(); $m->runSeeds(); break; case 'db:schema:dump': $dumper = new \Rails\ActiveRecord\Schema\Dumper(\Rails\ActiveRecord\ActiveRecord::connection()); $dumper->export(\Rails::root() . '/db/schema.sql'); break; } }
public function __construct() { $this->connection = \Rails\ActiveRecord\ActiveRecord::connection(); }
/** * If called class specifies a connection, such connection will be returned. */ public static function connection() { $name = static::connectionName(); return ActiveRecord::connection($name); }
public function find_joining_table($table1, $table2) { if (isset(self::$_joining_tables[$table1])) { return self::$_joining_tables[$table1]; } $tables = [$table1 . '_' . $table2, $table2 . '_' . $table1]; $sql = "SHOW TABLES LIKE ?"; $stmt = ActiveRecord::connection()->prepare($sql); foreach ($tables as $table) { $stmt->execute([$table]); if ($stmt->fetch(PDO::FETCH_NUM)) { self::$_joining_tables[$table1] = $table; self::$_joining_tables[$table2] = $table; return $table; } } return false; }