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

SQL Migration with variable #63

Closed
trejjam opened this issue Mar 3, 2017 · 6 comments
Closed

SQL Migration with variable #63

trejjam opened this issue Mar 3, 2017 · 6 comments

Comments

@trejjam
Copy link
Contributor

trejjam commented Mar 3, 2017

Hi, is it possible execute SQL migration with variable?

For example:

SELECT @adminRoleId := id
FROM `user_role`
WHERE `name` = 'administrator';

INSERT INTO `user_role` (`parent_id`, `name`)
VALUES (@adminRoleId, 'faq');

It will fail, because N/Migrations exec single queries from file.

Is there some workaround or solution?

@hrach
Copy link
Member

hrach commented Mar 3, 2017

Well, I'm not sure how this works with http://php.net/mysqli_query. If you can put both queries at once as one call, then possible workaround is to use php handler and manually call this query. But, I'm afraid this is not possible from php side. Or, am I wrong? Yes, I was wrong :(

@trejjam
Copy link
Contributor Author

trejjam commented Mar 3, 2017

I was use Kdyby/Doctrine connection in migration. If i switch to Nette/BD it works well.

Funny is they both share PDOConnection from Doctrine.

If I call manually:

/** @var Kdyby\Doctrine\Connection $connection */
$connection = $em->getConnection();

$connection->exec('SELECT @adminRoleId := id
FROM `user_role`
WHERE `name` = \'administrator\';

INSERT INTO `user_role` (`parent_id`, `name`)
VALUES (@adminRoleId, \'faq\');');

It is OK


$connection = $em->getConnection();

$connection->exec('SELECT @adminRoleId := id
FROM `user_role`
WHERE `name` = \'administrator\';');
$connection->exec('INSERT INTO `user_role` (`parent_id`, `name`)
VALUES (@adminRoleId, \'faq\');');

This will fail

Is it possible introduce some mechanism in Nextras\Migrations\Drivers\BaseDriver::loadFile to not explode input from file into multiple queries?

@JanTvrdik
Copy link
Member

Partially related is #52

@JanTvrdik
Copy link
Member

But IMHO solution to your problem is to set session-level variable.

@JanTvrdik
Copy link
Member

@trejjam
Copy link
Contributor Author

trejjam commented Mar 5, 2017

Thanks!
I was thinking that SET @foo = and SELECT @foo := are equal.

Rewrite migration into

SET @adminRoleId = (SELECT id
FROM `user_role`
WHERE `name` = 'administrator');

INSERT INTO `user_role` (`parent_id`, `name`)
VALUES (@adminRoleId, 'faq');

Work as expected => done without fail
(under nette and doctrine DBAL with MySQL)

@trejjam trejjam closed this as completed Mar 5, 2017
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