1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

utf8mb4環境のMySQLを利用するFuelPHPでSimpleauthドライバのAuthパッケージを使う

Last updated at Posted at 2015-11-03

タイトルのような環境においてSimpleauthドライバのマイグレーションを行うと例外を吐いて失敗します。

$ oil refine migrate --packages=auth
Uncaught exception Fuel\Core\Database_Exception: SQLSTATE[HY000]: General error: 1709 Index column size too large. The maximum column size is 767 bytes. with query: "CREATE UNIQUE INDEX username ON authusers (username, email)"

対応方法として、Simpleauthドライバのテーブルを作成するデータベースをutf8で作成するなど方法はあるかと思いますが、へたれな自分は001_auth_create_usertables.phpで作成するテーブルをAuthパッケージのマイグレーションではなく、作成しているWebアプリのマイグレーションとして実行させる方法を取りました。
つまり、fuel/app/migrations/ディレクトリにXXX_auth_create_usertables.phpというファイルを作成して(XXXの部分は適当な数値)、内容を以下のようにします。

XXX_auth_create_usertables.php
<?php

namespace Fuel\Migrations;

class Auth_Create_Usertables
{
	function up()
	{
		// get the driver used
		\Config::load('auth', true);

		// get the tablename
		\Config::load('simpleauth', true);
		$table = \Config::get('simpleauth.table_name', 'users');

		// make sure the configured DB is used
		$connection = \Config::get('simpleauth.db_connection', null);
		\DBUtil::set_connection($connection);

		// table users
		\DBUtil::create_table($table, array(
			'id' => array('type' => 'int', 'constraint' => 11, 'auto_increment' => true),
			'username' => array('type' => 'varchar', 'constraint' => 50),
			'password' => array('type' => 'varchar', 'constraint' => 255),
			'`group`' => array('type' => 'int', 'constraint' => 11, 'default' => 1),
			'email' => array('type' => 'varchar', 'constraint' => 255),
			'last_login' => array('type' => 'varchar', 'constraint' => 25),
			'login_hash' => array('type' => 'varchar', 'constraint' => 255),
			'profile_fields' => array('type' => 'text'),
			'created_at' => array('type' => 'int', 'constraint' => 11, 'default' => 0),
			'updated_at' => array('type' => 'int', 'constraint' => 11, 'default' => 0),
		), array('id'), true, 'InnoDB row_format=DYNAMIC', 'utf8mb4', [], $connection);

		// add a unique index on username and email
		\DBUtil::create_index($table, array('username', 'email'), 'username', 'UNIQUE');

		// reset any DBUtil connection set
		\DBUtil::set_connection(null);
	}

	function down()
	{
		// get the driver used
		\Config::load('auth', true);

		// get the tablename
		\Config::load('simpleauth', true);

		// make sure the configured DB is used
		\DBUtil::set_connection(\Config::get('simpleauth.db_connection', null));

		// drop the admin_users table
		\DBUtil::drop_table(\Config::get('simpleauth.table_name', 'users'));

		// reset any DBUtil connection set
		\DBUtil::set_connection(null);
	}
}

Webアプリのマイグレーションを行ったあとにSimpleauthドライバのマイグレーションを行うと無事成功します。

$ oil refine migrate --packages=auth
Performed migrations for package:auth:
001_auth_create_usertables
002_auth_create_grouptables
003_auth_create_roletables
004_auth_create_permissiontables
005_auth_create_authdefaults
006_auth_add_authactions
007_auth_add_permissionsfilter
008_auth_create_providers
009_auth_create_oauth2tables
010_auth_fix_jointables

良かった良かった。
001_auth_create_usertablesが重複するのですが、ファイルを見れば分かる通り、テーブルの存在確認を行っているので問題無いです。というかココで存在確認を行っているのを利用してWebアプリのマイグレーションで先にテーブルを作ってしまおうという企みです。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?