CodeIgniter Forums
drop_column() support for sqlite3_forge driver - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Development (https://forum.codeigniter.com/forumdisplay.php?fid=6)
+--- Forum: CodeIgniter 3.x (https://forum.codeigniter.com/forumdisplay.php?fid=17)
+--- Thread: drop_column() support for sqlite3_forge driver (/showthread.php?tid=75330)



drop_column() support for sqlite3_forge driver - nplaviola - 01-27-2020

Any chance of getting the drop_column() option added to the sqlite3_forge driver? Can someone help me piece it together if not? is it as simple as uncommenting the commented section below? (it's clearly not, haha). Thank you!

Existing method:

PHP Code:
/**
 * ALTER TABLE
 *
 * @todo implement drop_column(), modify_column()
 * @param string $alter_type ALTER type
 * @param string $table Table name
 * @param mixed $field Column definition
 * @return string|string[]
 */
 
protected function _alter_table($alter_type$table$field)
 {
 if (
$alter_type === 'DROP' OR $alter_type === 'CHANGE')
 {
 
// drop_column():
 // BEGIN TRANSACTION;
 // CREATE TEMPORARY TABLE t1_backup(a,b);
 // INSERT INTO t1_backup SELECT a,b FROM t1;
 // DROP TABLE t1;
 // CREATE TABLE t1(a,b);
 // INSERT INTO t1 SELECT a,b FROM t1_backup;
 // DROP TABLE t1_backup;
 // COMMIT;

 
return FALSE;
 }

 return 
parent::_alter_table($alter_type$table$field);
 } 



RE: drop_column() support for sqlite3_forge driver - nplaviola - 01-28-2020

(01-27-2020, 02:40 PM)nplaviola Wrote: Any chance of getting the drop_column() option added to the sqlite3_forge driver? Can someone help me piece it together if not? is it as simple as uncommenting the commented section below? (it's clearly not, haha). Thank you!

Existing method:

PHP Code:
/**
 * ALTER TABLE
 *
 * @todo implement drop_column(), modify_column()
 * @param string $alter_type ALTER type
 * @param string $table Table name
 * @param mixed $field Column definition
 * @return string|string[]
 */
 
protected function _alter_table($alter_type$table$field)
 {
 if (
$alter_type === 'DROP' OR $alter_type === 'CHANGE')
 {
 
// drop_column():
 // BEGIN TRANSACTION;
 // CREATE TEMPORARY TABLE t1_backup(a,b);
 // INSERT INTO t1_backup SELECT a,b FROM t1;
 // DROP TABLE t1;
 // CREATE TABLE t1(a,b);
 // INSERT INTO t1 SELECT a,b FROM t1_backup;
 // DROP TABLE t1_backup;
 // COMMIT;

 
return FALSE;
 }

 return 
parent::_alter_table($alter_type$table$field);
 } 

I wrote a workaround in my model and thought I'd share the meat of it in case anyone else runs into this. It's not very sophisticated but it gets the job done:

PHP Code:
$this->db->trans_start();
$this->db->query('CREATE TEMPORARY TABLE t1_backup(a,b);');
$this->db->query('INSERT INTO t1_backup SELECT a,b FROM t1;');
$this->db->query('DROP TABLE t1;');
$this->db->query('CREATE TABLE t1(a,b);');
$this->db->query('INSERT INTO t1 SELECT a,b FROM t1_backup;');
$this->db->query('DROP TABLE t1_backup;');
$this->db->trans_complete();

//check for the existence of the field to verify it's gone
if ($this->db->field_exists($old_field$table_name))
{
    
//do something


The query strings are dynamic in my code but I figure that's easy enough to do yourself.