Guest User

Untitled

a guest
Jul 18th, 2018
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1. <?php
  2. require_once('phing/Task.php');
  3.  
  4. class MigrationTask extends Task {
  5.  
  6. private $host;
  7.  
  8. private $port;
  9.  
  10. private $name;
  11.  
  12. private $user;
  13.  
  14. private $pass;
  15.  
  16. private $dir;
  17.  
  18.  
  19. public function setUser($user){
  20. $this->user = $user;
  21. }
  22.  
  23. public function setPass($pass){
  24. $this->pass = $pass;
  25. }
  26.  
  27. public function setHost($host){
  28. $this->host = $host;
  29. }
  30.  
  31. public function setName($name){
  32. $this->name = $name;
  33. }
  34.  
  35. public function setDir($dir){
  36. $this->dir = $dir;
  37. }
  38.  
  39. public function main(){
  40.  
  41.  
  42. $dh = opendir($this->dir);
  43.  
  44. if(!$dh){
  45. die("Unable to open {$this->dir} for reading.");
  46. }
  47.  
  48. $files = array();
  49.  
  50. while(($file = readdir($dh)) !== false){
  51.  
  52. if($file[0] == '.'){
  53. continue;
  54. }
  55.  
  56. $info = pathinfo($file);
  57.  
  58. if($info['extension'] != 'php' ){
  59. continue;
  60. }
  61.  
  62. $files[] = $file;
  63. }
  64.  
  65. sort($files);
  66.  
  67. $this->connect();
  68. $this->begin();
  69.  
  70. foreach($files as $file){
  71. $this->migrate($file);
  72. }
  73.  
  74. $this->commit();
  75. }
  76.  
  77. private function connect(){
  78.  
  79. $dsn = "";
  80.  
  81. if(!empty($this->host)){
  82. $dsn .= "host={$this->host} ";
  83. }
  84.  
  85. if(!empty($this->port)){
  86. $dsn .= "port={$this->port} ";
  87. }
  88.  
  89. if(!empty($this->name)){
  90. $dsn .= "dbname={$this->name} ";
  91. }
  92.  
  93. if(!empty($this->user)){
  94. $dsn .= "user={$this->user} ";
  95. }
  96.  
  97. if(!empty($this->pass)){
  98. $dsn .= "password={$this->pass} ";
  99. }
  100.  
  101. $dsn = trim($dsn);
  102.  
  103. pg_connect($dsn)
  104. or die("Unable to connect to the database!");
  105.  
  106. $result = $this->query("SELECT * FROM information_schema.tables WHERE table_schema='public' AND table_name='schema_migrations'");
  107.  
  108. if(empty($result)){
  109. $this->begin();
  110. $this->execute("CREATE TABLE schema_migrations(version INTEGER NOT NULL)");
  111. $this->commit();
  112. }
  113.  
  114. }
  115.  
  116. private function begin(){
  117. pg_query("BEGIN");
  118. }
  119.  
  120. private function commit(){
  121. pg_query("COMMIT");
  122. }
  123.  
  124. private function execute($sql, $params = null){
  125. if(empty($params)){
  126. pg_query($sql);
  127.  
  128. } else {
  129.  
  130. if(!is_array($params)){
  131. $params = array($params);
  132. }
  133.  
  134. pg_query_params($sql, $params);
  135. }
  136.  
  137. if(pg_last_error()){
  138. die(pg_last_error());
  139. }
  140. }
  141.  
  142. private function query($sql, $params = null){
  143.  
  144. if(empty($params)){
  145. $result = pg_query($sql);
  146.  
  147. } else {
  148.  
  149. if(!is_array($params)){
  150. $params = array($params);
  151. }
  152.  
  153. $result = pg_query_params($sql, $params);
  154. }
  155.  
  156. $result = pg_fetch_all($result);
  157.  
  158. if(pg_last_error()){
  159. die(pg_last_error());
  160. }
  161.  
  162. return $result;
  163. }
  164.  
  165. private function migrate($file){
  166.  
  167.  
  168. $version = $this->version($file);
  169.  
  170. $result = $this->query('SELECT * FROM schema_migrations WHERE version=$1', $version);
  171.  
  172. if(empty($result)){
  173.  
  174. echo "[MIGRATING]: {$file}\n";
  175.  
  176. $this->run("{$this->dir}/{$file}");
  177. $this->execute("INSERT INTO schema_migrations VALUES($1)", $version);
  178. }
  179. }
  180.  
  181.  
  182. private function version($file){
  183.  
  184. $subject = $file;
  185. $pattern = '/^[0-9]{3}/';
  186. $matches = array();
  187.  
  188. preg_match($pattern, $subject, $matches);
  189.  
  190. if(empty($matches[0])){
  191. die("Unable to exctract version from file: {$file}!");
  192. }
  193.  
  194.  
  195. $version = (int) $matches[0];
  196.  
  197. if($version <= 0){
  198. die("Invalid version {$version}, extracted from file {$file}!");
  199. }
  200.  
  201. return $version;
  202. }
  203.  
  204. private function run($file){
  205. require_once($file);
  206. }
  207. }
  208.  
  209. // SAMPLE: 001_create_something.php
  210. $this->execute("CREATE TABLE foo(bar INTEGER)");
  211. $this->execute("CREATE TABLE bar(foo INTEGER)");
Add Comment
Please, Sign In to add comment