bizley / yii2-migration

Yii 2 Migration Creator And Updater
Apache License 2.0
291 stars 36 forks source link

[Bug] Write here short summary of a problem #189

Open lav45 opened 16 hours ago

lav45 commented 16 hours ago

Describe the bug

No support:

  1. hash index
  2. gin index with options
  3. default boolean type
  4. default function
  5. where condition for index

DB Schema:

create table beneficiary
(
    id                            serial primary key,
    name                     varchar(255),
    is_deleted             boolean     default false,
    type                        integer,
    company_uuid      varchar,
    uuid                        varchar     default gen_random_uuid()  not null,
);

create index beneficiary_type_idx
    on beneficiary using hash (type);

create index beneficiary_name_gin_idx
    on beneficiary using gin (name gin_trgm_ops);

create unique index beneficiary_uuid_idx
    on beneficiary (uuid);

create index beneficiary_company_uuid_idx
    on beneficiary (company_uuid)
    where (company_uuid IS NOT NULL);
~$ yii migration/create beneficiary
Yii 2 Migration Generator Tool v4.4.1
 > 1 table excluded by the config.

 > Generating migration for creating table 'beneficiary' ...DONE!
 > Saved as '/var/www/console/migrations/schema/m241125_214006_create_table_beneficiary.php'

 Generated 1 file
 (!) Remember to verify files before applying migration.

Generated file

    public function safeUp()
    {
        $tableOptions = null;
        if ($this->db->driverName === 'mysql') {
            $tableOptions = 'CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE=InnoDB';
        }

        $this->createTable(
            'beneficiary',
            [
                'id' => $this->primaryKey(),
                'name' => $this->string(),
                'is_deleted' => $this->boolean()->defaultValue(''),
                'type' => $this->integer(),
                'company_uuid' => $this->string(),
                'uuid' => $this->string()->notNull(),
            ],
            $tableOptions
        );

        $this->createIndex('beneficiary_type_idx', 'beneficiary', ['type']);
        $this->createIndex('beneficiary_name_gin_idx', 'beneficiary', ['name']);
        $this->createIndex('beneficiary_uuid_idx', 'beneficiary', ['uuid'], true);
        $this->createIndex('beneficiary_company_uuid_idx', 'beneficiary', ['company_uuid']);
    }

Expected behavior

    use yii\db\pgsql\QueryBuilder;

    public function safeUp()
    {
        $this->createTable('beneficiary', [
            'id' => $this->primaryKey(),
            'name' => $this->string(),
            'is_deleted' => $this->boolean()->defaultValue(false),
            'type' => $this->integer(),
            'company_uuid' => $this->string(),
            'uuid' => 'varchar DEFAULT gen_random_uuid() NOT NULL',
        ]);

        $this->createIndex('beneficiary_type_idx', 'beneficiary', 'type', QueryBuilder::INDEX_HASH);
        $this->createIndex('beneficiary_uuid_idx', 'beneficiary', 'uuid', QueryBuilder::INDEX_UNIQUE);
        $this->execute('CREATE INDEX [[beneficiary_name_gin_idx]] ON [[beneficiary]] USING gin ({{name}} gin_trgm_ops);');
        $this->execute("CREATE INDEX [[beneficiary_company_uuid_idx]] ON [[beneficiary]] USING btree ({{company_uuid}}) WHERE {{company_uuid}} IS NOT NULL;");
    }

Additional context DB: Pstgresql 15.3

Affected versions v4.4.1

bizley commented 7 hours ago

I haven't checked if Yii can detect any of these although I'm surprised by the lack of bool default. Is there any chance you could prepare PR fixing at least some parts of it?

lav45 commented 5 hours ago

I just wanted to share my find.