Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require_once('phing/Task.php');
- class MigrationTask extends Task {
- private $host;
- private $port;
- private $name;
- private $user;
- private $pass;
- private $dir;
- public function setUser($user){
- $this->user = $user;
- }
- public function setPass($pass){
- $this->pass = $pass;
- }
- public function setHost($host){
- $this->host = $host;
- }
- public function setName($name){
- $this->name = $name;
- }
- public function setDir($dir){
- $this->dir = $dir;
- }
- public function main(){
- $dh = opendir($this->dir);
- if(!$dh){
- die("Unable to open {$this->dir} for reading.");
- }
- $files = array();
- while(($file = readdir($dh)) !== false){
- if($file[0] == '.'){
- continue;
- }
- $info = pathinfo($file);
- if($info['extension'] != 'php' ){
- continue;
- }
- $files[] = $file;
- }
- sort($files);
- $this->connect();
- $this->begin();
- foreach($files as $file){
- $this->migrate($file);
- }
- $this->commit();
- }
- private function connect(){
- $dsn = "";
- if(!empty($this->host)){
- $dsn .= "host={$this->host} ";
- }
- if(!empty($this->port)){
- $dsn .= "port={$this->port} ";
- }
- if(!empty($this->name)){
- $dsn .= "dbname={$this->name} ";
- }
- if(!empty($this->user)){
- $dsn .= "user={$this->user} ";
- }
- if(!empty($this->pass)){
- $dsn .= "password={$this->pass} ";
- }
- $dsn = trim($dsn);
- pg_connect($dsn)
- or die("Unable to connect to the database!");
- $result = $this->query("SELECT * FROM information_schema.tables WHERE table_schema='public' AND table_name='schema_migrations'");
- if(empty($result)){
- $this->begin();
- $this->execute("CREATE TABLE schema_migrations(version INTEGER NOT NULL)");
- $this->commit();
- }
- }
- private function begin(){
- pg_query("BEGIN");
- }
- private function commit(){
- pg_query("COMMIT");
- }
- private function execute($sql, $params = null){
- if(empty($params)){
- pg_query($sql);
- } else {
- if(!is_array($params)){
- $params = array($params);
- }
- pg_query_params($sql, $params);
- }
- if(pg_last_error()){
- die(pg_last_error());
- }
- }
- private function query($sql, $params = null){
- if(empty($params)){
- $result = pg_query($sql);
- } else {
- if(!is_array($params)){
- $params = array($params);
- }
- $result = pg_query_params($sql, $params);
- }
- $result = pg_fetch_all($result);
- if(pg_last_error()){
- die(pg_last_error());
- }
- return $result;
- }
- private function migrate($file){
- $version = $this->version($file);
- $result = $this->query('SELECT * FROM schema_migrations WHERE version=$1', $version);
- if(empty($result)){
- echo "[MIGRATING]: {$file}\n";
- $this->run("{$this->dir}/{$file}");
- $this->execute("INSERT INTO schema_migrations VALUES($1)", $version);
- }
- }
- private function version($file){
- $subject = $file;
- $pattern = '/^[0-9]{3}/';
- $matches = array();
- preg_match($pattern, $subject, $matches);
- if(empty($matches[0])){
- die("Unable to exctract version from file: {$file}!");
- }
- $version = (int) $matches[0];
- if($version <= 0){
- die("Invalid version {$version}, extracted from file {$file}!");
- }
- return $version;
- }
- private function run($file){
- require_once($file);
- }
- }
- // SAMPLE: 001_create_something.php
- $this->execute("CREATE TABLE foo(bar INTEGER)");
- $this->execute("CREATE TABLE bar(foo INTEGER)");
Add Comment
Please, Sign In to add comment