summaryrefslogtreecommitdiff
path: root/sql/legacy/Pg-upgrade-2.6.18-2.6.19.sql
blob: 384cae2582261c58ac93430371b90b7d91281510 (plain)
  1. BEGIN;
  2. ALTER TABLE ap ADD PRIMARY KEY (id);
  3. ALTER TABLE ar ADD PRIMARY KEY (id);
  4. ALTER TABLE assembly ADD PRIMARY KEY (id, parts_id);
  5. ALTER TABLE business ADD PRIMARY KEY (id);
  6. ALTER TABLE customer ADD PRIMARY KEY (id);
  7. ALTER TABLE customertax ADD PRIMARY KEY (customer_id, chart_id);
  8. ALTER TABLE department ADD PRIMARY KEY (id);
  9. ALTER TABLE dpt_trans ADD PRIMARY KEY (trans_id);
  10. ALTER TABLE employee ADD PRIMARY KEY (id);
  11. ALTER TABLE exchangerate ADD PRIMARY KEY (curr, transdate);
  12. ALTER TABLE gifi ADD PRIMARY KEY (accno);
  13. ALTER TABLE gl ADD PRIMARY KEY (id);
  14. ALTER TABLE invoice ADD PRIMARY KEY (id);
  15. ALTER TABLE jcitems ADD PRIMARY KEY (id);
  16. ALTER TABLE language ADD PRIMARY KEY (code);
  17. ALTER TABLE makemodel ADD PRIMARY KEY (parts_id);
  18. ALTER TABLE oe ADD PRIMARY KEY (id);
  19. ALTER TABLE orderitems ADD PRIMARY KEY (id);
  20. ALTER TABLE parts ADD PRIMARY KEY (id);
  21. ALTER TABLE partsgroup ADD PRIMARY KEY (id);
  22. ALTER TABLE partstax ADD PRIMARY KEY (parts_id, chart_id);
  23. ALTER TABLE partstax ADD FOREIGN KEY (chart_id) REFERENCES chart (id);
  24. ALTER TABLE partstax ADD FOREIGN KEY (parts_id) REFERENCES parts (id);
  25. ALTER TABLE pricegroup ADD PRIMARY KEY (id);
  26. ALTER TABLE project ADD PRIMARY KEY (id);
  27. ALTER TABLE recurringemail ADD PRIMARY KEY (id);
  28. ALTER TABLE recurring ADD PRIMARY KEY (id);
  29. ALTER TABLE recurringprint ADD PRIMARY KEY (id);
  30. ALTER TABLE sic ADD PRIMARY KEY (code);
  31. ALTER TABLE status ADD PRIMARY KEY (trans_id);
  32. ALTER TABLE tax ADD PRIMARY KEY (chart_id);
  33. ALTER TABLE tax ADD FOREIGN KEY (chart_id) REFERENCES chart (id);
  34. ALTER TABLE translation ADD PRIMARY KEY (trans_id, language_code);
  35. ALTER TABLE vendor ADD PRIMARY KEY (id);
  36. ALTER TABLE vendortax ADD PRIMARY KEY (vendor_id, chart_id);
  37. ALTER TABLE warehouse ADD PRIMARY KEY (id);
  38. ALTER TABLE yearend ADD PRIMARY KEY (trans_id);
  39. LOCK inventory in EXCLUSIVE mode;
  40. ALTER TABLE inventory ADD COLUMN entry_id bigint;
  41. CREATE SEQUENCE inventory_entry_id_seq;
  42. ALTER TABLE inventory ALTER COLUMN entry_id
  43. SET DEFAULT nextval('inventory_entry_id_seq');
  44. UPDATE inventory SET entry_id = nextval('inventory_entry_id_seq');
  45. ALTER TABLE inventory ADD PRIMARY key (entry_id);
  46. LOCK partscustomer IN EXCLUSIVE MODE;
  47. ALTER TABLE partscustomer ADD COLUMN entry_id int;
  48. CREATE SEQUENCE partscustomer_entry_id_seq;
  49. ALTER TABLE partscustomer ALTER COLUMN entry_id
  50. SET DEFAULT nextval('partscustomer_entry_id_seq');
  51. UPDATE partscustomer SET entry_id = nextval('partscustomer_entry_id_seq');
  52. ALTER TABLE partscustomer ADD PRIMARY KEY (entry_id);
  53. LOCK partsvendor IN EXCLUSIVE MODE;
  54. ALTER TABLE partsvendor ADD COLUMN entry_id int;
  55. CREATE SEQUENCE partsvendor_entry_id_seq;
  56. ALTER TABLE partsvendor ALTER COLUMN entry_id
  57. SET DEFAULT nextval('partsvendor_entry_id_seq');
  58. UPDATE partsvendor SET entry_id = nextval('partsvendor_entry_id_seq');
  59. ALTER TABLE partsvendor ADD PRIMARY KEY (entry_id);
  60. LOCK audittrail IN EXCLUSIVE MODE;
  61. ALTER TABLE audittrail ADD COLUMN entry_id int;
  62. CREATE SEQUENCE audittrail_entry_id_seq ;
  63. ALTER TABLE audittrail ALTER COLUMN entry_id
  64. SET DEFAULT nextval('audittrail_entry_id_seq');
  65. UPDATE audittrail SET entry_id = nextval('audittrail_entry_id_seq');
  66. ALTER TABLE audittrail ADD PRIMARY KEY (entry_id);
  67. LOCK shipto IN EXCLUSIVE MODE;
  68. ALTER TABLE shipto ADD COLUMN entry_id int;
  69. CREATE SEQUENCE shipto_entry_id_seq ;
  70. ALTER TABLE shipto ALTER COLUMN entry_id
  71. SET DEFAULT nextval('shipto_entry_id_seq');
  72. UPDATE shipto SET entry_id = nextval('shipto_entry_id_seq');
  73. ALTER TABLE shipto ADD PRIMARY KEY (entry_id);
  74. CREATE TABLE taxmodule (
  75. taxmodule_id serial PRIMARY KEY,
  76. taxmodulename text NOT NULL
  77. );
  78. INSERT INTO taxmodule (
  79. taxmodule_id, taxmodulename
  80. ) VALUES (
  81. 1, 'Simple'
  82. );
  83. CREATE TABLE taxcategory (
  84. taxcategory_id serial PRIMARY KEY,
  85. taxcategoryname text NOT NULL,
  86. taxmodule_id int NOT NULL REFERENCES taxmodule (taxmodule_id)
  87. );
  88. ALTER TABLE partstax ADD COLUMN taxcategory_id int REFERENCES taxcategory (taxcategory_id);
  89. LOCK tax IN EXCLUSIVE MODE;
  90. ALTER TABLE tax ADD COLUMN pass int DEFAULT 0;
  91. UPDATE tax SET pass = 0;
  92. ALTER TABLE tax ALTER COLUMN pass SET NOT NULL;
  93. ALTER TABLE tax ADD COLUMN taxmodule_id int REFERENCES taxmodule DEFAULT 1;
  94. UPDATE tax SET taxmodule_id = 1;
  95. ALTER TABLE tax ALTER COLUMN taxmodule_id SET NOT NULL;
  96. ALTER TABLE defaults RENAME TO old_defaults;
  97. CREATE TABLE defaults (
  98. setting_key TEXT PRIMARY KEY,
  99. value TEXT
  100. );
  101. COMMENT ON TABLE defaults IS $$This table replaces the old one column per value system with a simple key => value table$$;
  102. INSERT INTO defaults (setting_key, value)
  103. SELECT 'inventory_accno_id', inventory_accno_id::text FROM old_defaults
  104. UNION
  105. SELECT 'income_accno_id', income_accno_id::text FROM old_defaults
  106. UNION
  107. SELECT 'expense_accno_id', expense_accno_id::text FROM old_defaults
  108. UNION
  109. SELECT 'fxloss_accno_id', fxloss_accno_id::text FROM old_defaults
  110. UNION
  111. SELECT 'fxgain_accno_id', fxgain_accno_id::text FROM old_defaults
  112. UNION
  113. SELECT 'sinumber', sinumber::text FROM old_defaults
  114. UNION
  115. SELECT 'sonumber', sonumber::text FROM old_defaults
  116. UNION
  117. SELECT 'yearend', yearend::text FROM old_defaults
  118. UNION
  119. SELECT 'weightunit', weightunit::text FROM old_defaults
  120. UNION
  121. SELECT 'businessnumber', businessnumber::text FROM old_defaults
  122. UNION
  123. SELECT 'version', '1.2.0'::text
  124. UNION
  125. SELECT 'curr', curr::text FROM old_defaults
  126. UNION
  127. SELECT 'closedto', closedto::text FROM old_defaults
  128. UNION
  129. SELECT 'revtrans', (CASE WHEN revtrans IS NULL THEN NULL
  130. WHEN revtrans THEN '1'
  131. ELSE '0' END) FROM old_defaults
  132. UNION
  133. SELECT 'ponumber', ponumber::text FROM old_defaults
  134. UNION
  135. SELECT 'sqnumber', sqnumber::text FROM old_defaults
  136. UNION
  137. SELECT 'rfqnumber', rfqnumber::text FROM old_defaults
  138. UNION
  139. SELECT 'audittrail', (CASE WHEN audittrail IS NULL THEN NULL
  140. WHEN audittrail THEN '1'
  141. ELSE '0' END) FROM old_defaults
  142. UNION
  143. SELECT 'vinumber', vinumber::text FROM old_defaults
  144. UNION
  145. SELECT 'employeenumber', employeenumber::text FROM old_defaults
  146. UNION
  147. SELECT 'partnumber', partnumber::text FROM old_defaults
  148. UNION
  149. SELECT 'customernumber', customernumber::text FROM old_defaults
  150. UNION
  151. SELECT 'vendornumber', vendornumber::text FROM old_defaults
  152. UNION
  153. SELECT 'glnumber', glnumber::text FROM old_defaults
  154. UNION
  155. SELECT 'projectnumber', projectnumber::text FROM old_defaults
  156. UNION
  157. SELECT 'appname', 'LedgerSMB'::text;
  158. DROP TABLE old_defaults;
  159. CREATE OR REPLACE FUNCTION del_exchangerate() RETURNS TRIGGER AS '
  160. declare
  161. t_transdate date;
  162. t_curr char(3);
  163. t_id int;
  164. d_curr text;
  165. begin
  166. select into d_curr substr(value,1,3) from defaults where setting_key = ''curr'';
  167. if TG_RELNAME = ''ar'' then
  168. select into t_curr, t_transdate curr, transdate from ar where id = old.id;
  169. end if;
  170. if TG_RELNAME = ''ap'' then
  171. select into t_curr, t_transdate curr, transdate from ap where id = old.id;
  172. end if;
  173. if TG_RELNAME = ''oe'' then
  174. select into t_curr, t_transdate curr, transdate from oe where id = old.id;
  175. end if;
  176. if d_curr != t_curr then
  177. select into t_id a.id from acc_trans ac
  178. join ar a on (a.id = ac.trans_id)
  179. where a.curr = t_curr
  180. and ac.transdate = t_transdate
  181. except select a.id from ar a where a.id = old.id
  182. union
  183. select a.id from acc_trans ac
  184. join ap a on (a.id = ac.trans_id)
  185. where a.curr = t_curr
  186. and ac.transdate = t_transdate
  187. except select a.id from ap a where a.id = old.id
  188. union
  189. select o.id from oe o
  190. where o.curr = t_curr
  191. and o.transdate = t_transdate
  192. except select o.id from oe o where o.id = old.id;
  193. if not found then
  194. delete from exchangerate where curr = t_curr and transdate = t_transdate;
  195. end if;
  196. end if;
  197. return old;
  198. end;
  199. ' language 'plpgsql';
  200. CREATE OR REPLACE FUNCTION add_custom_field (VARCHAR, VARCHAR, VARCHAR)
  201. RETURNS BOOL AS
  202. 'BEGIN
  203. EXECUTE ''SELECT TABLE_ID FROM custom_table_catalog
  204. WHERE extends = '''''' || table_name || '''''' '';
  205. IF NOT FOUND THEN
  206. BEGIN
  207. INSERT INTO custom_table_catalog (extends)
  208. VALUES (table_name);
  209. EXECUTE ''CREATE TABLE custom_''||table_name ||
  210. '' (row_id INT PRIMARY KEY)'';
  211. EXCEPTION WHEN duplicate_table THEN
  212. -- do nothing
  213. END;
  214. END IF;
  215. EXECUTE ''INSERT INTO custom_field_catalog (field_name, table_id)
  216. VALUES ( '''''' || new_field_name ||'''''', (SELECT table_id FROM custom_table_catalog
  217. WHERE extends = ''''''|| table_name || ''''''))'';
  218. EXECUTE ''ALTER TABLE custom_''||table_name || '' ADD COLUMN ''
  219. || new_field_name || '' '' || field_datatype;
  220. RETURN TRUE;
  221. END;
  222. ' LANGUAGE PLPGSQL;
  223. COMMIT;