ZendDbSchema
Schema management for Zend Framework
 All Classes Namespaces Functions Pages
Migration.php
1 <?php
30  extends Zend_Tool_Project_Provider_Abstract
31  implements Zend_Tool_Framework_Provider_Pretendable
32 {
46  public static function createResource(Zend_Tool_Project_Profile $profile, $useSchema)
47  {
50 
51  // check to see if a model already exists
52  $existingMigrationFile = $profile->search(array(
53  'migrationsDirectory',
54  'migrationFile' => array('filesystemName' => $file))
55  );
56 
57  if ($existingMigrationFile !== false) {
58  throw new Zend_Tool_Project_Provider_Exception(
59  'Migration ' . $file . ' already exists.'
60  );
61  }
62 
63  $newMigration = $profile->createResourceAt(
64  'migrationsDirectory', // where to create at
65  'migrationFile', // what to create
66  array('filesystemName' => $file, 'useSchema' => $useSchema) // attrs to initiate with
67  );
68 
69  return $newMigration;
70  }
71 
78  public function getContextClasses()
79  {
80  return array(
81  'ZendDbSchema_Tool_Project_Context_Migration_File',
82  'ZendDbSchema_Tool_Project_Context_Migration_Directory'
83  );
84  }
85 
93  public function create($useSchema = true)
94  {
95  $profile = $this->_bootstrap();
96 
97  $migrationFile = self::createResource($profile, $useSchema);
98 
99  if ($this->_registry->getRequest()->isPretend()) {
100  $this->_registry->getResponse()->appendContent(
101  'Would create migration at ' . $migrationFile->getPath()
102  );
103  } else {
104  $this->_registry->getResponse()->appendContent(
105  'Creating migration at ' . $migrationFile->getPath()
106  );
107  $migrationFile->create();
108  $this->_storeProfile();
109  }
110  }
111 
116  public function show()
117  {
118  $profile = $this->_bootstrap();
119 
120  $profileSearchParams = array('migrationsDirectory');
121 
122  $directory = $profile->search($profileSearchParams);
123 
124  if (!$directory instanceof Zend_Tool_Project_Profile_Resource) {
125  return false;
126  }
127  $blockize = new Zend_Tool_Framework_Client_Console_ResponseDecorator_Blockize;
128  $colorizer = new Zend_Tool_Framework_Client_Console_ResponseDecorator_Colorizer;
129 
130  $colors = array('white', 'yellow', 'green');
131 
132  foreach ($directory as $i => $migrationFile) {
133  $change = ZendDbSchema_Db_Migration_Change::fromFile($migrationFile->getPath());
134  $version = $change->getVersion();
135 
136  $version = preg_replace('/(\d{4})(\d{2})(\d{2})\_(\d{2})(\d{2})(\d{2})\_(\d{2})/', '$1-$2-$3 $4:$5:$6.$7', $version);
137 
138  $message = "";
139  $color = $colors[$i % 2];
140  if ($change->compareVersion($directory->getVersion(), '=')) {
141  $message .= '*';
142  $color = 'green';
143  }
144  $message .= "\t" . $blockize->decorate($version, 25) . $change->getDescription();
145 
146  $message = $colorizer->decorate($message, $color);
147 
148  echo $message . PHP_EOL;
149  }
150  }
151 
152  public function rollup()
153  {
154  $profile = $this->_bootstrap();
155 
156  $profileSearchParams = array('migrationsDirectory');
157 
158  $directory = $profile->search($profileSearchParams);
159 
160  if (!$directory instanceof Zend_Tool_Project_Profile_Resource) {
161  return false;
162  }
163 
164  $directory->getVersion();
165 
166  foreach ($directory as $i => $migrationFile) {
167  $change = ZendDbSchema_Db_Migration_Change::fromFile($migrationFile->getPath());
168  if ($change->compareVersion($directory->getVersion(), '<')) {
169 
170  $directory->setVersion($change->getVersion());
171 
172  $this->_storeProfile();
173 
174  $message = "Updated to " . $change->getVersion();
175  echo $message . PHP_EOL;
176  break;
177  }
178  }
179  }
180 
185  public function status()
186  {
187  $this->_bootstrap();
188 
189  $directory = $profile->search($profileSearchParams);
190 
191  if (!$directory instanceof Zend_Tool_Project_Profile_Resource) {
192  return false;
193  }
194 
195  $manager = new ZendDbSchema_Db_Migration_Manager(
196  $directory->getVersion(),
197  $directory->getChildren()
198  );
199  $manager->isDirty();
200  }
201 
202  protected function _bootstrap()
203  {
204  $profile = $this->_loadProfile(self::NO_PROFILE_THROW_EXCEPTION);
205 
206  $bootstrapResource = $this->_loadedProfile->search('BootstrapFile');
207 
208  /* @var $zendApp Zend_Application */
209  $zendApp = $bootstrapResource->getApplicationInstance();
210 
211  try {
212  $zendApp->bootstrap('db');
213  Zend_Loader_Autoloader::getInstance()->registerNamespace('ZendDbSchema');
214  } catch (Zend_Application_Exception $e) {
215  throw new Zend_Tool_Project_Provider_Exception('Db resource not available, you might need to configure a DbAdapter.');
216  return;
217  }
218  return $profile;
219  }
220 }