PageRenderTime 45ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/framework/experimental/migration/Migration.php

http://zoop.googlecode.com/
PHP | 83 lines | 62 code | 13 blank | 8 comment | 3 complexity | 5a15233a83e787518a9b4ceeb1b79f22 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  1. <?php
  2. class Migration
  3. {
  4. static function initDb()
  5. {
  6. // create the migration table if it does not exist
  7. $schema = SqlGetSchema();
  8. if(!$schema->tableExists('migration'))
  9. {
  10. $sql = "create table migration (
  11. id serial primary key,
  12. name text not null,
  13. applied int2 not null default 0)";
  14. SqlAlterSchema($sql);
  15. }
  16. }
  17. // static
  18. function getAllMigrationNames()
  19. {
  20. $filenames = ListDir(getcwd() . '/migrations', array('extentions' => array('php')));
  21. $versions = array();
  22. foreach($filenames as $thisFilename)
  23. {
  24. $parts = explode('_', $thisFilename);
  25. $version = $parts[0];
  26. $versions[$thisFilename] = $version;
  27. }
  28. return $versions;
  29. }
  30. // static
  31. function filenameFromVersion($version)
  32. {
  33. $filenames = ListDir(getcwd() . '/migrations', array('extentions' => array('php')));
  34. foreach($filenames as $thisFilename)
  35. {
  36. $parts = explode('_', $thisFilename);
  37. $thisVersion = str_replace('.', '_', $parts[0]);
  38. if($version == $thisVersion)
  39. return $thisFilename;
  40. }
  41. trigger_error("version not found: " . $version);
  42. }
  43. // static
  44. function getAllAppiedMigrationNames()
  45. {
  46. return SqlFetchColumn("select name from migration where applied = 1", array());
  47. }
  48. // static
  49. function apply($filename, $name)
  50. {
  51. include_once(getcwd() . '/migrations/' . $filename);
  52. $className = 'Migration_' . str_replace('.', '_', $name);
  53. $migration = new $className();
  54. $migration->up();
  55. // mark it as applied
  56. SqlUpsertRow('migration', array('name' => $name), array('applied' => 1));
  57. print_r($migration);
  58. }
  59. // static
  60. function undo($filename, $name)
  61. {
  62. include_once(getcwd() . '/migrations/' . $filename);
  63. $className = 'Migration_' . str_replace('.', '_', $name);
  64. $migration = new $className();
  65. $migration->down();
  66. // mark it as applied
  67. SqlUpsertRow('migration', array('name' => $name), array('applied' => 0));
  68. print_r($migration);
  69. }
  70. }