64 |
Some migrations have been run:
65 |
66 | {% for info in migration_infos %}
67 | - {{ info }}
68 | {% endfor %}
69 |
70 | {% endif %}
71 | ```
72 |
--------------------------------------------------------------------------------
/Knp/Migration/Manager.php:
--------------------------------------------------------------------------------
1 | schema = $connection->getSchemaManager()->createSchema();
29 | $this->toSchema = clone($this->schema);
30 | $this->connection = $connection;
31 | $this->finder = $finder;
32 | $this->application = $application;
33 |
34 | if(isset($application['migration.migrations_table_name'])) {
35 | $this->migrationsTableName = $application['migration.migrations_table_name'];
36 | }
37 | }
38 |
39 | private function buildSchema(Schema $schema)
40 | {
41 | $queries = $this->schema->getMigrateToSql($schema, $this->connection->getDatabasePlatform());
42 |
43 | foreach ($queries as $query) {
44 | $this->connection->exec($query);
45 | }
46 | }
47 |
48 | private function findMigrations($from)
49 | {
50 | $finder = clone($this->finder);
51 | $migrations = array();
52 |
53 | $finder
54 | ->files()
55 | ->name('*Migration.php')
56 | ->sortByName()
57 | ;
58 |
59 | foreach ($finder as $migration) {
60 | if (preg_match('/^(\d+)_(.*Migration).php$/', basename($migration), $matches)) {
61 |
62 | list(, $version, $class) = $matches;
63 |
64 | if ((int) ltrim($version, 0) > $from) {
65 | require_once $migration;
66 |
67 | $fqcn = '\\Migration\\'.$class;
68 |
69 | if (!class_exists($fqcn)) {
70 | throw new \RuntimeException(sprintf('Could not find class "%s" in "%s"', $fqcn, $migration));
71 | }
72 |
73 | $migrations[] = new $fqcn();
74 | }
75 | }
76 | }
77 |
78 | return $migrations;
79 | }
80 |
81 | public function getMigrationInfos()
82 | {
83 | return $this->migrationInfos;
84 | }
85 |
86 | public function getMigrationExecuted()
87 | {
88 | return $this->migrationExecuted;
89 | }
90 |
91 | public function getCurrentVersion()
92 | {
93 | if (is_null($this->currentVersion)) {
94 | $this->currentVersion = $this->conn->fetchColumn('SELECT ' . $this->migrationsTableName . ' FROM schema_version');
95 | }
96 |
97 | return $this->currentVersion;
98 | }
99 |
100 | public function setCurrentVersion($version)
101 | {
102 | $this->currentVersion = $version;
103 | $this->connection->executeUpdate('UPDATE ' . $this->migrationsTableName . ' SET schema_version = ?', array($version));
104 | }
105 |
106 | public function hasVersionInfo()
107 | {
108 | return $this->schema->hasTable($this->migrationsTableName);
109 | }
110 |
111 | public function createVersionInfo()
112 | {
113 | $schema = clone($this->schema);
114 |
115 | $schemaVersion = $schema->createTable($this->migrationsTableName);
116 | $schemaVersion->addColumn($this->migrationsTableName, 'integer', array('unsigned' => true, 'default' => 0));
117 |
118 | $this->buildSchema($schema);
119 |
120 | $this->connection->insert($this->migrationsTableName, array('schema_version' => 0));
121 | }
122 |
123 | public function migrate()
124 | {
125 | $from = $this->connection->fetchColumn('SELECT ' . $this->migrationsTableName . ' FROM schema_version');
126 | $queries = array();
127 |
128 | $migrations = $this->findMigrations($from);
129 |
130 | if (count($migrations) == 0) {
131 | return null;
132 | }
133 |
134 | foreach ($migrations as $migration) {
135 | $migration->schemaUp($this->toSchema);
136 | }
137 |
138 | $this->buildSchema($this->toSchema);
139 |
140 | foreach ($migrations as $migration) {
141 | $migration->appUp($this->application);
142 | }
143 |
144 | $migrationInfos = array();
145 |
146 | foreach ($migrations as $migration) {
147 | if (null !== $migration->getMigrationInfo()) {
148 | $migrationInfos[$migration->getVersion()] = $migration->getMigrationInfo();
149 | }
150 |
151 | $this->migrationExecuted++;
152 | }
153 |
154 | $this->migrationInfos = $migrationInfos;
155 |
156 | $this->setCurrentVersion($migration->getVersion());
157 |
158 | return true;
159 | }
160 | }
161 |
--------------------------------------------------------------------------------