summaryrefslogtreecommitdiff
path: root/migrate.php
blob: 01723309c009fd6ebdf9bd7ec3d6b564d359b3c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php

function abort() {
  print("exiting\n");
  exit;
}

// get my current version.
if(file_exists("./db.ini")) {
  $config = parse_ini_file("./db.ini");
} else {
  die("No config file found\n");
}

if(!is_array($config)) {
  die("Can't parse your db.ini!\n");
}

$con = mysqli_connect($config['server'], $config['user'], $config['password']);

// check if DB exists. Otherwise create.
$versions = [];
$query = "show databases like '" . $config['database'] . "'";
if($res = $con->query($query)) {
  if($res->num_rows == 0) {
    print("no database found\n");
    if(readline("No database exists. Do want us to create it? (Y) ") != "Y") {
      abort();
    }
  }
}

// connect to DB.
$con->select_db($config['database']);

// group all ver
$line = fgets(STDIN);

if(!preg_match("/^# Versioned Migration Script$/", $line)) {
  die("Sorry, this doesn't look like a versioned file.\n");
}


// find current version.


// find max version

$current_version = 1;

$v = "0";
while($line = fgets(STDIN)) {
    $line = trim($line);
    if(preg_match("/^# Version ([0-9])+/", $line, $matches)) {
      $v  = $matches[1];
      if(array_key_exists($v, $versions)) {
        die("Version $v already exists, you cannot have two of the same version\n");
      }
      $versions[$v] = [];
    } else {
      $versions[$v][] = $line;
    }
  }
print_r($versions);
$max = max(array_keys($versions));
print("Current version: $current_version\n");
print_r("Max version : $max\n");
if($current_version == $max) {
  die("You're up to date on version $current_version");
}

if($current_version > $max) {
  die("You have a new version than provided by the sql file");
}

foreach($versions as $version => $rows) {
  if($version > $current_version) {
    print("I should run $version\n");
  }
}