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; } }