Skip to content

Create Index from existing Database #2

Description

@tom-lp-at

Dear all,
i used your perfect script to generate my migration file for CI3.
Except the generation of double(9,2), in contrast of double("9,2") it works in a basic way. I had only two of them, so its not a pain :)
I recognized that only the PRIMARY index is generated, so i decide to hack into it and generate all other indices ....
I marked the line

//                 if ($column['Key'] == 'PRI')
//                     $key = "\t\t" . '$this->dbforge->add_key("' . $column['Field'] . '",true);';

and paste before the $up .= .... create_table generation:

$q = $this->ci->db_master->query('show index from ' . $this->ci->db_master->protect_identifiers($this->ci->db_master->database . '.' . $table));
// No result means the table name was invalid
if ($q === FALSE)
{
	continue;
}

$indizes = $q->result_array();
			
$index_result=array();
foreach($indizes as $index) {
	if (!isset($index_result[$index['Key_name']])) {
		$index_result[$index['Key_name']] = [];
	}
	$index_result[$index['Key_name']][] = $index['Column_name'];
}
			
$key = "";
			
foreach($index_result as $key_index => $index) {
	$field_name="";
	foreach($index as $field) {
		$field_name .= "'".$field."',";
	}
	$field_name = substr($field_name,0,strlen($field_name)-1);
	if (count($index) > 1) {
		$field_name = "array(".$field_name.")";
	}
				
	$key .= "\t\t" . '$this->dbforge->add_key('. $field_name ;
	if ($key_index == "PRIMARY")  {
		$key .= ",true);".PHP_EOL;
	} else {
		$key .= ");".PHP_EOL;
	}
			
}

the results looks like

$this->dbforge->add_key(array('uid','ausgetreten'),true);
                $this->dbforge->add_key('nname');
                $this->dbforge->add_key('austritt');
                $this->dbforge->add_key('standort');
                $this->dbforge->add_key(array('name','ausgetreten'));
                $this->dbforge->add_key(array('user_anzeigen','vorstand_id','eintritt','austritt','beendigung'));
                $this->dbforge->add_key(array('user_anzeigen','eintritt','austritt'));
                $this->dbforge->add_key('name');
                $this->dbforge->add_key('standort_id');
                $this->dbforge->add_key('hpt_firma_id');

                $this->dbforge->create_table("users", TRUE);

kr Tom

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions