5 * Contains Drupal\Console\Command\Shared\MigrationTrait.
8 namespace Drupal\Console\Command\Shared;
10 use Drupal\Core\Database\Connection;
11 use Drupal\Core\Database\Database;
12 use Drupal\Console\Core\Style\DrupalStyle;
13 use Symfony\Component\Console\Input\InputInterface;
16 * Class MigrationTrait
18 * @package Drupal\Console\Command
25 * @param bool $version_tag
26 * @param bool $flatList
28 * @return array list of migrations
30 protected function getMigrations($version_tag = false, $flatList = false, $configuration = [])
32 //Get migration definitions by tag
33 $migrations = array_filter(
34 $this->pluginManagerMigration->getDefinitions(), function ($migration) use ($version_tag) {
35 return !empty($migration['migration_tags']) && in_array($version_tag, $migration['migration_tags']);
39 // Create an array to configure all migration plugins with same configuration
40 $keys = array_keys($migrations);
41 $migration_plugin_configuration = array_fill_keys($keys, $configuration);
43 //Create all migration instances
44 $all_migrations = $this->pluginManagerMigration->createInstances(array_keys($migrations), $migration_plugin_configuration);
47 foreach ($all_migrations as $migration) {
49 $migrations[$migration->id()] = ucwords($migration->label());
51 $migrations[$migration->id()]['tags'] = implode(', ', $migration->getMigrationTags());
52 $migrations[$migration->id()]['description'] = ucwords($migration->label());
58 protected function createDatabaseStateSettings(array $database, $drupal_version)
60 $database_state['key'] = 'upgrade';
61 $database_state['database'] = $database;
62 $database_state_key = 'migrate_drupal_' . $drupal_version;
64 $this->state->set($database_state_key, $database_state);
65 $this->state->set('migrate.fallback_state_key', $database_state_key);
71 protected function getDatabaseDrivers()
73 // Make sure the install API is available.
74 include_once DRUPAL_ROOT . '/core/includes/install.inc';
75 return drupal_get_database_types();
79 * @param DrupalStyle $io
83 public function dbDriverTypeQuestion(DrupalStyle $io)
85 $databases = $this->getDatabaseDrivers();
87 $dbType = $io->choice(
88 $this->trans('commands.migrate.setup.questions.db-type'),
89 array_keys($databases)
98 protected function getDatabaseTypes()
100 $drupal = $this->get('site');
101 return $drupal->getDatabaseTypes();
105 * Determine what version of Drupal the source database contains, copied from \Drupal\migrate_upgrade\MigrationCreationTrait
107 * @param \Drupal\Core\Database\Connection $connection
111 protected function getLegacyDrupalVersion(Connection $connection)
113 // Don't assume because a table of that name exists, that it has the columns
114 // we're querying. Catch exceptions and report that the source database is
117 // Druppal 5/6/7 can be detected by the schema_version in the system table.
118 if ($connection->schema()->tableExists('system')) {
120 $version_string = $connection->query('SELECT schema_version FROM {system} WHERE name = :module', [':module' => 'system'])
122 if ($version_string && $version_string[0] == '1') {
123 if ((int) $version_string >= 1000) {
124 $version_string = '5';
126 $version_string = false;
129 } catch (\PDOException $e) {
130 $version_string = false;
133 // For Drupal 8 (and we're predicting beyond) the schema version is in the
135 elseif ($connection->schema()->tableExists('key_value')) {
136 $result = $connection->query("SELECT value FROM {key_value} WHERE collection = :system_schema and name = :module", [':system_schema' => 'system.schema', ':module' => 'system'])->fetchField();
137 $version_string = unserialize($result);
139 $version_string = false;
142 return $version_string ? substr($version_string, 0, 1) : false;
148 protected function getDBInfo()
150 return $this->database;
154 * @param \Drupal\Console\Core\Style\DrupalStyle $io
158 protected function getDBConnection(DrupalStyle $io, $target, $key)
161 return Database::getConnection($target, $key);
162 } catch (\Exception $e) {
166 $this->trans('commands.migrate.execute.messages.destination-error'),
176 * @param InputInterface $input
177 * @param DrupalStyle $io
179 protected function registerMigrateDB(InputInterface $input, DrupalStyle $io)
181 $dbType = $input->getOption('db-type');
182 $dbHost = $input->getOption('db-host');
183 $dbName = $input->getOption('db-name');
184 $dbUser = $input->getOption('db-user');
185 $dbPass = $input->getOption('db-pass');
186 $dbPrefix = $input->getOption('db-prefix');
187 $dbPort = $input->getOption('db-port');
189 $this->addDBConnection($io, 'upgrade', 'default', $dbType, $dbName, $dbUser, $dbPass, $dbPrefix, $dbPort, $dbHost);
194 * @param DrupalStyle $io
205 protected function addDBConnection(DrupalStyle $io, $key, $target, $dbType, $dbName, $dbUser, $dbPass, $dbPrefix, $dbPort, $dbHost)
207 $database_type = $this->getDatabaseDrivers();
208 $reflection = new \ReflectionClass($database_type[$dbType]);
209 $install_namespace = $reflection->getNamespaceName();
210 // Cut the trailing \Install from namespace.
211 $namespace = substr($install_namespace, 0, strrpos($install_namespace, '\\'));
213 'database' => $dbName,
214 'username' => $dbUser,
215 'password' => $dbPass,
216 'prefix' => $dbPrefix,
219 'namespace' => $namespace,
225 return Database::addConnectionInfo($key, $target, $this->database);
226 } catch (\Exception $e) {
230 $this->trans('commands.migrate.execute.messages.source-error'),