如何解析pg_dump各种对象到它自己的文件中?像函数、table、视图、索引等
How to parse pg_dump all kinds of objects to it own file? Like function, table, view, index etc
我正在尝试将 pg_dump 文件解析为对象拥有的单个文件,以使其更易于开发和维护。
这是一个例子:
--
-- Name: u_id; Type: DEFAULT; Schema: sc_udccockpit; Owner: udccockpit
--
ALTER TABLE ONLY alarmentity
ADD CONSTRAINT alarmentity_pkey PRIMARY KEY (u_id);
--
-- Name: alarmstring_pkey; Type: CONSTRAINT; Schema: sc_udccockpit; Owner: udccockpit; Tablespace:
--
ALTER TABLE ONLY alarmentity
ADD CONSTRAINT alarmentity_f_active_description_id_fkey FOREIGN KEY (f_active_description_id) REFERENCES alarmstring(u_id);
--
-- Name: alarmentity_f_model_description_id_fkey; Type: FK CONSTRAINT; Schema: sc_udccockpit; Owner: udccockpit
--
ALTER TABLE ONLY alarmentity
ADD CONSTRAINT alarmentity_f_model_description_id_fkey FOREIGN KEY (f_model_description_id) REFERENCES alarmstring(u_id);
--
-- Name: alarmentity_f_module_id_fkey; Type: FK CONSTRAINT; Schema: sc_udccockpit; Owner: udccockpit
--
ALTER TABLE ONLY alarmentity
ADD CONSTRAINT alarmentity_f_module_id_fkey FOREIGN KEY (f_module_id) REFERENCES alarmstring(u_id);
--
-- Name: alarmentity_f_node_name_id_fkey; Type: FK CONSTRAINT; Schema: sc_udccockpit; Owner: udccockpit
--
ALTER TABLE ONLY alarmentity
ADD CONSTRAINT alarmentity_f_node_name_id_fkey FOREIGN KEY (f_node_name_id) REFERENCES alarmstring(u_id);
--
-- Name: alarmentity_f_resource_id_id_fkey; Type: FK CONSTRAINT; Schema: sc_udccockpit; Owner: udccockpit
--
ALTER TABLE ONLY alarmentity
ADD CONSTRAINT alarmentity_f_resource_id_id_fkey FOREIGN KEY (f_resource_id_id) REFERENCES alarmstring(u_id);
我想将其更改为,例如:
文件:alarmentity_pkey.sql 里面只有
这样的内容
--
-- Name: u_id; Type: DEFAULT; Schema: sc_udccockpit; Owner: udccockpit
--
ALTER TABLE ONLY alarmentity
ADD CONSTRAINT alarmentity_pkey PRIMARY KEY (u_id);
我想我想要的是:
从 "ALTER TABLE ONLY" 开始
以“;”结尾
包括 "CONSTRAINT"、"PRIMARY"
提取 "CONSTRAINT" 和 "PRIMARY" 之间的单词作为文件名。
好的,经过多次尝试,我找到了答案。要解析并获取 "fkey only" 单个文件,可以使用 'awk' 命令。
function extract_constraint_fkey(){
awk ' /--/,/\;$/ { str = str ? str "\n" [=10=] : [=10=] } /\;$/ { if( str ~ /FOREIGN KEY/ ) { print str > "database\/constraint\/fkey\/"substr(,1)".sql" };str = "" };'
}
你可以尝试按照这个继续解析主键、索引、alter sequence等涉及到对表进行约束的操作
BR//李元文
我正在尝试将 pg_dump 文件解析为对象拥有的单个文件,以使其更易于开发和维护。
这是一个例子:
--
-- Name: u_id; Type: DEFAULT; Schema: sc_udccockpit; Owner: udccockpit
--
ALTER TABLE ONLY alarmentity
ADD CONSTRAINT alarmentity_pkey PRIMARY KEY (u_id);
--
-- Name: alarmstring_pkey; Type: CONSTRAINT; Schema: sc_udccockpit; Owner: udccockpit; Tablespace:
--
ALTER TABLE ONLY alarmentity
ADD CONSTRAINT alarmentity_f_active_description_id_fkey FOREIGN KEY (f_active_description_id) REFERENCES alarmstring(u_id);
--
-- Name: alarmentity_f_model_description_id_fkey; Type: FK CONSTRAINT; Schema: sc_udccockpit; Owner: udccockpit
--
ALTER TABLE ONLY alarmentity
ADD CONSTRAINT alarmentity_f_model_description_id_fkey FOREIGN KEY (f_model_description_id) REFERENCES alarmstring(u_id);
--
-- Name: alarmentity_f_module_id_fkey; Type: FK CONSTRAINT; Schema: sc_udccockpit; Owner: udccockpit
--
ALTER TABLE ONLY alarmentity
ADD CONSTRAINT alarmentity_f_module_id_fkey FOREIGN KEY (f_module_id) REFERENCES alarmstring(u_id);
--
-- Name: alarmentity_f_node_name_id_fkey; Type: FK CONSTRAINT; Schema: sc_udccockpit; Owner: udccockpit
--
ALTER TABLE ONLY alarmentity
ADD CONSTRAINT alarmentity_f_node_name_id_fkey FOREIGN KEY (f_node_name_id) REFERENCES alarmstring(u_id);
--
-- Name: alarmentity_f_resource_id_id_fkey; Type: FK CONSTRAINT; Schema: sc_udccockpit; Owner: udccockpit
--
ALTER TABLE ONLY alarmentity
ADD CONSTRAINT alarmentity_f_resource_id_id_fkey FOREIGN KEY (f_resource_id_id) REFERENCES alarmstring(u_id);
我想将其更改为,例如: 文件:alarmentity_pkey.sql 里面只有
这样的内容--
-- Name: u_id; Type: DEFAULT; Schema: sc_udccockpit; Owner: udccockpit
--
ALTER TABLE ONLY alarmentity
ADD CONSTRAINT alarmentity_pkey PRIMARY KEY (u_id);
我想我想要的是: 从 "ALTER TABLE ONLY" 开始 以“;”结尾 包括 "CONSTRAINT"、"PRIMARY" 提取 "CONSTRAINT" 和 "PRIMARY" 之间的单词作为文件名。
好的,经过多次尝试,我找到了答案。要解析并获取 "fkey only" 单个文件,可以使用 'awk' 命令。
function extract_constraint_fkey(){
awk ' /--/,/\;$/ { str = str ? str "\n" [=10=] : [=10=] } /\;$/ { if( str ~ /FOREIGN KEY/ ) { print str > "database\/constraint\/fkey\/"substr(,1)".sql" };str = "" };'
}
你可以尝试按照这个继续解析主键、索引、alter sequence等涉及到对表进行约束的操作
BR//李元文