database-migrations.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php if ( ! defined( 'ABSPATH' ) ) exit;
  2. final class NF_Upgrade_Database_Migrations extends NF_Upgrade
  3. {
  4. public $name = 'database_migrations';
  5. public $priority = "0.0.1";
  6. public $description = 'The database needs to be updated to support the new version.';
  7. public $args = array();
  8. public $errors = array();
  9. public function loading()
  10. {
  11. $already_run = $this->isComplete();
  12. $this->total_steps = ( $already_run ) ? 0 : 1;
  13. }
  14. public function step( $step )
  15. {
  16. $this->createObjectTable();
  17. $this->createObjectMetaTable();
  18. $this->createObjectRelationshipsTable();
  19. }
  20. public function complete()
  21. {
  22. update_option( 'nf_database_migrations', true);
  23. }
  24. public function isComplete()
  25. {
  26. if( $this->existsObjectTable() ||
  27. $this->existsObjectMetaTable() ||
  28. $this->existsObjectRelationshipsTable()
  29. ) {
  30. return true;
  31. }
  32. return get_option( 'nf_database_migrations', false );
  33. }
  34. /*
  35. * PRIVATE METHODS
  36. */
  37. private function createObjectTable()
  38. {
  39. require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
  40. $sql = "CREATE TABLE IF NOT EXISTS " . NF_OBJECTS_TABLE_NAME . " (
  41. `id` bigint(20) NOT NULL AUTO_INCREMENT,
  42. `type` varchar(255) NOT NULL,
  43. PRIMARY KEY (`id`)
  44. ) DEFAULT CHARSET=utf8;";
  45. dbDelta( $sql );
  46. }
  47. private function existsObjectTable()
  48. {
  49. global $wpdb;
  50. return $wpdb->query( "SHOW TABLES LIKE '" . NF_OBJECTS_TABLE_NAME . "'" );
  51. }
  52. private function createObjectMetaTable()
  53. {
  54. require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
  55. $sql = "CREATE TABLE IF NOT EXISTS ". NF_OBJECT_META_TABLE_NAME . " (
  56. `id` bigint(20) NOT NULL AUTO_INCREMENT,
  57. `object_id` bigint(20) NOT NULL,
  58. `meta_key` varchar(255) NOT NULL,
  59. `meta_value` longtext NOT NULL,
  60. PRIMARY KEY (`id`)
  61. ) DEFAULT CHARSET=utf8;";
  62. dbDelta( $sql );
  63. }
  64. private function existsObjectMetaTable()
  65. {
  66. global $wpdb;
  67. return $wpdb->query( "SHOW TABLES LIKE '" . NF_OBJECT_META_TABLE_NAME . "'" );
  68. }
  69. private function createObjectRelationshipsTable()
  70. {
  71. require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
  72. $sql = "CREATE TABLE IF NOT EXISTS " . NF_OBJECT_RELATIONSHIPS_TABLE_NAME . " (
  73. `id` bigint(20) NOT NULL AUTO_INCREMENT,
  74. `child_id` bigint(20) NOT NULL,
  75. `parent_id` bigint(20) NOT NULL,
  76. `child_type` varchar(255) NOT NULL,
  77. `parent_type` varchar(255) NOT NULL,
  78. PRIMARY KEY (`id`)
  79. ) DEFAULT CHARSET=utf8;";
  80. dbDelta( $sql );
  81. }
  82. private function existsObjectRelationshipsTable()
  83. {
  84. global $wpdb;
  85. return $wpdb->query( "SHOW TABLES LIKE '" . NF_OBJECT_RELATIONSHIPS_TABLE_NAME . "'" );
  86. }
  87. }