DBModel.sql 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. create or replace function update_db_model() returns void as
  2. $$
  3. begin
  4. ------------------------------------------------------------------------------------------------
  5. --
  6. if not exists(select * from information_schema.tables
  7. where
  8. table_catalog = CURRENT_CATALOG and table_schema = CURRENT_SCHEMA
  9. and table_name = 'carrier_data') then
  10. CREATE TABLE "carrier_data"
  11. (
  12. "guid" text NOT NULL,
  13. "load_time" timestamp without time zone,
  14. "unload_time" timestamp without time zone,
  15. "rfid" text,
  16. "lot_id" text,
  17. "product_category" text,
  18. "station" text,
  19. CONSTRAINT "carrier_data_pkey" PRIMARY KEY ("guid" )
  20. )
  21. WITH (
  22. OIDS=FALSE
  23. );
  24. ALTER TABLE "carrier_data"
  25. OWNER TO postgres;
  26. GRANT SELECT ON TABLE "carrier_data" TO postgres;
  27. end if;
  28. ------------------------------------------------------------------------------------------------
  29. --
  30. if not exists(select * from information_schema.tables
  31. where
  32. table_catalog = CURRENT_CATALOG and table_schema = CURRENT_SCHEMA
  33. and table_name = 'wafer_data') then
  34. CREATE TABLE "wafer_data"
  35. (
  36. "guid" text NOT NULL,
  37. "create_time" timestamp without time zone,
  38. "delete_time" timestamp without time zone,
  39. "carrier_data_guid" text,
  40. "create_station" text,
  41. "create_slot" text,
  42. "process_data_guid" text,
  43. "wafer_id" text,
  44. "lasermarker1" text,
  45. "lasermarker2" text,
  46. "lasermarker3" text,
  47. "t7code1" text,
  48. "t7code2" text,
  49. "t7code3" text,
  50. "pj_data_guid" text,
  51. "lot_data_guid" text,
  52. CONSTRAINT "wafer_data_pkey" PRIMARY KEY ("guid" )
  53. )
  54. WITH (
  55. OIDS=FALSE
  56. );
  57. ALTER TABLE "wafer_data"
  58. OWNER TO postgres;
  59. GRANT SELECT ON TABLE "wafer_data" TO postgres;
  60. end if;
  61. if not exists(select * from information_schema.columns
  62. where table_catalog = CURRENT_CATALOG and table_schema = CURRENT_SCHEMA and table_name = 'wafer_data' and column_name = 'reason') then
  63. Alter TABLE "wafer_data" ADD column reason text;
  64. end if;
  65. ------------------------------------------------------------------------------------------------
  66. --
  67. if not exists(select * from information_schema.tables
  68. where
  69. table_catalog = CURRENT_CATALOG and table_schema = CURRENT_SCHEMA
  70. and table_name = 'event_data') then
  71. CREATE TABLE "event_data"
  72. (
  73. "gid" serial NOT NULL,
  74. "event_id" integer,
  75. "event_enum" text,
  76. "type" text,
  77. "source" text,
  78. "description" text,
  79. "level" text,
  80. "occur_time" timestamp without time zone,
  81. CONSTRAINT "event_data_pkey" PRIMARY KEY ("gid" )
  82. )
  83. WITH (
  84. OIDS=FALSE
  85. );
  86. --ALTER TABLE "EventManager"
  87. --OWNER TO postgres;
  88. GRANT ALL ON TABLE "event_data" TO postgres;
  89. GRANT SELECT ON TABLE "event_data" TO postgres;
  90. CREATE INDEX "event_data_occur_time_event_id_idx"
  91. ON "event_data"
  92. USING btree
  93. ("occur_time" , "event_id" );
  94. end if;
  95. ------------------------------------------------------------------------------------------------
  96. --
  97. if not exists(select * from information_schema.tables
  98. where
  99. table_catalog = CURRENT_CATALOG and table_schema = CURRENT_SCHEMA
  100. and table_name = 'wafer_move_history') then
  101. CREATE TABLE "wafer_move_history"
  102. (
  103. "gid" serial NOT NULL,
  104. "wafer_data_guid" text,
  105. "arrive_time" timestamp without time zone,
  106. "station" text,
  107. "slot" text,
  108. "status" text,
  109. CONSTRAINT "wafer_move_history_pkey" PRIMARY KEY ("gid" )
  110. )
  111. WITH (
  112. OIDS=FALSE
  113. );
  114. ALTER TABLE "wafer_move_history"
  115. OWNER TO postgres;
  116. GRANT SELECT ON TABLE "wafer_move_history" TO postgres;
  117. end if;
  118. ------------------------------------------------------------------------------------------------
  119. --
  120. if not exists(select * from information_schema.tables
  121. where
  122. table_catalog = CURRENT_CATALOG and table_schema = CURRENT_SCHEMA
  123. and table_name = 'process_data') then
  124. CREATE TABLE "process_data"
  125. (
  126. "guid" text NOT NULL,
  127. "process_begin_time" timestamp without time zone,
  128. "process_end_time" timestamp without time zone,
  129. "recipe_name" text,
  130. "chuck_name" text,
  131. "dechuck_name" text,
  132. "process_status" text,
  133. "wafer_data_guid" text,
  134. "process_in" text,
  135. CONSTRAINT "process_data_pkey" PRIMARY KEY ("guid" )
  136. )
  137. WITH (
  138. OIDS=FALSE
  139. );
  140. ALTER TABLE "process_data"
  141. OWNER TO postgres;
  142. GRANT SELECT ON TABLE "process_data" TO postgres;
  143. end if;
  144. ------------------------------------------------------------------------------------------------
  145. --
  146. if not exists(select * from information_schema.tables
  147. where
  148. table_catalog = CURRENT_CATALOG and table_schema = CURRENT_SCHEMA
  149. and table_name = 'stats_data') then
  150. CREATE TABLE "stats_data"
  151. (
  152. "name" text,
  153. "value" integer,
  154. "total" integer,
  155. "description" text,
  156. "last_update_time" timestamp without time zone,
  157. "last_reset_time" timestamp without time zone,
  158. "last_total_reset_time" timestamp without time zone,
  159. "is_visible" boolean,
  160. "enable_alarm" boolean,
  161. "alarm_value" integer,
  162. CONSTRAINT "stats_data_pkey" PRIMARY KEY ("name" )
  163. )
  164. WITH (
  165. OIDS=FALSE
  166. );
  167. ALTER TABLE "stats_data"
  168. OWNER TO postgres;
  169. GRANT SELECT ON TABLE "stats_data" TO postgres;
  170. end if;
  171. ------------------------------------------------------------------------------------------------
  172. --
  173. if not exists(select * from information_schema.tables
  174. where
  175. table_catalog = CURRENT_CATALOG and table_schema = CURRENT_SCHEMA
  176. and table_name = 'leak_check_data') then
  177. CREATE TABLE "leak_check_data"
  178. (
  179. "guid" text NOT NULL,
  180. "operate_time" timestamp without time zone,
  181. "status" text,
  182. "leak_rate" real,
  183. "start_pressure" real,
  184. "stop_pressure" real,
  185. "mode" text,
  186. "leak_check_time" integer,
  187. CONSTRAINT "leak_check_data_pkey" PRIMARY KEY ("guid" )
  188. )
  189. WITH (
  190. OIDS=FALSE
  191. );
  192. ALTER TABLE "leak_check_data"
  193. OWNER TO postgres;
  194. GRANT ALL ON TABLE "leak_check_data" TO postgres;
  195. GRANT SELECT ON TABLE "leak_check_data" TO postgres;
  196. end if;
  197. ------------------------------------------------------------------------------------------------
  198. --
  199. if not exists(select * from information_schema.tables
  200. where
  201. table_catalog = CURRENT_CATALOG and table_schema = CURRENT_SCHEMA
  202. and table_name = 'cj_data') then
  203. CREATE TABLE cj_data
  204. (
  205. "guid" text NOT NULL,
  206. "start_time" timestamp without time zone,
  207. "end_time" timestamp without time zone,
  208. "carrier_data_guid" text,
  209. "name" text,
  210. "input_port" text,
  211. "output_port" text,
  212. CONSTRAINT "cj_data_pkey" PRIMARY KEY ("guid" )
  213. )
  214. WITH (
  215. OIDS=FALSE
  216. );
  217. ALTER TABLE "cj_data"
  218. OWNER TO postgres;
  219. GRANT SELECT ON TABLE "cj_data" TO postgres;
  220. end if;
  221. ------------------------------------------------------------------------------------------------
  222. ------------------------------------------------------------------------------------------------
  223. --
  224. if not exists(select * from information_schema.tables
  225. where
  226. table_catalog = CURRENT_CATALOG and table_schema = CURRENT_SCHEMA
  227. and table_name = 'pj_data') then
  228. CREATE TABLE pj_data
  229. (
  230. "guid" text NOT NULL,
  231. "start_time" timestamp without time zone,
  232. "end_time" timestamp without time zone,
  233. "carrier_data_guid" text,
  234. "cj_data_guid" text,
  235. "name" text,
  236. "input_port" text,
  237. "output_port" text,
  238. "total_wafer_count" integer,
  239. "abort_wafer_count" integer,
  240. "unprocessed_wafer_count" integer,
  241. CONSTRAINT "pj_data_pkey" PRIMARY KEY ("guid" )
  242. )
  243. WITH (
  244. OIDS=FALSE
  245. );
  246. ALTER TABLE "pj_data"
  247. OWNER TO postgres;
  248. GRANT SELECT ON TABLE "pj_data" TO postgres;
  249. end if;
  250. ------------------------------------------------------------------------------------------------
  251. --
  252. if not exists(select * from information_schema.tables
  253. where
  254. table_catalog = CURRENT_CATALOG and table_schema = CURRENT_SCHEMA
  255. and table_name = 'offset_data') then
  256. CREATE TABLE "offset_data"
  257. (
  258. "guid" text NOT NULL,
  259. "source_module" text NOT NULL,
  260. "source_slot" integer NOT NULL,
  261. "destination_module" text NOT NULL,
  262. "destination_slot" integer NOT NULL,
  263. "origin_module" text NOT NULL,
  264. "origin_slot" integer NOT NULL,
  265. "arm_position" text NOT NULL,
  266. "arm_pan" text NOT NULL,
  267. "offset_x" real NOT NULL,
  268. "offset_y" real NOT NULL,
  269. "offset_d" real NOT NULL,
  270. "start_time" timestamp without time zone,
  271. "end_time" timestamp without time zone,
  272. CONSTRAINT "offset_data_pkey" PRIMARY KEY ("guid","start_time","end_time")
  273. )
  274. WITH (
  275. OIDS=FALSE
  276. );
  277. ALTER TABLE "offset_data"
  278. OWNER TO postgres;
  279. GRANT SELECT ON TABLE "offset_data" TO postgres;
  280. end if;
  281. ------------------------------------------------------------------------------------------------
  282. --
  283. if not exists(select * from information_schema.tables
  284. where
  285. table_catalog = CURRENT_CATALOG and table_schema = CURRENT_SCHEMA
  286. and table_name = 'stats_data_rf_pump') then
  287. CREATE TABLE "stats_data_rf_pump"
  288. (
  289. "name" text,
  290. "description" text,
  291. "last_pm_time" timestamp without time zone,
  292. "from_last_pm" real,
  293. "total" real,
  294. "pm_interval" real,
  295. "enable_alarm" boolean,
  296. CONSTRAINT "stats_data_rf_pump_pkey" PRIMARY KEY ("name" )
  297. )
  298. WITH (
  299. OIDS=FALSE
  300. );
  301. ALTER TABLE "stats_data_rf_pump"
  302. OWNER TO postgres;
  303. GRANT SELECT ON TABLE "stats_data_rf_pump" TO postgres;
  304. end if;
  305. ------------------------------------------------------------------------------------------------
  306. if not exists(select * from information_schema.tables
  307. where
  308. table_catalog = CURRENT_CATALOG and table_schema = CURRENT_SCHEMA
  309. and table_name = 'wafer_holder') then
  310. CREATE TABLE "wafer_holder"
  311. (
  312. "id" text,
  313. "buffer_id" integer,
  314. "crs_type" text,
  315. "wafer_size" integer,
  316. "enabled" boolean,
  317. "chemistry" text,
  318. "crsa_id" text,
  319. "crsa_total_uses" integer,
  320. "crsa_pad_uses" integer,
  321. "crsa_seal_uses" integer,
  322. "crsb_id" text,
  323. "crsb_total_uses" integer,
  324. "crsb_pad_uses" integer,
  325. "crsb_seal_uses" integer,
  326. "total_dummy_wafers" integer,
  327. "total_product_wafers" integer,
  328. "current_location" text,
  329. "wafera_id" text,
  330. "wafera_type" integer,
  331. "waferb_id" text,
  332. "waferb_type" integer,
  333. "status" integer,
  334. "create_time" timestamp without time zone,
  335. "update_time" timestamp without time zone,
  336. CONSTRAINT "wafer_holder_pkey" PRIMARY KEY ("id" )
  337. )
  338. WITH (
  339. OIDS=FALSE
  340. );
  341. ALTER TABLE "wafer_holder"
  342. OWNER TO postgres;
  343. GRANT SELECT ON TABLE "wafer_holder" TO postgres;
  344. end if;
  345. if not exists(select * from information_schema.columns
  346. where table_catalog = CURRENT_CATALOG and table_schema = CURRENT_SCHEMA and table_name = 'wafer_holder' and column_name = 'total_uses') then
  347. Alter TABLE "wafer_holder" ADD column "total_uses" INTEGER default 0;
  348. end if;
  349. ------------------------------------------------------------------------------------------------
  350. if not exists(select * from information_schema.tables
  351. where
  352. table_catalog = CURRENT_CATALOG and table_schema = CURRENT_SCHEMA
  353. and table_name = 'metal_usage') then
  354. CREATE TABLE "metal_usage"
  355. (
  356. "metal_name" text,
  357. "total_usage" real,
  358. "anode_a_usage" real,
  359. "anode_b_usage" real,
  360. "membrance_a_usage" real,
  361. "membrance_b_usage" real,
  362. "total_wafers" integer,
  363. "anode_a_wafers" integer,
  364. "anode_b_wafers" integer,
  365. "create_time" timestamp without time zone,
  366. "update_time" timestamp without time zone,
  367. CONSTRAINT "metal_usage_pkey" PRIMARY KEY ("metal_name" )
  368. )
  369. WITH (
  370. OIDS=FALSE
  371. );
  372. ALTER TABLE "metal_usage"
  373. OWNER TO postgres;
  374. GRANT SELECT ON TABLE "metal_usage" TO postgres;
  375. end if;
  376. if not exists(select * from information_schema.columns
  377. where table_catalog = CURRENT_CATALOG and table_schema = CURRENT_SCHEMA and table_name = 'metal_usage' and column_name = 'anode_a_bath_usage') then
  378. Alter TABLE "metal_usage" ADD column anode_a_bath_usage integer default 0;
  379. end if;
  380. if not exists(select * from information_schema.columns
  381. where table_catalog = CURRENT_CATALOG and table_schema = CURRENT_SCHEMA and table_name = 'metal_usage' and column_name = 'anode_b_bath_usage') then
  382. Alter TABLE "metal_usage" ADD column anode_b_bath_usage integer default 0;
  383. end if;
  384. if not exists(select * from information_schema.columns
  385. where table_catalog = CURRENT_CATALOG and table_schema = CURRENT_SCHEMA and table_name = 'metal_usage' and column_name = 'bath_reset_time') then
  386. Alter TABLE "metal_usage" ADD column bath_reset_time timestamp without time zone DEFAULT CURRENT_TIMESTAMP;
  387. end if;
  388. ------------------------------------------------------------------------------------------------
  389. if not exists(select * from information_schema.tables
  390. where
  391. table_catalog = CURRENT_CATALOG and table_schema = CURRENT_SCHEMA
  392. and table_name = 'reservoir_usage') then
  393. CREATE TABLE "reservoir_usage"
  394. (
  395. "reservoir_name" text,
  396. "total_usage" real,
  397. "bath_usage" real,
  398. "anode_usage" real,
  399. "membrance_usage" real,
  400. "total_wafers" integer,
  401. "bath_usage_days" integer,
  402. "bath_reset_time" timestamp without time zone,
  403. "create_time" timestamp without time zone,
  404. "update_time" timestamp without time zone,
  405. CONSTRAINT "reservoir_usage_pkey" PRIMARY KEY ("reservoir_name" )
  406. )
  407. WITH (
  408. OIDS=FALSE
  409. );
  410. ALTER TABLE "reservoir_usage"
  411. OWNER TO postgres;
  412. GRANT SELECT ON TABLE "reservoir_usage" TO postgres;
  413. end if;
  414. if not exists(select * from information_schema.columns
  415. where table_catalog = CURRENT_CATALOG and table_schema = CURRENT_SCHEMA and table_name = 'reservoir_usage' and column_name = 'cmm_membrance_usage') then
  416. Alter TABLE "reservoir_usage" ADD column cmm_membrance_usage REAL default 0;
  417. end if;
  418. if not exists(select * from information_schema.columns
  419. where table_catalog = CURRENT_CATALOG and table_schema = CURRENT_SCHEMA and table_name = 'reservoir_usage' and column_name = 'cmm_anode_usage') then
  420. Alter TABLE "reservoir_usage" ADD column cmm_anode_usage REAL default 0;
  421. end if;
  422. ------------------------------------------------------------------------------------------------
  423. if not exists(select * from information_schema.tables
  424. where
  425. table_catalog = CURRENT_CATALOG and table_schema = CURRENT_SCHEMA
  426. and table_name = 'wafer_holder_history') then
  427. CREATE TABLE "wafer_holder_history"
  428. (
  429. "gid" serial NOT NULL,
  430. "wafer_holder_guid" text,
  431. "operation" text,
  432. "operation_time" timestamp without time zone,
  433. "station" text,
  434. CONSTRAINT "wafer_holder_history_pkey" PRIMARY KEY ("gid" )
  435. )
  436. WITH (
  437. OIDS=FALSE
  438. );
  439. ALTER TABLE "wafer_holder_history"
  440. OWNER TO postgres;
  441. GRANT SELECT ON TABLE "wafer_holder_history" TO postgres;
  442. end if;
  443. ------------------------------------------------------------------------------------------------
  444. if not exists(select * from information_schema.tables
  445. where
  446. table_catalog = CURRENT_CATALOG and table_schema = CURRENT_SCHEMA
  447. and table_name = 'alarm_list') then
  448. CREATE TABLE "alarm_list"
  449. (
  450. "gid" serial NOT NULL,
  451. "module_name" text,
  452. "module_state" text,
  453. "module_step" integer,
  454. "module_cmd" integer,
  455. "alarm_msg" text,
  456. "data_item" text,
  457. "alarm_type" integer,
  458. "create_time" timestamp without time zone,
  459. "update_time" timestamp without time zone,
  460. CONSTRAINT "alarm_list_pkey" PRIMARY KEY (gid )
  461. )
  462. WITH (
  463. OIDS=FALSE
  464. );
  465. ALTER TABLE "alarm_list"
  466. OWNER TO postgres;
  467. GRANT SELECT ON TABLE "alarm_list" TO postgres;
  468. end if;
  469. ------------------------------------------------------------------------------------------------
  470. if not exists(select * from information_schema.tables
  471. where
  472. table_catalog = CURRENT_CATALOG and table_schema = CURRENT_SCHEMA
  473. and table_name = 'alarm_list_history') then
  474. CREATE TABLE "alarm_list_history"
  475. (
  476. "gid" serial NOT NULL,
  477. "module_name" text,
  478. "module_state" text,
  479. "module_step" integer,
  480. "module_cmd" integer,
  481. "alarm_msg" text,
  482. "data_item" text,
  483. "alarm_type" integer,
  484. "alarm_operation" text,
  485. "create_time" timestamp without time zone,
  486. "update_time" timestamp without time zone,
  487. CONSTRAINT "alarm_list_history_pkey" PRIMARY KEY ("gid" )
  488. )
  489. WITH (
  490. OIDS=FALSE
  491. );
  492. ALTER TABLE "alarm_list_history"
  493. OWNER TO postgres;
  494. GRANT SELECT ON TABLE "alarm_list_history" TO postgres;
  495. end if;
  496. ------------------------------------------------------------------------------------------------
  497. if not exists(select * from information_schema.tables
  498. where
  499. table_catalog = CURRENT_CATALOG and table_schema = CURRENT_SCHEMA
  500. and table_name = 'user') then
  501. CREATE TABLE "user"
  502. (
  503. "No" serial PRIMARY KEY,
  504. "UserName" VARCHAR(100),
  505. "Role" VARCHAR(100),
  506. "Password" VARCHAR(100),
  507. "Notes" text
  508. )
  509. WITH (
  510. OIDS=FALSE
  511. );
  512. ALTER TABLE "user"
  513. OWNER TO postgres;
  514. GRANT SELECT ON TABLE "user" TO postgres;
  515. end if;
  516. -------------------------------------------------------------------------------------------------
  517. end;
  518. $$
  519. language 'plpgsql';
  520. select update_db_model();
  521. CREATE OR REPLACE FUNCTION batch_delete_tables(text)
  522. RETURNS int AS
  523. $$
  524. DECLARE
  525. r RECORD;
  526. count int;
  527. BEGIN
  528. count := 0;
  529. FOR r IN SELECT tablename FROM pg_tables where tablename like $1 || '%' LOOP
  530. RAISE NOTICE 'tablename: %', r.tablename;
  531. EXECUTE 'DROP TABLE "' || r.tablename || '" CASCADE';
  532. count := count + 1;
  533. END LOOP;
  534. RETURN count;
  535. END;
  536. $$
  537. LANGUAGE 'plpgsql' VOLATILE;