03-database_installer.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace cx_newsletter;
  3. use \exception;
  4. class database_installer
  5. extends database_builder
  6. implements database_builder_interface {
  7. public function clean() : self {
  8. $this->drop_table($this->get_tables()->send_log());
  9. $this->drop_table($this->get_tables()->campaigns());
  10. $this->drop_table($this->get_tables()->customers());
  11. $this->drop_table($this->get_tables()->messages());
  12. $this->drop_table($this->get_tables()->groups());
  13. $this->get_versions()->set_installed(null);
  14. return $this;
  15. }
  16. public function update() : self {
  17. while (true) {
  18. if ($this->increase_version() === false) {
  19. return $this;
  20. }
  21. }
  22. }
  23. private function increase_version() : bool {
  24. $version = $this->get_versions()->get_installed();
  25. if ($version === null) {
  26. $this->init();
  27. return true;
  28. }
  29. if ($version === 1) {
  30. $this->from_first_to_second();
  31. return true;
  32. }
  33. if ($version === 2) {
  34. return false;
  35. }
  36. }
  37. private function from_first_to_second() : void {
  38. $this->create_table($this->get_tables()->groups(), [
  39. 'id' => 'int auto_increment not null primary key',
  40. 'name' => 'char(128)'
  41. ]);
  42. $this->add_column(
  43. $this->get_tables()->customers(),
  44. 'grouped',
  45. 'int null default null'
  46. );
  47. $this->add_foreign_key(
  48. $this->get_tables()->customers(),
  49. 'grouped',
  50. $this->get_tables()->groups(),
  51. 'id'
  52. );
  53. $this->add_column(
  54. $this->get_tables()->campaigns(),
  55. 'grouped',
  56. 'int null default null'
  57. );
  58. $this->add_foreign_key(
  59. $this->get_tables()->campaigns(),
  60. 'grouped',
  61. $this->get_tables()->groups(),
  62. 'id'
  63. );
  64. $this->get_versions()->set_installed(2);
  65. }
  66. public function init() : self {
  67. if ($this->get_versions()->get_installed() !== null) {
  68. $error = 'Can not initialize currently initialized database. ';
  69. $error .= 'Clean it first.';
  70. throw new exception($error);
  71. }
  72. $this->create_table($this->get_tables()->customers(), [
  73. 'id' => 'int auto_increment not null primary key',
  74. 'company' => 'char(128)',
  75. 'name' => 'char(64)',
  76. 'surname' => 'char(64)',
  77. 'comments' => 'char(255)',
  78. 'email' => 'char(64)',
  79. 'phone_number' => 'char(12)',
  80. 'tester' => 'bool',
  81. ]);
  82. $this->create_table($this->get_tables()->messages(), [
  83. 'id' => 'int auto_increment not null primary key',
  84. 'title' => 'char(128)',
  85. 'content' => 'text'
  86. ]);
  87. $this->create_table($this->get_tables()->campaigns(), [
  88. 'id' => 'int auto_increment not null primary key',
  89. 'message' => 'int not null',
  90. 'type' => 'enum("SMS", "EMAIL")',
  91. 'test' => 'bool',
  92. 'finalized' => 'datetime default null',
  93. 'foreign key (message)' =>
  94. 'references '.$this->get_tables()->messages().'(id)'
  95. ]);
  96. $this->create_table($this->get_tables()->send_log(), [
  97. 'id' => 'int auto_increment not null primary key',
  98. 'campaign' => 'int not null',
  99. 'customer' => 'int not null',
  100. 'sended' => 'datetime not null',
  101. 'foreign key (campaign)' =>
  102. 'references '.$this->get_tables()->campaigns().'(id)',
  103. 'foreign key (customer)' =>
  104. 'references '.$this->get_tables()->customers().'(id)'
  105. ]);
  106. $this->get_versions()->set_installed(1);
  107. return $this;
  108. }
  109. }