Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Index from existing Database #2

Open
tom-lp-at opened this issue Nov 1, 2019 · 0 comments
Open

Create Index from existing Database #2

tom-lp-at opened this issue Nov 1, 2019 · 0 comments

Comments

@tom-lp-at
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant