Skip to content

Commit faa85fe

Browse files
committed
refactor tree sql, log client_addr
1 parent a804f30 commit faa85fe

File tree

5 files changed

+325
-298
lines changed

5 files changed

+325
-298
lines changed

compose.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: pgblackboard
22
services:
3-
pgbb:
3+
dev:
44
build:
55
context: .
66
target: dev
@@ -18,10 +18,10 @@ services:
1818
path: .
1919
target: /app
2020

21-
pgbbprod:
21+
prod:
2222
image: exedealer/pgblackboard
2323
build: .
24-
ports: [7891:7890]
24+
ports: [7890:7890]
2525
profiles: [prod]
2626
# depends_on: [postgres]
2727
# command: [pgbb, postgres://postgres:5432]

makefile

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
.PHONY: up shell dist clean ui/_vendor/* server/_vendor/*
1+
.PHONY: up produp shell dist clean ui/_vendor/* server/_vendor/*
22

33
up:
4-
COMPOSE_BAKE=true docker compose up --build --watch --menu=false
4+
COMPOSE_BAKE=true docker compose up --build --watch --menu=false dev postgres
5+
6+
produp:
7+
COMPOSE_BAKE=true docker compose up --build --menu=false prod postgres
58

69
shell:
7-
COMPOSE_BAKE=true docker compose run --build --rm --volume $(PWD):/w --workdir /w pgbb ash
10+
COMPOSE_BAKE=true docker compose run --build --rm --volume $(PWD):/w --workdir /w dev ash
811

912
clean:
1013
rm -rf ui/.build dist
@@ -20,23 +23,18 @@ dist/pgbb.js: \
2023
ui/.build/main.js \
2124
ui/.build/map.js
2225

23-
deno bundle \
24-
--unstable-raw-imports \
26+
deno bundle ./server/pgbb.js --output=$@ \
2527
--import-map=ui/.build/importmap.json \
26-
./server/pgbb.js \
27-
--output=$@
28+
--unstable-raw-imports
2829

2930
ui/.build/importmap.json:
30-
install -D /dev/null $@
31-
echo '{ "imports": { "../assets.js": "./assets.js" } }' > $@
31+
echo '{ "imports": { "../assets.js": "./assets.js" } }' | install -D /dev/stdin $@
3232

3333
ui/.build/assets.js: ui/assets.js
3434
esbuild $< --outfile=$@ --drop-labels=DEV
3535

3636
ui/.build/%.js: ui/%.js
37-
esbuild $< --outfile=$@ \
38-
--bundle \
39-
--format=esm
37+
esbuild $< --outfile=$@ --bundle --format=esm
4038

4139
ui/.build/style.css: ui/style.css
4240
esbuild $< --outfile=$@ \

server/api_defn.sql

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
-- "database"
2+
select concat_ws(e'\n'
3+
, format('\connect %I', current_catalog)
4+
, ''
5+
, 'SELECT * FROM text(''hello world'');'
6+
, ''
7+
, format('-- https://www.postgresql.org/docs/%s/sql-alterdatabase.html', pg_major_ver)
8+
, ''
9+
)
10+
from substring(current_setting('server_version') from '\d+') pg_major_ver
11+
where 'database' = $1
12+
13+
-- schema
14+
union all
15+
select concat_ws(e'\n'
16+
, format('\connect %I', current_catalog)
17+
, ''
18+
, format('-- https://www.postgresql.org/docs/%s/sql-alterschema.html', pg_major_ver)
19+
, ''
20+
)
21+
from substring(current_setting('server_version') from '\d+') pg_major_ver
22+
where 'schema' = $1
23+
24+
-- table
25+
union all
26+
select concat_ws(e'\n'
27+
, format('\connect %I', current_catalog)
28+
, ''
29+
, 'SELECT'
30+
, select_cols
31+
, format('FROM %I.%I', nspname, relname)
32+
, format('-- WHERE (%s) = ('''')', orderby_cols)
33+
, 'ORDER BY ' || orderby_cols
34+
, 'LIMIT 1000'
35+
, ';'
36+
, ''
37+
, format('-- https://www.postgresql.org/docs/%s/sql-altertable.html', pg_major_ver)
38+
, ''
39+
, '/*'
40+
, (
41+
case relkind
42+
-- view
43+
when 'v' then format(
44+
e'CREATE OR REPLACE VIEW %I.%I AS\n%s'
45+
, nspname
46+
, relname
47+
, pg_get_viewdef(pg_class.oid, true)
48+
)
49+
else 'CREATE TABLE'
50+
end
51+
)
52+
, '*/'
53+
, ''
54+
)
55+
from substring(current_setting('server_version') from '\d+') pg_major_ver
56+
, pg_class
57+
join pg_namespace on pg_class.relnamespace = pg_namespace.oid
58+
left join pg_constraint pk on (contype, conrelid) = ('p', pg_class.oid)
59+
, lateral (
60+
select string_agg(format(' %I', attname), e',\n' order by attnum)
61+
-- TODO desc indexing
62+
, string_agg(format('%I', attname), ', ' order by pk_pos) filter (where pk_pos is not null)
63+
from pg_attribute, array_position(pk.conkey, attnum) pk_pos
64+
where attrelid = pg_class.oid and (attnum > 0 or attname = 'oid') and not attisdropped
65+
) _(select_cols, orderby_cols)
66+
where ('table', pg_class.oid) = ($1, $2)
67+
68+
-- function
69+
union all
70+
select concat_ws(e'\n'
71+
, format('\connect %I', current_catalog)
72+
, ''
73+
, pg_get_functiondef(p.oid) || ';'
74+
, ''
75+
, '/*'
76+
, format('DROP %s %s(%s);'
77+
, case when pg_get_function_result(p.oid) is null then 'PROCEDURE' else 'FUNCTION' end
78+
, regproc(p.oid)
79+
, pg_get_function_identity_arguments(p.oid)
80+
)
81+
, '*/'
82+
, ''
83+
)
84+
from pg_proc p
85+
where ('function', p.oid) = ($1, $2)
86+
and not exists (select from pg_aggregate where aggfnoid = p.oid)
87+
88+
89+
-- aggregate
90+
union all
91+
select concat_ws(e'\n'
92+
, format('\connect %I', current_catalog)
93+
, ''
94+
, format('CREATE OR REPLACE AGGREGATE %s(%s) (', aggfnoid, fnargs)
95+
, ' SFUNC = ' || aggtransfn
96+
, ' ,STYPE = ' || format_type(aggtranstype, null)
97+
, ' ,FINALFUNC = ' || nullif(aggfinalfn, 0)::regproc
98+
, ' ,INITCOND = ' || array_to_string(nullif(agginitval, '')::text[], ', ')
99+
, ' ,SORTOP = ' || nullif(aggsortop, 0)::regoperator
100+
, ');'
101+
, ''
102+
, '/*'
103+
, format('DROP AGGREGATE %s(%s);', aggfnoid, fnargs)
104+
, '*/'
105+
, ''
106+
)
107+
from pg_aggregate, pg_get_function_identity_arguments(aggfnoid) fnargs
108+
where ('function', aggfnoid) = ($1, $2)
109+
110+
-- constraint
111+
union all
112+
select concat_ws(e'\n'
113+
, format('\connect %I', current_catalog)
114+
, ''
115+
, format('ALTER TABLE %s DROP CONSTRAINT %I;'
116+
, conrelid::regclass
117+
, conname
118+
)
119+
, ''
120+
, format('ALTER TABLE %s ADD CONSTRAINT %I %s;'
121+
, conrelid::regclass
122+
, conname
123+
, pg_get_constraintdef(oid)
124+
, ''
125+
)
126+
)
127+
from pg_constraint
128+
where ('constraint', oid) = ($1, $2)
129+
130+
-- index
131+
union all
132+
select concat_ws(e'\n'
133+
, format('\connect %I', current_catalog)
134+
, ''
135+
, format('DROP INDEX %s;', oid::regclass)
136+
, ''
137+
, pg_get_indexdef(oid) || ';'
138+
, ''
139+
)
140+
from pg_class
141+
where ('index', oid) = ($1, $2)
142+
143+
-- trigger
144+
union all
145+
select concat_ws(e'\n'
146+
, format('\connect %I', current_catalog)
147+
, ''
148+
, format('DROP TRIGGER %I ON %s;', tgname, tgrelid::regclass)
149+
, ''
150+
, pg_get_triggerdef(oid, true)
151+
, ''
152+
)
153+
from pg_trigger
154+
where ('trigger', oid) = ($1, $2)
155+
156+
-- file
157+
union all
158+
select concat_ws(e'\n'
159+
, format('\connect %I', current_catalog)
160+
, ''
161+
, format('SELECT pg_read_file(%L, 0, 5000);', $3)
162+
, ''
163+
)
164+
where 'file' = $1

server/api_tree.sql

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
select text '' db
2+
, text '' ntype
3+
, oid '0' noid
4+
, text '' ntid
5+
, text '' collate "C" "name"
6+
, text '' descr
7+
, text '' mod
8+
, float8 '0' size
9+
, bool 'false' leaf
10+
, int2 '0' ord
11+
where false
12+
13+
-- databases
14+
union all
15+
select datname, 'database', null, null, datname, descr, null, null, false, 1
16+
from pg_database, shobj_description(oid, 'pg_database') descr
17+
where $1 is null
18+
and not datistemplate
19+
20+
-- schemas
21+
union all
22+
select current_catalog, 'schema', oid, null, nspname, descr, null, null, false, 1
23+
from pg_namespace, obj_description(oid, 'pg_namespace') descr
24+
where 'database' = $1
25+
and nspname not like all ('{ pg\\_toast , pg\\_temp\\_% , pg\\_toast\\_temp\\_% }')
26+
27+
-- functions
28+
union all
29+
select current_catalog, 'function', p.oid, null, sign, descr, mod, null, true, 2
30+
from pg_proc p
31+
left join pg_aggregate agg on aggfnoid = oid
32+
, obj_description(p.oid, 'pg_proc') descr
33+
, pg_get_function_result(p.oid) fnres
34+
, format(
35+
'%s (%s)%s'
36+
, proname
37+
, pg_get_function_identity_arguments(p.oid)
38+
, e' \u2022 ' || fnres
39+
) sign
40+
,concat_ws(' '
41+
, case when agg is not null then 'aggregate' end
42+
, case when fnres is null then 'procedure' end
43+
) mod
44+
where ('schema', pronamespace) = ($1, $2)
45+
46+
-- tables
47+
union all
48+
select current_catalog, 'table', oid, null, relname, descr, mod, size, false, 1
49+
from pg_class
50+
, obj_description(oid, 'pg_class') descr
51+
, format('table_%s', relkind) mod
52+
, nullif(reltuples, -1) size
53+
where ('schema', relnamespace) = ($1, $2)
54+
and relkind not in ('i', 'I', 't', 'c', 'S')
55+
56+
-- columns
57+
union all
58+
select current_catalog, 'column', attrelid, text(attnum), attname, descr, mod, null, true, attnum
59+
from pg_attribute
60+
, concat_ws(' '
61+
, format_type(atttypid, atttypmod)
62+
, case when attnotnull then 'not null' end
63+
, '-- ' || col_description(attrelid, attnum)
64+
) descr
65+
, concat_ws(' '
66+
, (
67+
select 'column_pk'
68+
from pg_constraint
69+
where conrelid = attrelid and contype = 'p' and attnum = any(conkey)
70+
-- limit 1
71+
)
72+
) mod
73+
where ('table', attrelid) = ($1, $2)
74+
and (attnum > 0 or attname = 'oid')
75+
and not attisdropped
76+
77+
-- constraints
78+
union all
79+
select current_catalog, 'constraint', oid, null, conname, descr, mod, null, true, 10010
80+
from pg_constraint
81+
, obj_description(oid, 'pg_constraint') descr
82+
, concat_ws(' '
83+
, format('constraint_%s', contype)
84+
, case when not convalidated then 'constraint_not_validated' end
85+
) mod
86+
where ('table', conrelid) = ($1, $2)
87+
-- and contype not in ()
88+
89+
-- indexes
90+
union all
91+
select current_catalog, 'index', indexrelid, null, relname, descr, mod, null, true, 10020
92+
from pg_index join pg_class on indexrelid = oid
93+
, obj_description(indexrelid, 'pg_class') descr
94+
, concat_ws(' ', null) mod -- TODO uniq
95+
where ('table', indrelid) = ($1, $2)
96+
97+
-- triggers
98+
union all
99+
select current_catalog, 'trigger', oid, null, tgname, descr, mod, null, true, 10030
100+
from pg_trigger
101+
, obj_description(oid, 'pg_trigger') descr
102+
, concat_ws(' ', null) mod -- TODO tgenabled=D
103+
where ('table', tgrelid) = ($1, $2)
104+
and tgconstraint = 0
105+
106+
-- fs
107+
union all
108+
select current_catalog, 'dir', null, '.', './', null, null, null, false, 2
109+
where $1 is null
110+
and has_function_privilege('pg_ls_dir(text)', 'execute')
111+
112+
-- dir
113+
union all
114+
select current_catalog, 'dir', null, fpath, fname, null, null, null, false, 1
115+
from pg_ls_dir($3) fname
116+
, concat($3, '/', fname) fpath
117+
, pg_stat_file(fpath) stat
118+
where 'dir' = $1
119+
and stat.isdir
120+
121+
-- file
122+
union all
123+
select current_catalog, 'file', null, fpath, fname, null, null, null, true, 2
124+
from pg_ls_dir($3) fname
125+
, concat($3, '/', fname) fpath
126+
, pg_stat_file(fpath) stat
127+
where 'dir' = $1
128+
and not stat.isdir
129+
130+
order by ord, "name"

0 commit comments

Comments
 (0)