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

field ordering of compound index #221

Open
mcfog opened this issue Jan 28, 2017 · 2 comments
Open

field ordering of compound index #221

mcfog opened this issue Jan 28, 2017 · 2 comments

Comments

@mcfog
Copy link

mcfog commented Jan 28, 2017

The order of field in compound index is crucial ( a,b index and b,a index is totally different thing) but now we just determine it by order of fields() declaration. In #203 we gain ability to declare multiple compound about same field, but we can't declare field order in index

e.g. we have a table (a,b,c,d), we cannot declare index b_c(b,c) and index a_c_b(a,c,b) at same time

IMO there's two way to solve this, first is change the index format in fields() declaration to something like

'index' => [true, 'b_c' => 1, 'a_c_b' => 3],

decide index group by key, order by value

or we can just separate a new indexes() method to declare these kind of indexes

    public static function indexes()
    {
        return [
            'b_c' => ['unique' => false, 'fields' => ['b', 'c']],
            'a_c_b' => ['unique' => false, 'fields' => ['a', 'c', 'b']],
        ];
    }

the second way make compound index looks more clearly but might involve more code, and interaction with the old 'index'=>'name' way

@nwoodthorpe-test1
Copy link

nwoodthorpe-test1 commented Jan 28, 2017

Removed

@mcfog
Copy link
Author

mcfog commented Jan 28, 2017

FYI here's my monkey patch works on both 2.2 tag and master branch

extend Mapper

class DMapper extends Mapper
{
    public function resolver()
    {
        return new DResolver($this);
    }
}

extend Resolver to call Entity's alterTableSchema

class DResolver extends Resolver
{
    public function migrateCreateSchema()
    {
        $schema = parent::migrateCreateSchema();
        foreach ($schema->getTables() as $table) {
            $entityName = $this->mapper->entity();
            $entityName::alterTableSchema($table);
        }

        return $schema;
    }
}

extend Entity base class with new mapper and default empty alterTableSchema

class DEntity extends Entity
{
    protected static $mapper = DMapper::class;

    public static function alterTableSchema(Table $table)
    {

    }
}

finally, manually add index in Entity class

    public static function alterTableSchema(Table $table)
    {
        parent::alterTableSchema($table);

        $table->addIndex(['a', 'c', 'b'], 'a_c_b');
    }

@nwoodthorpe-test1
Copy link

No time for monkeying around.

Dont monkey patch bro 🙈

@splio-aleroux
Copy link
Contributor

The second options seems to be the best one.
I would like to see such changes too.

I'll try to open a PR for one of the two solutions if i have some time for that soon.

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

3 participants