{"id":179789,"date":"2013-05-16T14:56:02","date_gmt":"2013-05-16T10:56:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=179789"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=179789","title":{"rendered":"<span class=\"post_title\">\u0411\u043e\u0440\u0435\u043c\u0441\u044f \u0441 \u0434\u0443\u0431\u043b\u0438\u043a\u0430\u0442\u0430\u043c\u0438<\/span>"},"content":{"rendered":"<div class=\"content html_format\">   \t\u041f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u044f <a href=\"http:\/\/habrahabr.ru\/post\/177957\/\">\u0442\u0435\u043c\u0443<\/a> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e SQL, \u044f \u0445\u043e\u0447\u0443 \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u0442\u044c \u043e\u0431 \u043e\u0434\u043d\u043e\u043c \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u043c \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0435, \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u043e\u043c \u043c\u043d\u043e\u0439 \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u043e\u0434\u043d\u043e\u0433\u043e \u0438\u0437 \u0442\u0435\u043a\u0443\u0449\u0438\u0445 \u043f\u0440\u043e\u0435\u043a\u0442\u043e\u0432. \u0420\u0435\u0447\u044c \u043f\u043e\u0439\u0434\u0435\u0442 \u043e \u0434\u0443\u0431\u043b\u0438\u043a\u0430\u0442\u0430\u0445 \u0432 \u0441\u043f\u0440\u0430\u0432\u043e\u0447\u043d\u0438\u043a\u0430\u0445. \u041f\u043e\u0434 \u0434\u0443\u0431\u043b\u0438\u043a\u0430\u0442\u0430\u043c\u0438, \u0432 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435, \u044f \u043f\u043e\u043d\u0438\u043c\u0430\u044e \u0437\u0430\u043f\u0438\u0441\u0438, \u0432\u043d\u0435\u0441\u0435\u043d\u043d\u044b\u0435 \u0432 \u0441\u043f\u0440\u0430\u0432\u043e\u0447\u043d\u0438\u043a\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043e\u0440\u0444\u043e\u0433\u0440\u0430\u0444\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u043e\u0448\u0438\u0431\u043a\u0438 \u043f\u0440\u0438 \u0432\u0432\u043e\u0434\u0435 \u043d\u0430\u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u0438\u044f. <br \/>  <a name=\"habracut\"><\/a><br \/>  \u0421\u0443\u0442\u044c \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u0435\u043c\u043e\u0433\u043e \u043c\u043d\u043e\u0439 \u043f\u043e\u0434\u0445\u043e\u0434\u0430 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u0434\u0430\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u044a\u044f\u0432\u0438\u0442\u044c \u043b\u044e\u0431\u0443\u044e \u0437\u0430\u043f\u0438\u0441\u044c \u0441\u043f\u0440\u0430\u0432\u043e\u0447\u043d\u0438\u043a\u0430 \u0434\u0443\u0431\u043b\u0438\u043a\u0430\u0442\u043e\u043c \u0443\u0436\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0439. \u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435, \u0434\u0443\u0431\u043b\u0438\u0440\u0443\u044e\u0449\u0430\u044f \u0437\u0430\u043f\u0438\u0441\u044c \u0431\u0443\u0434\u0435\u0442 \u0443\u0434\u0430\u043b\u0435\u043d\u0430, \u0430 \u0432\u0441\u0435 \u0441\u0441\u044b\u043b\u043a\u0438 \u043d\u0430 \u043d\u0435\u0435 \u0431\u0443\u0434\u0443\u0442 \u0438\u0441\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u044b \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u043e\u043d\u0438 \u0441\u0442\u0430\u043b\u0438 \u0441\u0441\u044b\u043b\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u0443\u044e \u0437\u0430\u043f\u0438\u0441\u044c. \u0422\u0430\u043a\u0436\u0435, \u043e\u0447\u0435\u043d\u044c \u0432\u0430\u0436\u043d\u043e, \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043e\u0442\u043a\u0430\u0442\u0430 \u0442\u0430\u043a\u0438\u0445 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439, \u043d\u0430 \u0441\u043b\u0443\u0447\u0430\u0439, \u0435\u0441\u043b\u0438 \u043e\u043d\u0438 \u0441\u0434\u0435\u043b\u0430\u043d\u044b \u043f\u043e \u043e\u0448\u0438\u0431\u043a\u0435.<\/p>\n<p>  \u041d\u0430\u0447\u043d\u0435\u043c \u0441 \u0442\u0430\u0431\u043b\u0438\u0446 \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0441\u043b\u0443\u0436\u0435\u0431\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445:<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0421\u043b\u0443\u0436\u0435\u0431\u043d\u044b\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"sql\">create table    mg_table (   table_name    varchar(100)   not null,   pk_name       varchar(100)   not null,   primary key(name) );  create sequence mg_action_seq;  create table    mg_action (   id            bigint         default nextval('mg_action_seq') not null,   table_name    varchar(100)   not null references mg_table(name),   old_id        varchar(50)    not null,   new_id        varchar(50)    not null,   action_time   timestamp      default now() not null,   primary key(id) );  create sequence mg_action_detail_seq;  create table    mg_action_detail (   id            bigint         default nextval('mg_action_detail_seq') not null,   action_id     bigint         not null references mg_action(id),   table_name    varchar(100)   not null,   pk_name       varchar(100)   not null,   column_name   varchar(100)   not null,   obj_id        varchar(50)    not null,   primary key(id) ); <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u0417\u0434\u0435\u0441\u044c, \u0442\u0430\u0431\u043b\u0438\u0446\u0430 mg_table \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u043e \u0442\u0430\u0431\u043b\u0438\u0446\u0430\u0445, \u0434\u043b\u044f \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0441\u043b\u0438\u044f\u043d\u0438\u0435 \u0434\u0443\u0431\u043b\u0438\u043a\u0430\u0442\u043e\u0432. \u0422\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u0435 \u043a \u0442\u0430\u043a\u0438\u043c \u0442\u0430\u0431\u043b\u0438\u0446\u0430\u043c \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0435 \u2014 \u043f\u0435\u0440\u0432\u0438\u0447\u043d\u044b\u0439 \u043a\u043b\u044e\u0447 \u0434\u043e\u043b\u0436\u0435\u043d \u0441\u043e\u0441\u0442\u043e\u044f\u0442\u044c \u0438\u0437 \u043e\u0434\u043d\u043e\u0433\u043e \u0447\u0438\u0441\u043b\u043e\u0432\u043e\u0433\u043e \u0438\u043b\u0438 \u0441\u0442\u0440\u043e\u043a\u043e\u0432\u043e\u0433\u043e \u0441\u0442\u043e\u043b\u0431\u0446\u0430. \u041d\u0430\u043c \u043d\u0435 \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u0431\u0435\u0441\u043f\u043e\u043a\u043e\u0438\u0442\u044c\u0441\u044f \u043e\u0431 \u044d\u0442\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u0435, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043e\u043d\u0430 \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u043f\u043e\u043b\u043d\u044f\u0442\u044c\u0441\u044f \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438. \u0422\u0430\u0431\u043b\u0438\u0446\u044b mg_action \u0438 mg_action_detail \u0431\u0443\u0434\u0443\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u0434\u043b\u044f \u043e\u0442\u043a\u0430\u0442\u0430 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439.<\/p>\n<p>  \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c \u043f\u0430\u0440\u0443 \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439:<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0412\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"sql\">create or replace function mg_get_pk_column(in p_table varchar) returns varchar as $$ declare   l_pk  text;   l_cn  int; begin   select max(f.name), count(*) as name into l_pk, l_cn   from ( select ps_array_to_set(a.conkey) as nn          from   pg_constraint a, pg_class b          where  b.oid = a.conrelid          and    a.contype = 'p'          and    b.relname = lower(p_table) ) c,         ( select d.attname as name, d.attnum as nn          from   pg_attribute d, pg_class e          where  e.oid = d.attrelid          and    e.relname = lower(p_table) ) f   where  f.nn = c.nn;    if l_cn &lt;&gt; 1 then      raise EXCEPTION 'Can''t support composite PK';   end if;    return l_pk; end; $$ language plpgsql;  create or replace function mg_add_dict(in p_table varchar) returns void as $$ declare   l_pk  text;   l_sql text; begin   l_pk := mg_get_pk_column(p_table);    perform 1   from mg_table where table_name = lower(p_table);   if not FOUND then       l_sql :=      'create table mg_' || lower(p_table) || ' ' ||     'as select * from ' || lower(p_table) || ' limit 0';      execute l_sql;       l_sql :=     'alter table mg_' || lower(p_table) || ' ' ||     'add primary key(' || l_pk || ')';      execute l_sql;       insert into mg_table(table_name, pk_name) values (lower(p_table), l_pk);   end if; end; $$ language plpgsql; <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u0424\u0443\u043d\u043a\u0446\u0438\u044f mg_get_pk_column \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b\u0439 \u043d\u0430\u043c \u043f\u043e \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 <a href=\"http:\/\/habrahabr.ru\/post\/177957\/\">\u0441\u0442\u0430\u0442\u044c\u0435<\/a> \u0437\u0430\u043f\u0440\u043e\u0441, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0449\u0438\u0439 \u0438\u043c\u044f \u0441\u0442\u043e\u043b\u0431\u0446\u0430 \u043f\u0435\u0440\u0432\u0438\u0447\u043d\u043e\u0433\u043e \u043a\u043b\u044e\u0447\u0430, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043e\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043b\u044f\u0435\u0442 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u043d\u0430 \u0442\u043e, \u0447\u0442\u043e \u043f\u0435\u0440\u0432\u0438\u0447\u043d\u044b\u0439 \u043a\u043b\u044e\u0447 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 \u043e\u0434\u043d\u043e\u0433\u043e \u0441\u0442\u043e\u043b\u0431\u0446\u0430.<\/p>\n<p>  \u0424\u0443\u043d\u043a\u0446\u0438\u044f mg_add_dict, \u043f\u043e\u043c\u0438\u043c\u043e \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f mg_table, \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0441 \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u043e\u043c &#8216;mg_&#8217;, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0431\u0443\u0434\u0443\u0442 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u044c\u0441\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u043d\u044b\u0435 \u0434\u0443\u0431\u043b\u0438\u043a\u0430\u0442\u044b, \u043d\u0430 \u0442\u043e\u0442 \u0441\u043b\u0443\u0447\u0430\u0439, \u0435\u0441\u043b\u0438 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u044c\u0441\u044f \u043e\u0442\u043a\u0430\u0442\u0438\u0442\u044c. \u041f\u043e \u0441\u0432\u043e\u0435\u0439 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0435, \u044d\u0442\u0430 \u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u0430 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0439.<\/p>\n<p>  \u041f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u043a \u0441\u0430\u043c\u043e\u043c\u0443 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u043c\u0443:<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">mg_merge<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"sql\">create or replace function mg_merge(in p_table varchar, in p_old varchar, in p_new varchar) returns void as $$ declare   l_action int;   l_pk     text;   l_sql    text;   tabs     record; begin   perform mg_add_dict(p_table);    select pk_name into l_pk   from   mg_table where table_name = lower(p_table);    l_action := nextval('mg_action_seq');   insert into mg_action(id, table_name, old_id, new_id)   values (l_action, p_table, p_old, p_new);    l_sql :=   'insert into mg_' || lower(p_table) || ' ' ||  'select * from ' || lower(p_table) || ' ' ||  'where ' || l_pk || ' = ''' || p_old || '''';   execute l_sql;    for tabs in       select b.relname as table_name,               d.attname as column_name       from   pg_constraint a, pg_class b, pg_class c,              pg_attribute d       where  a.contype = 'f'       and    b.oid = a.conrelid       and    c.oid = a.confrelid       and    c.relname = lower(p_table)       and    d.attrelid = b.oid       and    a.conkey[1] = d.attnum       loop      l_sql :=      'insert into mg_action_detail(action_id, table_name, column_name, obj_id, pk_name) ' ||     'select ' || l_action || ', ''' || tabs.table_name || ''', ''' ||       tabs.column_name || ''', id, ' ||     '''' || mg_get_pk_column(tabs.table_name::varchar) || ''' ' ||     'from ' || lower(tabs.table_name) || ' ' ||     'where ' || lower(tabs.column_name) || ' = ''' || p_old || '''';      execute l_sql;          l_sql :=        'update ' || lower(tabs.table_name) || ' ' ||        'set ' || lower(tabs.column_name) || ' = ''' || p_new || ''' ' ||        'where ' || lower(tabs.column_name) || ' = ''' || p_old || '''';         execute l_sql;       end loop;    l_sql :=  'delete from ' || lower(p_table) || ' where ' || l_pk || ' = ''' || p_old || '''';   execute l_sql; end; $$ language plpgsql;  create or replace function mg_merge(in p_table varchar, in p_old bigint, in p_new bigint)  returns void as $$ declare begin   perform mg_merge(p_table, p_old::varchar, p_new::varchar); end; $$ language plpgsql; <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u042d\u0442\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u043f\u043e\u0438\u0441\u043a \u0432\u0441\u0435\u0445 \u0442\u0430\u0431\u043b\u0438\u0446, \u0441\u0441\u044b\u043b\u0430\u044e\u0449\u0438\u0445\u0441\u044f \u043d\u0430 p_table \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 \u0432\u043d\u0435\u0448\u043d\u0435\u0433\u043e \u043a\u043b\u044e\u0447\u0430 \u0438 \u0437\u0430\u043c\u0435\u043d\u044f\u0435\u0442 \u0432 \u043d\u0438\u0445 p_old \u043d\u0430 p_new, \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u044f \u0434\u0430\u043d\u043d\u044b\u0435, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u0434\u043b\u044f \u043e\u0442\u043a\u0430\u0442\u0430 \u0438\u0437\u043c\u0435\u043d\u0438\u0439. \u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443, \u0447\u0430\u0449\u0435 \u0432\u0441\u0435\u0433\u043e, \u0441\u0442\u043e\u043b\u0431\u0435\u0446 \u043f\u0435\u0440\u0432\u0438\u0447\u043d\u043e\u0433\u043e \u043a\u043b\u044e\u0447\u0430 \u0431\u0443\u0434\u0435\u0442 \u0447\u0438\u0441\u043b\u043e\u0432\u044b\u043c, \u0434\u043b\u044f \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u0430, \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0436\u0435\u043d\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f mg_merge(varchar, bigint, bigint).<\/p>\n<p>  \u041e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043e\u0442\u043a\u0430\u0442\u0430 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439:<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">mg_undo<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"sql\">create or replace function mg_undo() returns void as $$ declare   l_action int;   l_old    varchar(50);   l_table  text;   l_sql    text;   tabs     record; begin   select max(id) into l_action   from   mg_action;    if l_action is null then      raise EXCEPTION 'Can''t UNDO';   end if;    select table_name, old_id into l_table, l_old   from   mg_action   where  id = l_action;    l_sql :=   'insert into ' || l_table || ' ' ||  'select * from mg_' || l_table || ' ' ||  'where id = ''' || l_old || '''';   execute l_sql;    for tabs in       select table_name,              pk_name,              column_name       from   mg_action_detail       where  action_id = l_action       group  by table_name, pk_name, column_name       loop          l_sql :=          'update ' || tabs.table_name || ' ' ||         'set ' || tabs.column_name || ' = ''' || l_old || ''' ' ||         'where ''*'' || ' || tabs.pk_name || ' in (' ||         'select ''*'' || obj_id from mg_action_detail '||         'where table_name = ''' || tabs.table_name || ''' ' ||         'and action_id = ' || l_action || ') ';          execute l_sql;       end loop;    l_sql :=   'delete from mg_' || l_table || ' where id = ''' || l_old || '''';   execute l_sql;    delete from mg_action_detail where action_id = l_action;   delete from mg_action where id = l_action; end; $$ language plpgsql; <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u0418\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0431\u0443\u0434\u0443\u0442 \u043e\u0442\u043a\u0430\u0442\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u0432 \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u0441\u0442\u0440\u043e\u0433\u043e \u043f\u0440\u043e\u0442\u0438\u0432\u043e\u043f\u043e\u043b\u043b\u043e\u0436\u043d\u043e\u043c \u0438\u0445 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044e. \u041f\u043e \u044d\u0442\u043e\u0439 \u043f\u0440\u0438\u0447\u0438\u043d\u0435, \u043d\u0438\u043a\u0430\u043a\u0438\u0435 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u044b \u0434\u043b\u044f mg_undo \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f.<\/p>\n<p>  \u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043a\u0430\u043a \u0432\u0441\u0435 \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442. \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0441\u043f\u0440\u0430\u0432\u043e\u0447\u043d\u044b\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b:<\/p>\n<pre><code class=\"sql\">create sequence city_seq;  create table    city (   id            bigint         default nextval('city_seq') not null,   name          varchar(100)   not null,   primary key(id) );  create sequence street_seq;  create table    street (   id            bigint         default nextval('street_seq') not null,   city_id       bigint         not null references city(id),   name          varchar(100)   not null,   primary key(id) );  create sequence address_seq;  create table    address (   id            bigint         default nextval('address_seq') not null,   street_id     bigint         not null references street(id),   house         varchar(10)    not null,   apartment     varchar(10)    not null,   primary key(id) ); <\/code><\/pre>\n<p>  \u2026 \u0438 \u043d\u0430\u043f\u043e\u043b\u043d\u0438\u043c \u0438\u0445 \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u043c\u0438:<\/p>\n<pre><code class=\"sql\">insert into city(id, name) values (1, '\u041a\u0430\u0437\u0430\u043d\u044c');  insert into street(id, city_id, name) values (1, 1, '\u041f\u043e\u0431\u0435\u0434\u044b'); insert into street(id, city_id, name) values (2, 1, '\u041f\u043e\u0431\u0435\u0434\u044b \u043f\u0440\u043e\u0441\u043f\u0435\u043a\u0442');  insert into address(id, street_id, house, apartment) values (1, 1, '10', '1'); insert into address(id, street_id, house, apartment) values (2, 2, '10', '2'); <\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c, \u0434\u043b\u044f \u0442\u043e\u0433\u043e \u0447\u0442\u043e\u0431\u044b \u00ab\u0441\u043b\u0438\u0442\u044c\u00bb \u0443\u043b\u0438\u0446\u0443 &#8216;\u041f\u043e\u0431\u0435\u0434\u044b \u043f\u0440\u043e\u0441\u043f\u0435\u043a\u0442&#8217; \u0441 \u0443\u043b\u0438\u0446\u0435\u0439 &#8216;\u041f\u043e\u0431\u0435\u0434\u044b&#8217;, \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0443\u044e \u043a\u043e\u043c\u0430\u043d\u0434\u0443:<\/p>\n<pre><code class=\"sql\">select mg_merge('street', 2, 1); <\/code><\/pre>\n<p>  \u0424\u0443\u043d\u043a\u0446\u0438\u044f mg_undo(), \u043a\u0430\u043a \u0438 \u0433\u043e\u0432\u043e\u0440\u0438\u043b\u043e\u0441\u044c \u0432\u044b\u0448\u0435, \u043e\u0442\u043a\u0430\u0442\u0438\u0442 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f.      \t \t\t   \t<\/p>\n<div class=\"clear\"><\/div>\n<\/p><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/179789\/\"> http:\/\/habrahabr.ru\/post\/179789\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"content html_format\">   \t\u041f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u044f <a href=\"http:\/\/habrahabr.ru\/post\/177957\/\">\u0442\u0435\u043c\u0443<\/a> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e SQL, \u044f \u0445\u043e\u0447\u0443 \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u0442\u044c \u043e\u0431 \u043e\u0434\u043d\u043e\u043c \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u043c \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0435, \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u043e\u043c \u043c\u043d\u043e\u0439 \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u043e\u0434\u043d\u043e\u0433\u043e \u0438\u0437 \u0442\u0435\u043a\u0443\u0449\u0438\u0445 \u043f\u0440\u043e\u0435\u043a\u0442\u043e\u0432. \u0420\u0435\u0447\u044c \u043f\u043e\u0439\u0434\u0435\u0442 \u043e \u0434\u0443\u0431\u043b\u0438\u043a\u0430\u0442\u0430\u0445 \u0432 \u0441\u043f\u0440\u0430\u0432\u043e\u0447\u043d\u0438\u043a\u0430\u0445. \u041f\u043e\u0434 \u0434\u0443\u0431\u043b\u0438\u043a\u0430\u0442\u0430\u043c\u0438, \u0432 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435, \u044f \u043f\u043e\u043d\u0438\u043c\u0430\u044e \u0437\u0430\u043f\u0438\u0441\u0438, \u0432\u043d\u0435\u0441\u0435\u043d\u043d\u044b\u0435 \u0432 \u0441\u043f\u0440\u0430\u0432\u043e\u0447\u043d\u0438\u043a\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043e\u0440\u0444\u043e\u0433\u0440\u0430\u0444\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u043e\u0448\u0438\u0431\u043a\u0438 \u043f\u0440\u0438 \u0432\u0432\u043e\u0434\u0435 \u043d\u0430\u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u0438\u044f.   <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-179789","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/179789","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=179789"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/179789\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=179789"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=179789"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=179789"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}