2023-08-01 16:55:42 +00:00
< ? php
defined ( 'BASEPATH' ) OR exit ( 'No direct script access allowed' );
class Migration_create_label_paper_types_table extends CI_Migration {
public function up () {
2023-08-02 11:05:27 +00:00
if ( ! $this -> db -> table_exists ( 'paper_types' )) {
$this -> dbforge -> add_field ( array (
2023-08-03 04:37:25 +00:00
'paper_id' => array (
2023-08-02 11:05:27 +00:00
'type' => 'INT' ,
'constraint' => 5 ,
'unsigned' => TRUE ,
'auto_increment' => TRUE
),
2023-08-01 16:55:42 +00:00
2023-08-02 11:05:27 +00:00
'user_id' => array (
'type' => 'INT' ,
'constraint' => 5 ,
),
2023-08-01 16:55:42 +00:00
2023-08-02 11:05:27 +00:00
'paper_name' => array (
'type' => 'VARCHAR' ,
'constraint' => '250' ,
),
2023-08-01 16:55:42 +00:00
'metric' => array (
2023-08-02 11:05:27 +00:00
'type' => 'VARCHAR' ,
'constraint' => '10' ,
),
2023-08-01 16:55:42 +00:00
2023-08-02 11:05:27 +00:00
'width' => array (
'type' => 'DECIMAL' ,
'constraint' => '6,3' ,
'null' => TRUE ,
),
2023-08-01 16:55:42 +00:00
2023-08-02 11:05:27 +00:00
'orientation' => array (
'type' => 'VARCHAR' ,
'constraint' => '1' ,
'null' => TRUE ,
),
2023-08-02 10:20:38 +00:00
2023-08-02 11:05:27 +00:00
'height' => array (
'type' => 'DECIMAL' ,
'constraint' => '6,3' ,
'null' => TRUE ,
),
2023-08-01 16:55:42 +00:00
2023-08-02 11:05:27 +00:00
'last_modified' => array (
'type' => 'timestamp' ,
'null' => TRUE ,
),
));
2023-08-01 16:55:42 +00:00
2023-08-03 04:37:25 +00:00
$this -> dbforge -> add_key ( 'paper_id' , TRUE );
2023-08-02 11:05:27 +00:00
$this -> dbforge -> add_key ( 'user_id' , TRUE );
2023-08-01 16:55:42 +00:00
2023-08-02 11:05:27 +00:00
$this -> dbforge -> create_table ( 'paper_types' );
2023-08-03 04:37:25 +00:00
$this -> db -> query ( " ALTER TABLE label_types ADD COLUMN paper_type_id INT(5) NOT NULL; " );
2023-08-02 11:18:58 +00:00
$this -> db -> query ( " CREATE UNIQUE INDEX idx_paper_types_user_id_paper_name ON paper_types (user_id, paper_name) ALGORITHM DEFAULT LOCK DEFAULT; " );
2023-08-03 04:37:25 +00:00
$this -> db -> query ( " insert into paper_types (paper_id,user_id,paper_name,metric,width,orientation,height) values ('1','-1','A4','mm','210.000','P','297.000'); " );
$this -> db -> query ( " insert into paper_types (paper_id,user_id,paper_name,metric,width,orientation,height) values ('2','-1','A5','mm','148.000','P','210.000'); " );
$this -> db -> query ( " insert into paper_types (paper_id,user_id,paper_name,metric,width,orientation,height) values ('3','-1','letter','mm','215.900','P','279.400'); " );
2023-08-02 17:14:18 +00:00
$this -> db -> query ( " insert ignore paper_types (user_id,paper_name,metric,width,orientation,height) SELECT u.user_id, pt.paper_name, pt.metric, pt.width, pt.orientation,pt.height FROM paper_types pt inner join users u where pt.user_id = -1; " );
2023-08-03 04:37:25 +00:00
$this -> db -> query ( " update label_types l set l.paper_type_id=(select p.paper_id from paper_types p where upper(p.paper_name)=upper(l.paper_type) and p.user_id=l.user_id limit 1) where l.paper_type_id=0; " );
2023-08-03 06:16:13 +00:00
$this -> db -> query ( " update label_types l set l.paper_type_id = (select p.paper_id from paper_types p where p.user_id = l.user_id limit 1) where l.paper_type_id = 0; " );
2023-08-03 04:42:51 +00:00
$this -> db -> query ( " alter table label_types drop column paper_type; " );
2023-08-02 11:05:27 +00:00
}
2023-08-01 16:55:42 +00:00
}
public function down (){
if ( $this -> db -> table_exists ( 'paper_types' )) {
$this -> dbforge -> drop_table ( 'paper_types' );
}
2023-08-03 06:16:13 +00:00
if ( $this -> db -> field_exists ( 'paper_type_id' , 'label_types' )) {
$this -> dbforge -> drop_column ( 'label_types' , 'paper_type_id' );
}
if ( ! $this -> db -> field_exists ( 'paper_type' , 'label_types' )) {
$fields = array (
'paper_type varchar(250)' ,
);
$this -> dbforge -> add_column ( 'label_types' , $fields );
$this -> db -> query ( " update label_types set paper_type = 'a4'; " );
}
2023-08-01 16:55:42 +00:00
}
}