Ver código fonte

Add ability to determine the latest version, and the last install date

Nathaniel van Diepen 7 anos atrás
pai
commit
c0acac06f5
1 arquivos alterados com 32 adições e 2 exclusões
  1. 32 2
      PDO/migration.abstract.class.php

+ 32 - 2
PDO/migration.abstract.class.php

@@ -11,6 +11,8 @@
 		const MIGRATE_UP = 'up';
 		const MIGRATE_DOWN = 'down';
 		private static $pdo;
+		private static $date;
+		private static $version;
 
 		final public static function version(){
 			$name = get_called_class();
@@ -22,6 +24,30 @@
 			}
 			return Settings::get('db.versions');
 		}
+		final public static function get_current_version(bool $force = false){
+			if(is_null(self::$version) || $force){
+				if(is_null(self::$pdo)){
+					throw new \Exception("Migration is not bound to a data source");
+				}
+				$table_name = self::version_table();
+				$query = self::$pdo->query("select max(version) version from `{$table_name}`");
+				self::$version = $query->fetch()['version'];
+				$query->closeCursor();
+			}
+			return self::$version;
+		}
+		final public static function get_last_install_date(bool $force = false){
+			if(is_null(self::$date) || $force){
+				if(is_null(self::$pdo)){
+					throw new \Exception("Migration is not bound to a data source");
+				}
+				$table_name = self::version_table();
+				$query = self::$pdo->query("select max(date) date from `{$table_name}`");
+				self::$date = $query->fetch()['date'];
+				$query->closeCursor();
+			}
+			return self::$date;
+		}
 		final public static function installed(){
 			$pdo = self::$pdo;
 			$count = $pdo->exec(
@@ -46,10 +72,12 @@
 				throw new \Exception("Migration amount must be a positive integer");
 			}
 			$pdo = self::$pdo;
-			$table = $pdo->table(self::version_table());
+			$table = self::$pdo->table(self::version_table());
 			$table
 				->column('version', 'varchar(100)')
+				->column('date', 'datetime', DefaultValue::named('current_timestamp'))
 				->primaryKey('version')
+				->index('date_idx', ['date'])
 				->commit();
 			switch($direction){
 				case self::MIGRATE_UP:
@@ -89,6 +117,8 @@
 				default:
 					throw new \Exception("Invalid migration direction '{$direction}'");
 			}
+			self::get_current_version(true);
+			self::get_last_install_date(true);
 		}
 		final public static function migrate_all(string $direction){
 			self::migrate($direction, count(self::migrations()));
@@ -100,4 +130,4 @@
 			self::$pdo = null;
 		}
 	}
-?>
+?>