Guest User

Untitled

a guest
May 28th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. <?php
  2.  
  3. require dirname(dirname(__FILE__)) . '/config/bootstrap.php';
  4. require 'config/settings.php';
  5. require 'config/connections.php';
  6.  
  7. function get_migrations() {
  8. $migrations = Filesystem::getFiles('db/migrations');
  9. natsort($migrations);
  10. return $migrations;
  11. }
  12.  
  13. function get_migration_version($migration) {
  14. return substr($migration, 0, 14);
  15. }
  16.  
  17. function create_schema_migrations($connection) {
  18. $sql = <<<EOT
  19. CREATE TABLE `schema_migrations` (
  20. `version` varchar(255),
  21. PRIMARY KEY (`version`)
  22. )
  23. ENGINE = InnoDB
  24. CHARACTER SET utf8 COLLATE utf8_general_ci;
  25. EOT;
  26. $connection->query($sql);
  27. }
  28.  
  29. function should_migrate($migration, $connection) {
  30. $should_migrate = $connection->count(array(
  31. 'table' => 'schema_migrations',
  32. 'conditions' => array(
  33. 'version' => get_migration_version($migration)
  34. )
  35. ));
  36. return !$should_migrate;
  37. }
  38.  
  39. function migrate($migration, $connection) {
  40. echo 'importing ' . $migration . '... ';
  41. $connection->query(utf8_decode(Filesystem::read('db/migrations/' . $migration)));
  42. $connection->create(array(
  43. 'table' => 'schema_migrations',
  44. 'values' => array(
  45. 'version' => get_migration_version($migration)
  46. )
  47. ));
  48. echo 'done' . PHP_EOL;
  49. }
  50.  
  51. $connection = Connection::get(Config::read('App.environment'));
  52. if(!in_array('schema_migrations', $connection->listSources())) {
  53. create_schema_migrations($connection);
  54. }
  55.  
  56. $migrations = get_migrations();
  57. foreach($migrations as $migration) {
  58. if(should_migrate($migration, $connection)) {
  59. migrate($migration, $connection);
  60. }
  61. }
Add Comment
Please, Sign In to add comment