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