2018-07-17 02:00:00 +07:00
|
|
|
CREATE OR REPLACE FUNCTION adempiere.altercolumn(tablename name, columnname name, datatype name, nullclause character varying, defaultclause character varying, namespace name)
|
|
|
|
RETURNS void
|
|
|
|
LANGUAGE plpgsql
|
|
|
|
AS $function$
|
2008-11-01 09:07:34 +07:00
|
|
|
declare
|
|
|
|
command text;
|
|
|
|
viewtext text[];
|
|
|
|
viewname name[];
|
|
|
|
dropviews name[];
|
2018-07-17 02:00:00 +07:00
|
|
|
perms text[];
|
|
|
|
privs text;
|
2008-11-01 09:07:34 +07:00
|
|
|
i int;
|
|
|
|
j int;
|
|
|
|
v record;
|
|
|
|
sqltype text;
|
|
|
|
sqltype_short text;
|
|
|
|
typename name;
|
|
|
|
begin
|
|
|
|
if datatype is not null then
|
|
|
|
select pg_type.typname, format_type(pg_type.oid, pg_attribute.atttypmod)
|
2013-02-09 01:55:24 +07:00
|
|
|
into typename, sqltype
|
2018-07-17 02:00:00 +07:00
|
|
|
from pg_class, pg_attribute, pg_type, pg_namespace
|
2013-02-09 01:55:24 +07:00
|
|
|
where relname = lower(tablename)
|
|
|
|
and relkind = 'r'
|
|
|
|
and pg_class.oid = pg_attribute.attrelid
|
|
|
|
and attname = lower(columnname)
|
2018-07-17 02:00:00 +07:00
|
|
|
and atttypid = pg_type.oid
|
|
|
|
and pg_class.relnamespace = pg_namespace.oid
|
|
|
|
and pg_namespace.nspname = lower(namespace);
|
2008-11-01 09:07:34 +07:00
|
|
|
sqltype_short := sqltype;
|
|
|
|
if typename = 'numeric' then
|
|
|
|
sqltype_short := replace(sqltype, ',0', '');
|
|
|
|
elsif strpos(sqltype,'character varying') = 1 then
|
|
|
|
sqltype_short := replace(sqltype, 'character varying', 'varchar');
|
|
|
|
elsif sqltype = 'timestamp without time zone' then
|
|
|
|
sqltype_short := 'timestamp';
|
|
|
|
end if;
|
|
|
|
if lower(datatype) <> sqltype and lower(datatype) <> sqltype_short then
|
|
|
|
i := 0;
|
2013-02-09 01:55:24 +07:00
|
|
|
for v in
|
|
|
|
with recursive depv(relname, viewoid, depth) as (
|
|
|
|
select distinct a.relname, a.oid, 1
|
2018-07-17 02:00:00 +07:00
|
|
|
from pg_class a, pg_depend b, pg_depend c, pg_class d, pg_attribute e, pg_namespace
|
2013-02-09 01:55:24 +07:00
|
|
|
where a.oid = b.refobjid
|
|
|
|
and b.objid = c.objid
|
|
|
|
and b.refobjid <> c.refobjid
|
|
|
|
and b.deptype = 'n'
|
|
|
|
and c.refobjid = d.oid
|
|
|
|
and d.relname = lower(tablename)
|
|
|
|
and d.relkind = 'r'
|
|
|
|
and d.oid = e.attrelid
|
|
|
|
and e.attname = lower(columnname)
|
|
|
|
and c.refobjsubid = e.attnum
|
|
|
|
and a.relkind = 'v'
|
2018-07-17 02:00:00 +07:00
|
|
|
and a.relnamespace = pg_namespace.oid
|
|
|
|
and pg_namespace.nspname = lower(namespace)
|
2013-02-09 01:55:24 +07:00
|
|
|
union all
|
|
|
|
select distinct dependee.relname, dependee.oid, depv.depth+1
|
|
|
|
from pg_depend
|
|
|
|
join pg_rewrite on pg_depend.objid = pg_rewrite.oid
|
|
|
|
join pg_class as dependee on pg_rewrite.ev_class = dependee.oid
|
|
|
|
join pg_class as dependent on pg_depend.refobjid = dependent.oid
|
|
|
|
join pg_attribute ON pg_depend.refobjid = pg_attribute.attrelid and pg_depend.refobjsubid = pg_attribute.attnum and pg_attribute.attnum > 0
|
|
|
|
join depv on dependent.relname = depv.relname
|
2018-07-17 02:00:00 +07:00
|
|
|
join pg_namespace on dependee.relnamespace = pg_namespace.oid
|
|
|
|
where pg_namespace.nspname = lower(namespace)
|
2013-02-09 01:55:24 +07:00
|
|
|
)
|
|
|
|
select relname, viewoid, max(depth) from depv group by relname, viewoid order by 3 desc
|
|
|
|
loop
|
2018-07-17 02:00:00 +07:00
|
|
|
raise notice 'view -> % %', v.relname, v.viewoid;
|
2008-11-01 09:07:34 +07:00
|
|
|
i := i + 1;
|
2013-02-09 01:55:24 +07:00
|
|
|
viewtext[i] := pg_get_viewdef(v.viewoid);
|
2008-11-01 09:07:34 +07:00
|
|
|
viewname[i] := v.relname;
|
|
|
|
end loop;
|
|
|
|
if i > 0 then
|
|
|
|
begin
|
|
|
|
for j in 1 .. i loop
|
2018-07-17 02:00:00 +07:00
|
|
|
SELECT String_agg('grant ' || privilege_type || ' on ' || viewname[j] || ' to ' || grantee, '; ')
|
|
|
|
into privs
|
|
|
|
FROM information_schema.role_table_grants
|
|
|
|
WHERE table_name=viewname[j];
|
|
|
|
perms[j] := privs;
|
2008-11-01 09:07:34 +07:00
|
|
|
command := 'drop view ' || viewname[j];
|
2013-02-09 01:55:24 +07:00
|
|
|
raise notice 'executing -> %', command;
|
2008-11-01 09:07:34 +07:00
|
|
|
execute command;
|
|
|
|
dropviews[j] := viewname[j];
|
|
|
|
end loop;
|
|
|
|
exception
|
|
|
|
when others then
|
|
|
|
i := array_upper(dropviews, 1);
|
|
|
|
if i > 0 then
|
2013-02-09 01:55:24 +07:00
|
|
|
for j in reverse i .. 1 loop
|
2008-11-01 09:07:34 +07:00
|
|
|
command := 'create or replace view ' || dropviews[j] || ' as ' || viewtext[j];
|
2013-02-09 01:55:24 +07:00
|
|
|
raise notice 'executing -> %', 'create view ' || dropviews[j];
|
2008-11-01 09:07:34 +07:00
|
|
|
execute command;
|
|
|
|
end loop;
|
|
|
|
end if;
|
2013-02-09 01:55:24 +07:00
|
|
|
raise exception 'Failed to recreate dependent view. SQLERRM=%', SQLERRM;
|
2008-11-01 09:07:34 +07:00
|
|
|
end;
|
|
|
|
end if;
|
|
|
|
command := 'alter table ' || lower(tablename) || ' alter column ' || lower(columnname) || ' type ' || lower(datatype);
|
2013-02-09 01:55:24 +07:00
|
|
|
raise notice 'executing -> %', command;
|
2008-11-01 09:07:34 +07:00
|
|
|
execute command;
|
|
|
|
i := array_upper(dropviews, 1);
|
|
|
|
if i > 0 then
|
2013-02-09 01:55:24 +07:00
|
|
|
for j in reverse i .. 1 loop
|
2008-11-01 09:07:34 +07:00
|
|
|
command := 'create or replace view ' || dropviews[j] || ' as ' || viewtext[j];
|
2013-02-09 01:55:24 +07:00
|
|
|
raise notice 'executing -> %', 'create view ' || dropviews[j];
|
2008-11-01 09:07:34 +07:00
|
|
|
execute command;
|
2018-07-17 02:00:00 +07:00
|
|
|
command := perms[j];
|
|
|
|
raise notice 'executing -> %', 'grant ' || perms[j];
|
|
|
|
execute command;
|
2008-11-01 09:07:34 +07:00
|
|
|
end loop;
|
|
|
|
end if;
|
|
|
|
end if;
|
|
|
|
end if;
|
|
|
|
|
|
|
|
if defaultclause is not null then
|
|
|
|
if lower(defaultclause) = 'null' then
|
|
|
|
command := 'alter table ' || lower(tablename) || ' alter column ' || lower(columnname) || ' drop default ';
|
|
|
|
else
|
2013-03-15 13:03:01 +07:00
|
|
|
if defaultclause ~ '.*[(].*[)].*' or lower(defaultclause) = 'current_timestamp' then
|
|
|
|
command := 'alter table ' || lower(tablename) || ' alter column ' || lower(columnname) || ' set default ' || defaultclause;
|
|
|
|
else
|
|
|
|
command := 'alter table ' || lower(tablename) || ' alter column ' || lower(columnname) || ' set default ''' || defaultclause || '''';
|
|
|
|
end if;
|
2008-11-01 09:07:34 +07:00
|
|
|
end if;
|
2013-02-09 01:55:24 +07:00
|
|
|
raise notice 'executing -> %', command;
|
2008-11-01 09:07:34 +07:00
|
|
|
execute command;
|
|
|
|
end if;
|
|
|
|
|
|
|
|
if nullclause is not null then
|
|
|
|
if lower(nullclause) = 'not null' then
|
|
|
|
command := 'alter table ' || lower(tablename) || ' alter column ' || lower(columnname) || ' set not null';
|
2013-02-09 01:55:24 +07:00
|
|
|
raise notice 'executing -> %', command;
|
2008-11-01 09:07:34 +07:00
|
|
|
execute command;
|
|
|
|
elsif lower(nullclause) = 'null' then
|
|
|
|
command := 'alter table ' || lower(tablename) || ' alter column ' || lower(columnname) || ' drop not null';
|
2013-02-09 01:55:24 +07:00
|
|
|
raise notice 'executing -> %', command;
|
2008-11-01 09:07:34 +07:00
|
|
|
execute command;
|
|
|
|
end if;
|
|
|
|
end if;
|
|
|
|
end;
|
2018-07-17 02:00:00 +07:00
|
|
|
$function$
|
2008-11-01 09:07:34 +07:00
|
|
|
|
2013-02-09 01:55:24 +07:00
|
|
|
/*
|
2008-11-01 09:07:34 +07:00
|
|
|
create table t_alter_column
|
|
|
|
( tablename name, columnname name, datatype name, nullclause varchar(10), defaultclause varchar(200));
|
|
|
|
|
|
|
|
create rule alter_column_rule as on insert to t_alter_column
|
|
|
|
do instead select altercolumn(new.tablename, new.columnname, new.datatype, new.nullclause,
|
|
|
|
new.defaultclause);
|
2013-02-09 01:55:24 +07:00
|
|
|
*/
|