没有不值得去解决的问题,也没有不值得去学习的技术!

Adjusting RBAC String Field Lengths to 200 in Yii2

The lengths of the string-type fields in all 4 tables starting with auth_ have been successfully adjusted to 200, as expected

1. After running the migration command: yii migrate –migrationPath=@yii/rbac/migrations, the length of the string-type fields is 64.

SQL


-- apply_server_v6.auth_item definition

CREATE TABLE `auth_item` (
  `name` varchar(64) COLLATE utf8mb3_unicode_ci NOT NULL,
  `type` smallint NOT NULL,
  `description` text COLLATE utf8mb3_unicode_ci,
  `rule_name` varchar(64) COLLATE utf8mb3_unicode_ci DEFAULT NULL,
  `data` blob,
  `created_at` int DEFAULT NULL,
  `updated_at` int DEFAULT NULL,
  PRIMARY KEY (`name`),
  KEY `rule_name` (`rule_name`),
  KEY `idx-auth_item-type` (`type`),
  CONSTRAINT `auth_item_ibfk_1` FOREIGN KEY (`rule_name`) REFERENCES `auth_rule` (`name`) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci;

-- apply_server_v6.auth_assignment definition

CREATE TABLE `auth_assignment` (
  `item_name` varchar(64) COLLATE utf8mb3_unicode_ci NOT NULL,
  `user_id` varchar(64) COLLATE utf8mb3_unicode_ci NOT NULL,
  `created_at` int DEFAULT NULL,
  PRIMARY KEY (`item_name`,`user_id`),
  KEY `idx-auth_assignment-user_id` (`user_id`),
  CONSTRAINT `auth_assignment_ibfk_1` FOREIGN KEY (`item_name`) REFERENCES `auth_item` (`name`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci;

-- apply_server_v6.auth_item_child definition

CREATE TABLE `auth_item_child` (
  `parent` varchar(64) COLLATE utf8mb3_unicode_ci NOT NULL,
  `child` varchar(64) COLLATE utf8mb3_unicode_ci NOT NULL,
  PRIMARY KEY (`parent`,`child`),
  KEY `child` (`child`),
  CONSTRAINT `auth_item_child_ibfk_1` FOREIGN KEY (`parent`) REFERENCES `auth_item` (`name`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `auth_item_child_ibfk_2` FOREIGN KEY (`child`) REFERENCES `auth_item` (`name`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci;

-- apply_server_v6.auth_rule definition

CREATE TABLE `auth_rule` (
  `name` varchar(64) COLLATE utf8mb3_unicode_ci NOT NULL,
  `data` blob,
  `created_at` int DEFAULT NULL,
  `updated_at` int DEFAULT NULL,
  PRIMARY KEY (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci;

2. I originally planned to create a new migration to modify the field lengths. However, when running the migration command, it threw an error: SQLSTATE[HY000]: General error: 1833 Cannot change column ‘name’: used in a foreign key constraint ‘auth_item_child_ibfk_1’ of table. See Figure 1.

I originally planned to create a new migration to modify the field lengths. However, when running the migration command, it threw an error: SQLSTATE[HY000]: General error: 1833 Cannot change column 'name': used in a foreign key constraint 'auth_item_child_ibfk_1' of table
Figure 1
Plaintext

PS C:\wwwroot\object\src> ./yii migrate
Yii Migration Tool (based on Yii v2.0.49.3)

Total 1 new migration to be applied:
        m250409_162156_alter_columns_in_auth

Apply the above migration? (yes|no) [no]:yes
*** applying m250409_162156_alter_columns_in_auth
    > alter column name in table auth_item to string(200) NOT NULL ...Exception: SQLSTATE[HY000]: General error: 1833 Cannot change column 'name': used in a foreign key constraint 'auth_item_child_ibfk_1' of table 'apply_server_v6.auth_item_child'
The SQL being executed was: ALTER TABLE `auth_item` CHANGE `name` `name` varchar(200) NOT NULL (C:\wwwroot\object\src\vendor\yiisoft\yii2\db\Schema.php:676)
#0 C:\wwwroot\object\src\vendor\yiisoft\yii2\db\Command.php(1325): yii\db\Schema->convertException(Object(PDOException), 'ALTER TABLE `au...')
#1 C:\wwwroot\object\src\vendor\yiisoft\yii2\db\Command.php(1120): yii\db\Command->internalExecute('ALTER TABLE `au...')
#2 C:\wwwroot\object\src\vendor\yiisoft\yii2\db\Migration.php(431): yii\db\Command->execute()
#3 C:\wwwroot\object\src\console\migrations\m250409_162156_alter_columns_in_auth.php(15): yii\db\Migration->alterColumn('auth_item', 'name', Object(yii\db\mysql\ColumnSchemaBuilder))
#4 C:\wwwroot\object\src\vendor\yiisoft\yii2\db\Migration.php(114): m250409_162156_alter_columns_in_auth->safeUp()
#5 C:\wwwroot\object\src\vendor\yiisoft\yii2\console\controllers\BaseMigrateController.php(758): yii\db\Migration->up()
#6 C:\wwwroot\object\src\vendor\yiisoft\yii2\console\controllers\BaseMigrateController.php(216): yii\console\controllers\BaseMigrateController->migrateUp('m250409_162156_...')
#7 [internal function]: yii\console\controllers\BaseMigrateController->actionUp(0)
#8 C:\wwwroot\object\src\vendor\yiisoft\yii2\base\InlineAction.php(57): call_user_func_array(Array, Array)
#9 C:\wwwroot\object\src\vendor\yiisoft\yii2\base\Controller.php(178): yii\base\InlineAction->runWithParams(Array)
#10 C:\wwwroot\object\src\vendor\yiisoft\yii2\console\Controller.php(180): yii\base\Controller->runAction('', Array)
#11 C:\wwwroot\object\src\vendor\yiisoft\yii2\base\Module.php(552): yii\console\Controller->runAction('', Array)
#12 C:\wwwroot\object\src\vendor\yiisoft\yii2\console\Application.php(180): yii\base\Module->runAction('migrate', Array)
#13 C:\wwwroot\object\src\vendor\yiisoft\yii2\console\Application.php(147): yii\console\Application->runAction('migrate', Array)
#14 C:\wwwroot\object\src\vendor\yiisoft\yii2\base\Application.php(384): yii\console\Application->handleRequest(Object(yii\console\Request))
#15 C:\wwwroot\object\src\yii(23): yii\base\Application->run()
#16 {main}
*** failed to apply m250409_162156_alter_columns_in_auth (time: 0.036s)

0 from 1 migrations were applied.

Migration failed. The rest of the migrations are canceled.

3. I ultimately decided to copy the 4 migration files involved in the migration command: yii migrate –migrationPath=@yii/rbac/migrations to console/migrations, and then directly modify m140506_102106_rbac_init to replace 64 with 200. Next, I deleted the tables starting with auth_ and removed the 4 migration records from the migration table. Finally, I re-ran the migration command: ./yii migrate

Plaintext

PS C:\wwwroot\object\src> ./yii migrate
Yii Migration Tool (based on Yii v2.0.49.3)

Total 4 new migrations to be applied:
        m140506_102106_rbac_init
        m170907_052038_rbac_add_index_on_auth_assignment_user_id
        m180523_151638_rbac_updates_indexes_without_prefix
        m200409_110543_rbac_update_mssql_trigger

Apply the above migrations? (yes|no) [no]:yes
*** applying m140506_102106_rbac_init
    > create table {{%auth_rule}} ... done (time: 0.023s)
    > create table {{%auth_item}} ... done (time: 0.026s)
    > create index idx-auth_item-type on {{%auth_item}} (type) ... done (time: 0.018s)
    > create table {{%auth_item_child}} ... done (time: 0.027s)
    > create table {{%auth_assignment}} ... done (time: 0.021s)
*** applied m140506_102106_rbac_init (time: 0.127s)

*** applying m170907_052038_rbac_add_index_on_auth_assignment_user_id
    > create index auth_assignment_user_id_idx on {{%auth_assignment}} (user_id) ... done (time: 0.015s)
*** applied m170907_052038_rbac_add_index_on_auth_assignment_user_id (time: 0.021s)

*** applying m180523_151638_rbac_updates_indexes_without_prefix
    > drop index auth_assignment_user_id_idx on {{%auth_assignment}} ... done (time: 0.009s)
    > create index {{%idx-auth_assignment-user_id}} on {{%auth_assignment}} (user_id) ... done (time: 0.010s)
    > drop index idx-auth_item-type on {{%auth_item}} ... done (time: 0.017s)
    > create index {{%idx-auth_item-type}} on {{%auth_item}} (type) ... done (time: 0.016s)
*** applied m180523_151638_rbac_updates_indexes_without_prefix (time: 0.057s)

*** applying m200409_110543_rbac_update_mssql_trigger
*** applied m200409_110543_rbac_update_mssql_trigger (time: 0.007s)

4 migrations were applied.

Migrated up successfully.

4. The lengths of the string-type fields in all 4 tables starting with auth_ have been successfully adjusted to 200, as expected. See Figure 2.

The lengths of the string-type fields in all 4 tables starting with auth_ have been successfully adjusted to 200, as expected
Figure 2

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系