-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_columns.sql
More file actions
67 lines (67 loc) · 1.9 KB
/
Copy path_columns.sql
File metadata and controls
67 lines (67 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
create function schema._columns(_schemas text[])
returns table (
type text,
schema text,
name text,
table_schema text,
table_name text,
order_by int,
comment text,
short_definition text,
definition text
)
language sql
as
$$
select
sub.type,
sub.schema,
sub.name,
sub.table_schema,
sub.table_name,
sub.order_by,
sub.comment,
sub.definition as short_definition,
concat(
'ALTER TABLE ',
schema._ident(sub.table_schema, sub.table_name),
' ADD COLUMN ',
sub.definition,
';',
case when sub.comment is not null then concat(
E'\n',
'COMMENT ON COLUMN ',
schema._ident(sub.table_schema, sub.table_name), '.', quote_ident(sub.name),
' IS ',
schema._quote(sub.comment),
';'
) else '' end
) as definition
from (
select
'column' as type,
c.table_schema::text as schema,
c.column_name::text as name,
c.table_schema::text as table_schema,
c.table_name::text as table_name,
c.ordinal_position as order_by,
pgdesc.description as comment,
concat(
quote_ident(c.column_name),
' ',
(c.udt_schema || '.' || c.udt_name)::regtype::text,
' ',
schema._parse_column(c)
) as definition
from
information_schema.columns c
join information_schema.tables t
on c.table_schema = t.table_schema and c.table_name = t.table_name and t.table_type = 'BASE TABLE'
left outer join pg_catalog.pg_statio_user_tables pgtbl
on pgtbl.schemaname = c.table_schema and c.table_name = pgtbl.relname
left outer join pg_catalog.pg_description pgdesc
on pgtbl.relid = pgdesc.objoid and c.ordinal_position = pgdesc.objsubid
where
c.table_schema = any(_schemas)
) sub
$$;