Commit b2fc02348b2e595927383af83cc096387acfc4e2
1 parent
40cac1b6
add migration PrefixDelete function
Showing
1 changed file
with
51 additions
and
0 deletions
Show diff stats
console/migrations/m151013_062829_deletePrefixFunction.php
0 → 100644
1 | +<?php | ||
2 | + | ||
3 | +use yii\db\Schema; | ||
4 | +use yii\db\Migration; | ||
5 | + | ||
6 | +/** | ||
7 | + * Class m151013_062829_deletePrefixFunction | ||
8 | + * добавляем две функции - одна ищет префикс по поставщику и бренду, | ||
9 | + * другая удаляет найденный префикс из переданного артикула | ||
10 | + */ | ||
11 | +class m151013_062829_deletePrefixFunction extends Migration | ||
12 | +{ | ||
13 | + public function safeUp() | ||
14 | + { | ||
15 | + $find_prefix = <<< SQL | ||
16 | + CREATE FUNCTION FindPrefix(p_importer_id int, p_brand VARCHAR(100)) RETURNS VARCHAR(50) | ||
17 | + BEGIN | ||
18 | + DECLARE _prefix varchar(10); | ||
19 | + | ||
20 | + select prefix into _prefix From w_importers_prefix where importer_id = p_importer_id and brand = p_brand COLLATE utf8_general_ci; | ||
21 | + | ||
22 | + RETURN (_prefix); | ||
23 | + END | ||
24 | +SQL; | ||
25 | + | ||
26 | + $delete_prefix = <<< SQL | ||
27 | + CREATE FUNCTION DeletePrefix(p_articul VARCHAR(150), p_importer_id int, p_brand VARCHAR(100)) RETURNS VARCHAR(150) | ||
28 | + BEGIN | ||
29 | + DECLARE _articul varchar(10); | ||
30 | + | ||
31 | + select substring(p_articul, LENGTH( FindPrefix( p_importer_id, p_brand ) ) + 1 ) into _articul; | ||
32 | + RETURN (_articul); | ||
33 | + END | ||
34 | +SQL; | ||
35 | + | ||
36 | + $this->execute($find_prefix); | ||
37 | + $this->execute($delete_prefix); | ||
38 | + | ||
39 | + } | ||
40 | + | ||
41 | + public function safedown() | ||
42 | + { | ||
43 | + $sql = | ||
44 | + 'drop FUNCTION IF EXIST FindPrefix; | ||
45 | + drop FUNCTION IF EXIST DeletePrefix;'; | ||
46 | + | ||
47 | + $this->execute($sql); | ||
48 | + } | ||
49 | + | ||
50 | + | ||
51 | +} |