summaryrefslogtreecommitdiff
path: root/sql/Pg-upgrade-2.6.17-2.6.18.sql
blob: 8144f1df406812d9ca17451a7ef24e404ac7c637 (plain)
  1. -- linuxpoet:
  2. -- adding primary key to acc_trans
  3. -- We are using standard postgresql names for the sequence for consistency as we move forward
  4. -- Do everything in a transaction in case it blows up
  5. LOCK acc_trans in EXCLUSIVE mode;
  6. ALTER TABLE acc_trans ADD COLUMN entry_id bigint;
  7. CREATE SEQUENCE acctrans_entry_id_seq;
  8. ALTER TABLE acc_trans ALTER COLUMN entry_id SET DEFAULT nextval('acctrans_entry_id_seq');
  9. UPDATE acc_trans SET entry_id = nextval('acctrans_entry_id_seq');
  10. ALTER TABLE acc_trans ADD PRIMARY key (entry_id);
  11. -- We should probably add a foreign key to chart.id
  12. ALTER TABLE acc_trans ADD FOREIGN KEY (chart_id) REFERENCES chart (id);
  13. -- Start changing floats
  14. ALTER TABLE acc_trans ALTER COLUMN amount TYPE NUMERIC;
  15. -- This may break someone if they for some reason have an actual float type in the qty column
  16. ALTER TABLE invoice ALTER COLUMN qty TYPE numeric;
  17. ALTER TABLE invoice ALTER COLUMN allocated TYPE numeric;
  18. ALTER TABLE invoice ALTER COLUMN sellprice TYPE NUMERIC;
  19. ALTER TABLE invoice ALTER COLUMN fxsellprice TYPE NUMERIC;
  20. ALTER TABLE customer ALTER COLUMN discount TYPE numeric;
  21. ALTER TABLE customer ALTER COLUMN creditlimit TYPE NUMERIC;
  22. ALTER TABLE parts ALTER COLUMN listprice TYPE NUMERIC;
  23. ALTER TABLE parts ALTER COLUMN sellprice TYPE NUMERIC;
  24. ALTER TABLE parts ALTER COLUMN lastcost TYPE NUMERIC;
  25. ALTER TABLE parts ALTER COLUMN weight TYPE numeric;
  26. ALTER TABLE parts ALTER COLUMN onhand TYPE numeric;
  27. ALTER TABLE parts ALTER COLUMN avgcost TYPE NUMERIC;
  28. ALTER TABLE assembly ALTER COLUMN qty TYPE numeric;
  29. ALTER TABLE ar ALTER COLUMN amount TYPE NUMERIC;
  30. ALTER TABLE ar ALTER COLUMN netamount TYPE NUMERIC;
  31. ALTER TABLE ar ALTER COLUMN paid TYPE NUMERIC;
  32. ALTER TABLE ap ALTER COLUMN amount TYPE NUMERIC;
  33. ALTER TABLE ap ALTER COLUMN netamount TYPE NUMERIC;
  34. ALTER TABLE ap ALTER COLUMN paid TYPE NUMERIC;
  35. ALTER TABLE tax ALTER COLUMN rate TYPE numeric;
  36. ALTER TABLE oe ALTER COLUMN amount TYPE NUMERIC;
  37. ALTER TABLE oe ALTER COLUMN netamount TYPE NUMERIC;
  38. ALTER TABLE orderitems ALTER COLUMN qty TYPE numeric;
  39. ALTER TABLE orderitems ALTER COLUMN sellprice TYPE NUMERIC;
  40. ALTER TABLE orderitems ALTER COLUMN discount TYPE numeric;
  41. ALTER TABLE orderitems ALTER COLUMN ship TYPE numeric;
  42. ALTER TABLE exchangerate ALTER COLUMN buy TYPE numeric;
  43. ALTER TABLE exchangerate ALTER COLUMN sell TYPE numeric;
  44. ALTER TABLE vendor ALTER COLUMN discount TYPE numeric;
  45. ALTER TABLE vendor ALTER COLUMN creditlimit TYPE numeric;
  46. ALTER TABLE project ALTER COLUMN production TYPE numeric;
  47. ALTER TABLE project ALTER COLUMN completed TYPE numeric;
  48. ALTER TABLE business ALTER COLUMN discount TYPE numeric;
  49. ALTER TABLE inventory ALTER COLUMN qty TYPE numeric;
  50. ALTER TABLE partsvendor ALTER COLUMN lastcost TYPE NUMERIC;
  51. ALTER TABLE partscustomer ALTER COLUMN pricebreak TYPE numeric;
  52. ALTER TABLE partscustomer ALTER COLUMN sellprice TYPE NUMERIC;
  53. ALTER TABLE jcitems ALTER COLUMN qty TYPE numeric;
  54. ALTER TABLE jcitems ALTER COLUMN allocated TYPE numeric;
  55. ALTER TABLE jcitems ALTER COLUMN sellprice TYPE NUMERIC;
  56. ALTER TABLE jcitems ALTER COLUMN fxsellprice TYPE NUMERIC;
  57. -- The query rewrite rule necessary to notify the email app that a new report
  58. -- needs to be sent to the designated administrator.
  59. -- By Chris Travers
  60. -- chris@metatrontech.com
  61. -- Licensed under the GNU GPL 2.0 or later at your option. See accompanying
  62. -- GPL.txt
  63. CREATE OR REPLACE FUNCTION trigger_parts_short() RETURNS TRIGGER
  64. AS
  65. '
  66. BEGIN
  67. IF NEW.onhand >= NEW.rop THEN
  68. NOTIFY parts_short;
  69. END IF;
  70. RETURN NEW;
  71. END;
  72. ' LANGUAGE PLPGSQL;
  73. CREATE TRIGGER parts_short AFTER UPDATE ON parts
  74. FOR EACH ROW EXECUTE PROCEDURE trigger_parts_short();
  75. create table transaction_ledger (
  76. id int PRIMARY KEY,
  77. table_name text
  78. );
  79. insert into transaction_ledger (id, table_name) SELECT id, 'ap' FROM ap;
  80. CREATE RULE ap_id_track_i AS ON insert TO ap
  81. DO ALSO INSERT INTO transaction_ledger (id, table_name) VALUES (new.id, 'ap');
  82. CREATE RULE ap_id_track_u AS ON update TO ap
  83. DO ALSO UPDATE transaction_ledger SET id = new.id WHERE id = old.id;
  84. insert into transaction_ledger (id, table_name) SELECT id, 'ar' FROM ap;
  85. CREATE RULE ar_id_track_i AS ON insert TO ar
  86. DO ALSO INSERT INTO transaction_ledger (id, table_name) VALUES (new.id, 'ar');
  87. CREATE RULE ar_id_track_u AS ON update TO ar
  88. DO ALSO UPDATE transaction_ledger SET id = new.id WHERE id = old.id;
  89. INSERT INTO transaction_ledger (id, table_name) SELECT id, 'business' FROM business;
  90. CREATE RULE business_id_track_i AS ON insert TO business
  91. DO ALSO INSERT INTO transaction_ledger (id, table_name) VALUES (new.id, 'business');
  92. CREATE RULE business_id_track_u AS ON update TO business
  93. DO ALSO UPDATE transaction_ledger SET id = new.id WHERE id = old.id;
  94. INSERT INTO transaction_ledger (id, table_name) SELECT id, 'chart' FROM chart;
  95. CREATE RULE chart_id_track_i AS ON insert TO chart
  96. DO ALSO INSERT INTO transaction_ledger (id, table_name) VALUES (new.id, 'chart');
  97. CREATE RULE chart_id_track_u AS ON update TO chart
  98. DO ALSO UPDATE transaction_ledger SET id = new.id WHERE id = old.id;
  99. INSERT INTO transaction_ledger (id, table_name) SELECT id, 'customer' FROM customer;
  100. CREATE RULE customer_id_track_i AS ON insert TO customer
  101. DO ALSO INSERT INTO transaction_ledger (id, table_name) VALUES (new.id, 'customer');
  102. CREATE RULE customer_id_track_u AS ON update TO customer
  103. DO ALSO UPDATE transaction_ledger SET id = new.id WHERE id = old.id;
  104. INSERT INTO transaction_ledger (id, table_name) SELECT id, 'department' FROM department;
  105. CREATE RULE department_id_track_i AS ON insert TO department
  106. DO ALSO INSERT INTO transaction_ledger (id, table_name) VALUES (new.id, 'department');
  107. CREATE RULE department_id_track_u AS ON update TO department
  108. DO ALSO UPDATE transaction_ledger SET id = new.id WHERE id = old.id;
  109. INSERT INTO transaction_ledger (id, table_name) SELECT id, 'employee' FROM employee;
  110. CREATE RULE employee_id_track_i AS ON insert TO employee
  111. DO ALSO INSERT INTO transaction_ledger (id, table_name) VALUES (new.id, 'employee');
  112. CREATE RULE employee_id_track_u AS ON update TO employee
  113. DO ALSO UPDATE transaction_ledger SET id = new.id WHERE id = old.id;
  114. INSERT INTO transaction_ledger (id, table_name) SELECT id, 'gl' FROM gl;
  115. CREATE RULE gl_id_track_i AS ON insert TO gl
  116. DO ALSO INSERT INTO transaction_ledger (id, table_name) VALUES (new.id, 'gl');
  117. CREATE RULE gl_id_track_u AS ON update TO gl
  118. DO ALSO UPDATE transaction_ledger SET id = new.id WHERE id = old.id;
  119. INSERT INTO transaction_ledger (id, table_name) SELECT id, 'oe' FROM oe;
  120. CREATE RULE oe_id_track_i AS ON insert TO oe
  121. DO ALSO INSERT INTO transaction_ledger (id, table_name) VALUES (new.id, 'oe');
  122. CREATE RULE oe_id_track_u AS ON update TO oe
  123. DO ALSO UPDATE transaction_ledger SET id = new.id WHERE id = old.id;
  124. INSERT INTO transaction_ledger (id, table_name) SELECT id, 'parts' FROM parts;
  125. CREATE RULE parts_id_track_i AS ON insert TO parts
  126. DO ALSO INSERT INTO transaction_ledger (id, table_name) VALUES (new.id, 'parts');
  127. CREATE RULE parts_id_track_u AS ON update TO parts
  128. DO ALSO UPDATE transaction_ledger SET id = new.id WHERE id = old.id;
  129. INSERT INTO transaction_ledger (id, table_name) SELECT id, 'partsgroup' FROM partsgroup;
  130. CREATE RULE partsgroup_id_track_i AS ON insert TO partsgroup
  131. DO ALSO INSERT INTO transaction_ledger (id, table_name) VALUES (new.id, 'partsgroup');
  132. CREATE RULE partsgroup_id_track_u AS ON update TO partsgroup
  133. DO ALSO UPDATE transaction_ledger SET id = new.id WHERE id = old.id;
  134. INSERT INTO transaction_ledger (id, table_name) SELECT id, 'pricegroup' FROM pricegroup;
  135. CREATE RULE pricegroup_id_track_i AS ON insert TO pricegroup
  136. DO ALSO INSERT INTO transaction_ledger (id, table_name) VALUES (new.id, 'pricegroup');
  137. CREATE RULE pricegroup_id_track_u AS ON update TO pricegroup
  138. DO ALSO UPDATE transaction_ledger SET id = new.id WHERE id = old.id;
  139. INSERT INTO transaction_ledger (id, table_name) SELECT id, 'project' FROM project;
  140. CREATE RULE project_id_track_i AS ON insert TO project
  141. DO ALSO INSERT INTO transaction_ledger (id, table_name) VALUES (new.id, 'project');
  142. CREATE RULE project_id_track_u AS ON update TO project
  143. DO ALSO UPDATE transaction_ledger SET id = new.id WHERE id = old.id;
  144. INSERT INTO transaction_ledger (id, table_name) SELECT id, 'vendor' FROM vendor;
  145. CREATE RULE vendor_id_track_i AS ON insert TO vendor
  146. DO ALSO INSERT INTO transaction_ledger (id, table_name) VALUES (new.id, 'vendor');
  147. CREATE RULE employee_id_track_u AS ON update TO vendor
  148. DO ALSO UPDATE transaction_ledger SET id = new.id WHERE id = old.id;
  149. INSERT INTO transaction_ledger (id, table_name) SELECT id, 'warehouse' FROM warehouse;
  150. CREATE RULE warehouse_id_track_i AS ON insert TO warehouse
  151. DO ALSO INSERT INTO transaction_ledger (id, table_name) VALUES (new.id, 'employee');
  152. CREATE RULE warehouse_id_track_u AS ON update TO warehouse
  153. DO ALSO UPDATE transaction_ledger SET id = new.id WHERE id = old.id;
  154. CREATE TABLE custom_table_catalog (
  155. table_id SERIAL PRIMARY KEY,
  156. extends TEXT,
  157. table_name TEXT
  158. );
  159. CREATE TABLE custom_field_catalog (
  160. field_id SERIAL PRIMARY KEY,
  161. table_id INT REFERENCES custom_table_catalog,
  162. field_name TEXT
  163. );
  164. CREATE OR REPLACE FUNCTION add_custom_field (VARCHAR, VARCHAR, VARCHAR)
  165. RETURNS BOOL AS
  166. '
  167. DECLARE
  168. table_name ALIAS FOR $1;
  169. new_field_name ALIAS FOR $2;
  170. field_datatype ALIAS FOR $3;
  171. BEGIN
  172. EXECUTE ''SELECT TABLE_ID FROM custom_table_catalog
  173. WHERE extends = '''''' || table_name || '''''' '';
  174. IF NOT FOUND THEN
  175. INSERT INTO custom_table_catalog (extends) VALUES (table_name);
  176. EXECUTE ''CREATE TABLE custom_''||table_name || '' ()'';
  177. END IF;
  178. EXECUTE ''INSERT INTO custom_field_catalog (field_name, table_id)
  179. VALUES ( '''''' || new_field_name ||'''''', (SELECT table_id FROM custom_table_catalog
  180. WHERE extends = ''''''|| table_name || ''''''))'';
  181. EXECUTE ''ALTER TABLE custom_''||table_name || '' ADD COLUMN ''
  182. || new_field_name || '' '' || field_datatype;
  183. RETURN TRUE;
  184. END;
  185. ' LANGUAGE PLPGSQL;
  186. CREATE OR REPLACE FUNCTION drop_custom_field (VARCHAR, VARCHAR)
  187. RETURNS BOOL AS
  188. '
  189. DECLARE
  190. table_name ALIAS FOR $1;
  191. custom_field_name ALIAS FOR $2;
  192. BEGIN
  193. DELETE FROM custom_field_catalog
  194. WHERE field_name = custom_field_name AND
  195. table_id = (SELECT table_id FROM custom_table_catalog
  196. WHERE extends = table_name);
  197. EXECUTE ''ALTER TABLE custom_'' || table_name ||
  198. '' DROP COLUMN '' || field_name;
  199. RETURN TRUE;
  200. END;
  201. ' LANGUAGE PLPGSQL;
  202. UPDATE defaults SET version = '2.6.18';