DBModel.sql 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. CONSTRAINT "wafer_data_pkey" PRIMARY KEY ("guid" )
  51. )
  52. WITH (
  53. OIDS=FALSE
  54. );
  55. ALTER TABLE "wafer_data"
  56. OWNER TO postgres;
  57. GRANT SELECT ON TABLE "wafer_data" TO postgres;
  58. end if;
  59. ------------------------------------------------------------------------------------------------
  60. --
  61. if not exists(select * from information_schema.tables
  62. where
  63. table_catalog = CURRENT_CATALOG and table_schema = CURRENT_SCHEMA
  64. and table_name = 'event_data') then
  65. CREATE TABLE "event_data"
  66. (
  67. "gid" serial NOT NULL,
  68. "event_id" integer,
  69. "event_enum" text,
  70. "type" text,
  71. "source" text,
  72. "description" text,
  73. "level" text,
  74. "occur_time" timestamp without time zone,
  75. CONSTRAINT "event_data_pkey" PRIMARY KEY ("gid" )
  76. )
  77. WITH (
  78. OIDS=FALSE
  79. );
  80. --ALTER TABLE "EventManager"
  81. --OWNER TO postgres;
  82. GRANT ALL ON TABLE "event_data" TO postgres;
  83. GRANT SELECT ON TABLE "event_data" TO postgres;
  84. CREATE INDEX "event_data_occur_time_event_id_idx"
  85. ON "event_data"
  86. USING btree
  87. ("occur_time" , "event_id" );
  88. end if;
  89. ------------------------------------------------------------------------------------------------
  90. --
  91. if not exists(select * from information_schema.tables
  92. where
  93. table_catalog = CURRENT_CATALOG and table_schema = CURRENT_SCHEMA
  94. and table_name = 'wafer_move_history') then
  95. CREATE TABLE "wafer_move_history"
  96. (
  97. "gid" serial NOT NULL,
  98. "wafer_data_guid" text,
  99. "arrive_time" timestamp without time zone,
  100. "station" text,
  101. "slot" text,
  102. "status" text,
  103. CONSTRAINT "wafer_move_history_pkey" PRIMARY KEY ("gid" )
  104. )
  105. WITH (
  106. OIDS=FALSE
  107. );
  108. ALTER TABLE "wafer_move_history"
  109. OWNER TO postgres;
  110. GRANT SELECT ON TABLE "wafer_move_history" TO postgres;
  111. end if;
  112. ------------------------------------------------------------------------------------------------
  113. --
  114. if not exists(select * from information_schema.tables
  115. where
  116. table_catalog = CURRENT_CATALOG and table_schema = CURRENT_SCHEMA
  117. and table_name = 'process_data') then
  118. CREATE TABLE "process_data"
  119. (
  120. "guid" text NOT NULL,
  121. "process_begin_time" timestamp without time zone,
  122. "process_end_time" timestamp without time zone,
  123. "recipe_name" text,
  124. "process_status" text,
  125. "wafer_data_guid" text,
  126. "process_in" text,
  127. CONSTRAINT "process_data_pkey" PRIMARY KEY ("guid" )
  128. )
  129. WITH (
  130. OIDS=FALSE
  131. );
  132. ALTER TABLE "process_data"
  133. OWNER TO postgres;
  134. GRANT SELECT ON TABLE "process_data" TO postgres;
  135. end if;
  136. ------------------------------------------------------------------------------------------------
  137. --
  138. if not exists(select * from information_schema.tables
  139. where
  140. table_catalog = CURRENT_CATALOG and table_schema = CURRENT_SCHEMA
  141. and table_name = 'stats_data') then
  142. CREATE TABLE "stats_data"
  143. (
  144. "name" text,
  145. "value" integer,
  146. "total" integer,
  147. "description" text,
  148. "last_update_time" timestamp without time zone,
  149. "last_reset_time" timestamp without time zone,
  150. "last_total_reset_time" timestamp without time zone,
  151. CONSTRAINT "stats_data_pkey" PRIMARY KEY ("name" )
  152. )
  153. WITH (
  154. OIDS=FALSE
  155. );
  156. ALTER TABLE "stats_data"
  157. OWNER TO postgres;
  158. GRANT SELECT ON TABLE "stats_data" TO postgres;
  159. end if;
  160. ------------------------------------------------------------------------------------------------
  161. --
  162. end;
  163. $$
  164. language 'plpgsql';
  165. select update_db_model();
  166. CREATE OR REPLACE FUNCTION batch_delete_tables(text)
  167. RETURNS int AS
  168. $$
  169. DECLARE
  170. r RECORD;
  171. count int;
  172. BEGIN
  173. count := 0;
  174. FOR r IN SELECT tablename FROM pg_tables where tablename like $1 || '%' LOOP
  175. RAISE NOTICE 'tablename: %', r.tablename;
  176. EXECUTE 'DROP TABLE "' || r.tablename || '" CASCADE';
  177. count := count + 1;
  178. END LOOP;
  179. RETURN count;
  180. END;
  181. $$
  182. LANGUAGE 'plpgsql' VOLATILE;