DBModel.sql 18 KB

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