| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- <?php
- namespace cx_newsletter;
- use \exception;
- class database_installer
- extends database_builder
- implements database_builder_interface {
- public function clean() : self {
- $this->drop_table($this->get_tables()->send_log());
- $this->drop_table($this->get_tables()->campaigns());
- $this->drop_table($this->get_tables()->customers());
- $this->drop_table($this->get_tables()->messages());
- $this->drop_table($this->get_tables()->groups());
- $this->get_versions()->set_installed(null);
- return $this;
- }
- public function update() : self {
- while (true) {
- if ($this->increase_version() === false) {
- return $this;
- }
- }
- }
- private function increase_version() : bool {
- $version = $this->get_versions()->get_installed();
- if ($version === null) {
- $this->init();
- return true;
- }
- if ($version === 1) {
- $this->from_first_to_second();
- return true;
- }
- if ($version === 2) {
- return false;
- }
- }
- private function from_first_to_second() : void {
- $this->create_table($this->get_tables()->groups(), [
- 'id' => 'int auto_increment not null primary key',
- 'name' => 'char(128)'
- ]);
- $this->add_column(
- $this->get_tables()->customers(),
- 'grouped',
- 'int null default null'
- );
- $this->add_foreign_key(
- $this->get_tables()->customers(),
- 'grouped',
- $this->get_tables()->groups(),
- 'id'
- );
- $this->add_column(
- $this->get_tables()->campaigns(),
- 'grouped',
- 'int null default null'
- );
- $this->add_foreign_key(
- $this->get_tables()->campaigns(),
- 'grouped',
- $this->get_tables()->groups(),
- 'id'
- );
- $this->get_versions()->set_installed(2);
- }
- public function init() : self {
- if ($this->get_versions()->get_installed() !== null) {
- $error = 'Can not initialize currently initialized database. ';
- $error .= 'Clean it first.';
- throw new exception($error);
- }
- $this->create_table($this->get_tables()->customers(), [
- 'id' => 'int auto_increment not null primary key',
- 'company' => 'char(128)',
- 'name' => 'char(64)',
- 'surname' => 'char(64)',
- 'comments' => 'char(255)',
- 'email' => 'char(64)',
- 'phone_number' => 'char(12)',
- 'tester' => 'bool',
- ]);
- $this->create_table($this->get_tables()->messages(), [
- 'id' => 'int auto_increment not null primary key',
- 'title' => 'char(128)',
- 'content' => 'text'
- ]);
- $this->create_table($this->get_tables()->campaigns(), [
- 'id' => 'int auto_increment not null primary key',
- 'message' => 'int not null',
- 'type' => 'enum("SMS", "EMAIL")',
- 'test' => 'bool',
- 'finalized' => 'datetime default null',
- 'foreign key (message)' =>
- 'references '.$this->get_tables()->messages().'(id)'
- ]);
- $this->create_table($this->get_tables()->send_log(), [
- 'id' => 'int auto_increment not null primary key',
- 'campaign' => 'int not null',
- 'customer' => 'int not null',
- 'sended' => 'datetime not null',
- 'foreign key (campaign)' =>
- 'references '.$this->get_tables()->campaigns().'(id)',
- 'foreign key (customer)' =>
- 'references '.$this->get_tables()->customers().'(id)'
- ]);
- $this->get_versions()->set_installed(1);
-
- return $this;
- }
- }
|