Skip to content

Commit 423a7cb

Browse files
committed
add dropIndex tests and fix addIndex
1 parent c79fa3f commit 423a7cb

File tree

2 files changed

+104
-59
lines changed

2 files changed

+104
-59
lines changed

src/Schema.php

Lines changed: 71 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -497,36 +497,7 @@ public function nullable()
497497
}
498498

499499

500-
/**
501-
* Generate a CREATE TABLE query for the current table and fields.
502-
*
503-
* @return string
504-
*/
505-
public function create($engine = 'InnoDB', $charset = 'utf8mb4', $collate = 'utf8mb4_unicode_ci')
506-
{
507-
$this->addToFieldsAndResetFieldQuery();
508-
509-
$sql = "CREATE TABLE IF NOT EXISTS `{$this->table}` (";
510-
$sql .= $this->generateFieldQueryString();
511-
$sql .= ") ENGINE={$engine} DEFAULT CHARSET={$charset} COLLATE={$collate};";
512-
513-
// die("$sql\n");
514-
return DB::query($sql);
515-
}
516500

517-
/**
518-
* Generate an ALTER TABLE query for the current table and fields.
519-
*
520-
* @return string
521-
*/
522-
public function change()
523-
{
524-
$this->addToFieldsAndResetFieldQuery();
525-
$sql = "ALTER TABLE `{$this->table}` ";
526-
$sql .= $this->generateFieldQueryString();
527-
528-
return DB::query($sql);
529-
}
530501

531502

532503
private function generateFieldQueryString(): string
@@ -600,6 +571,7 @@ public function dropColumn($name)
600571
*/
601572
public function renameColumn($current_name)
602573
{
574+
// $this->setFieldQuery('Action', "CHANGE IF EXISTS `$current_name`");
603575
$this->change_action = "CHANGE IF EXISTS `$current_name`";
604576
return $this;
605577
}
@@ -612,6 +584,51 @@ public function after($column_name)
612584
}
613585

614586

587+
/**
588+
* Drop an index from the table.
589+
*
590+
* @param string $name
591+
* @return $this
592+
*/
593+
public function dropIndex($name)
594+
{
595+
$this->setFieldQuery('Action', 'DROP INDEX');
596+
$this->setFieldQuery('Field', "`$name`");
597+
// $this->fields[] = " ";
598+
599+
return $this;
600+
}
601+
602+
/**
603+
* Add a foreign key constraint to the table.
604+
*
605+
* @param string $name
606+
* @param string $column
607+
* @param string $table
608+
* @param string $references
609+
* @param string $onDelete
610+
* @param string $onUpdate
611+
* @return $this
612+
*/
613+
public function addForeign($name, $column, $table, $references, $onDelete = 'CASCADE', $onUpdate = 'CASCADE')
614+
{
615+
$this->fields[] = "CONSTRAINT `$name` FOREIGN KEY (`$column`) REFERENCES `$table` (`$references`) ON DELETE $onDelete ON UPDATE $onUpdate";
616+
return $this;
617+
}
618+
619+
/**
620+
* Drop a foreign key constraint from the table.
621+
*
622+
* @param string $name
623+
* @return $this
624+
*/
625+
public function dropForeign($name)
626+
{
627+
$this->fields[] = "DROP FOREIGN KEY `$name`";
628+
return $this;
629+
}
630+
631+
615632
/**
616633
* Set the character set and collation of the last added column in the $fields array to utf8mb4.
617634
*
@@ -660,53 +677,48 @@ public function modifyColumn()
660677
*/
661678
public function addIndex($name, array $columns, $type = 'INDEX')
662679
{
663-
// $this->setFieldQuery('Action', 'ADD');
680+
664681
$this->setFieldQuery('Action', "ADD $type");
665682
$this->setFieldQuery('Field', "`$name`");
666683
$this->setFieldQuery('Extra', "(" . implode(', ', $columns) . ")");
684+
$this->setFieldQuery('Null', '');
667685

668-
// $this->fields[] = "ADD $type `$name` (" . implode(', ', $columns) . ")";
669686
return $this;
670687
}
671688

672-
/**
673-
* Drop an index from the table.
674-
*
675-
* @param string $name
676-
* @return $this
677-
*/
678-
public function dropIndex($name)
679-
{
680-
$this->fields[] = "DROP INDEX `$name`";
681-
return $this;
682-
}
689+
690+
691+
683692

684693
/**
685-
* Add a foreign key constraint to the table.
694+
* Generate a CREATE TABLE query for the current table and fields.
686695
*
687-
* @param string $name
688-
* @param string $column
689-
* @param string $table
690-
* @param string $references
691-
* @param string $onDelete
692-
* @param string $onUpdate
693-
* @return $this
696+
* @return string
694697
*/
695-
public function addForeign($name, $column, $table, $references, $onDelete = 'CASCADE', $onUpdate = 'CASCADE')
698+
public function create($engine = 'InnoDB', $charset = 'utf8mb4', $collate = 'utf8mb4_unicode_ci')
696699
{
697-
$this->fields[] = "CONSTRAINT `$name` FOREIGN KEY (`$column`) REFERENCES `$table` (`$references`) ON DELETE $onDelete ON UPDATE $onUpdate";
698-
return $this;
700+
$this->addToFieldsAndResetFieldQuery();
701+
702+
$sql = "CREATE TABLE IF NOT EXISTS `{$this->table}` (";
703+
$sql .= $this->generateFieldQueryString();
704+
$sql .= ") ENGINE={$engine} DEFAULT CHARSET={$charset} COLLATE={$collate};";
705+
706+
$this->reset();
707+
return DB::query($sql);
699708
}
700709

701710
/**
702-
* Drop a foreign key constraint from the table.
711+
* Generate an ALTER TABLE query for the current table and fields.
703712
*
704-
* @param string $name
705-
* @return $this
713+
* @return string
706714
*/
707-
public function dropForeign($name)
715+
public function change()
708716
{
709-
$this->fields[] = "DROP FOREIGN KEY `$name`";
710-
return $this;
717+
$this->addToFieldsAndResetFieldQuery();
718+
$sql = "ALTER TABLE `{$this->table}` ";
719+
$sql .= $this->generateFieldQueryString();
720+
721+
$this->reset();
722+
return DB::query($sql);
711723
}
712724
}

tests/SchemaTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ public function testRenameColumn(){
125125
public function testAddIndex(){
126126
$table = new Schema('users');
127127
$table->addIndex('phone',['phone'], Schema::INDEX_UNIQUE)->change();
128+
$table->addIndex('email',['email'])->change();
128129

129130
$columns = DB::query('SHOW COLUMNS FROM `users`', [], true);
130131

@@ -140,6 +141,38 @@ public function testAddIndex(){
140141
$this->assertTrue($status);
141142
}
142143

144+
public function testDropIndex(){
145+
$table = new Schema('users');
146+
147+
$columns = DB::query('SHOW COLUMNS FROM `users`', [], true);
148+
149+
$status = false;
150+
151+
foreach($columns as $column){
152+
if($column->Field == 'email' && $column->Key=='MUL'){
153+
$status = true;
154+
break;
155+
}
156+
}
157+
158+
$this->assertTrue($status);
159+
160+
$table->dropIndex('email')->change();
161+
162+
$columns = DB::query('SHOW COLUMNS FROM `users`', [], true);
163+
164+
$status = false;
165+
166+
foreach($columns as $column){
167+
if($column->Field == 'email' && $column->Key=='MUL'){
168+
$status = true;
169+
break;
170+
}
171+
}
172+
173+
$this->assertFalse($status);
174+
}
175+
143176
}
144177

145178

0 commit comments

Comments
 (0)