1
0

migration.abstract.class.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace Juju\PDO;
  3. require_once(realpath(dirname(__DIR__).'/pdo.class.php'));
  4. use Juju\{PDO, PDO\Transaction, Settings};
  5. abstract class Migration {
  6. public abstract static function up(Transaction $pdo);
  7. public abstract static function down(Transaction $pdo);
  8. public abstract static function change(Transaction $pdo, string $direction);
  9. const MIGRATE_UP = 'up';
  10. const MIGRATE_DOWN = 'down';
  11. private static $pdo;
  12. private static $date;
  13. private static $version;
  14. final public static function version(){
  15. $name = get_called_class();
  16. return substr($name, strrpos($name, '\\') + 1);
  17. }
  18. final public static function version_table(){
  19. if(!class_exists("Juju\\Settings")){
  20. throw new \Exception("Settings not loaded");
  21. }
  22. return Settings::get('db.versions');
  23. }
  24. final public static function get_current_version(bool $force = false){
  25. if(is_null(self::$version) || $force){
  26. if(is_null(self::$pdo)){
  27. throw new \Exception("Migration is not bound to a data source");
  28. }
  29. $table_name = self::version_table();
  30. $query = self::$pdo->query("select max(version) version from `{$table_name}`");
  31. self::$version = $query->fetch()['version'];
  32. $query->closeCursor();
  33. }
  34. return self::$version;
  35. }
  36. final public static function get_last_install_date(bool $force = false){
  37. if(is_null(self::$date) || $force){
  38. if(is_null(self::$pdo)){
  39. throw new \Exception("Migration is not bound to a data source");
  40. }
  41. $table_name = self::version_table();
  42. $query = self::$pdo->query("select max(date) date from `{$table_name}`");
  43. self::$date = $query->fetch()['date'];
  44. $query->closeCursor();
  45. }
  46. return self::$date;
  47. }
  48. final public static function installed(){
  49. $pdo = self::$pdo;
  50. $count = $pdo->exec(
  51. "select 1 ".
  52. "from `".self::version_table()."` ".
  53. "where version = {$pdo->quote(static::version())}"
  54. );
  55. if($count === false){
  56. throw $pdo->getError();
  57. }
  58. return $count;
  59. }
  60. final public static function migrations(){
  61. $migrations = array_filter(get_declared_classes(), function($class){
  62. return 0 === strpos($class, "Migration\\");
  63. });
  64. sort($migrations);
  65. return $migrations;
  66. }
  67. final public static function migrate(string $direction, int $amount = 1){
  68. if($amount < 1){
  69. throw new \Exception("Migration amount must be a positive integer");
  70. }
  71. $pdo = self::$pdo;
  72. $table = self::$pdo->table(self::version_table());
  73. $table
  74. ->column('version', 'varchar(100)')
  75. ->column('date', 'datetime', DefaultValue::named('current_timestamp'))
  76. ->primaryKey('version')
  77. ->index('date_idx', ['date'])
  78. ->commit();
  79. switch($direction){
  80. case self::MIGRATE_UP:
  81. foreach(self::migrations() as $migration){
  82. if(!$migration::installed()){
  83. $pdo->transaction(function($pdo) use($migration, $table){
  84. $migration::up($pdo);
  85. $migration::change($pdo, self::MIGRATE_UP);
  86. });
  87. $table->insert(['version'=>$migration::version()]);
  88. if(!$migration::installed()){
  89. throw new \Exception("Migration {$migration::version()} {$direction} failed");
  90. }
  91. if(--$amount == 0){
  92. break;
  93. }
  94. }
  95. }
  96. break;
  97. case self::MIGRATE_DOWN:
  98. foreach(array_reverse(self::migrations()) as $migration){
  99. if($migration::installed()){
  100. $pdo->transaction(function($pdo) use($migration, $table){
  101. $migration::change($pdo, self::MIGRATE_DOWN);
  102. $migration::down($pdo);
  103. });
  104. $table->delete(['version'=>$migration::version()]);
  105. if($migration::installed()){
  106. throw new \Exception("Migration {$migration::version()} {$direction} failed");
  107. }
  108. if(--$amount == 0){
  109. break;
  110. }
  111. }
  112. }
  113. break;
  114. default:
  115. throw new \Exception("Invalid migration direction '{$direction}'");
  116. }
  117. self::get_current_version(true);
  118. self::get_last_install_date(true);
  119. }
  120. final public static function migrate_all(string $direction){
  121. self::migrate($direction, count(self::migrations()));
  122. }
  123. final public static function bind(string $dsn){
  124. self::$pdo = PDO::from($dsn);
  125. }
  126. final public static function release(){
  127. self::$pdo = null;
  128. }
  129. }
  130. ?>