summaryrefslogtreecommitdiff
path: root/sql/Pg-upgrade-2.6.17-2.6.18.sql
blob: 0ed4e0da8a24001cfabcf98f0793d83d18e9a7d4 (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. BEGIN;
  6. LOCK acc_trans in EXCLUSIVE mode;
  7. ALTER TABLE acc_trans ADD COLUMN entry_id bigint;
  8. CREATE SEQUENCE acctrans_entry_id_seq;
  9. ALTER TABLE acc_trans ALTER COLUMN entry_id SET DEFAULT nextval('acctrans_entry_id_seq');
  10. UPDATE acc_trans SET entry_id = nextval('acctrans_entry_id_seq');
  11. ALTER TABLE acc_trans ADD PRIMARY key (entry_id);
  12. -- Start changing floats
  13. ALTER TABLE acc_trans ALTER COLUMN amount TYPE numeric(10,2);
  14. -- This may break someone if they for some reason have an actual float type in the qty column
  15. ALTER TABLE invoice ALTER COLUMN qty TYPE numeric;
  16. ALTER TABLE invoice ALTER COLUMN allocated TYPE numeric;
  17. ALTER TABLE invoice ALTER COLUMN sellprice TYPE numeric(10,2);
  18. ALTER TABLE invoice ALTER COLUMN fxsellprice TYPE numeric(10,2);
  19. ALTER TABLE customer ALTER COLUMN discount TYPE numeric;
  20. ALTER TABLE customer ALTER COLUMN creditlimit TYPE numeric(10,2);
  21. ALTER TABLE parts ALTER COLUMN listprice TYPE numeric(10,2);
  22. ALTER TABLE parts ALTER COLUMN sellprice TYPE numeric(10,2);
  23. ALTER TABLE parts ALTER COLUMN lastcost TYPE numeric(10,2);
  24. ALTER TABLE parts ALTER COLUMN weight TYPE numeric;
  25. ALTER TABLE parts ALTER COLUMN onhand TYPE numeric;
  26. ALTER TABLE parts ALTER COLUMN avgcost TYPE numeric(10,2);
  27. ALTER TABLE assembly ALTER COLUMN qty TYPE numeric;
  28. ALTER TABLE ar ALTER COLUMN amount TYPE numeric(10,2);
  29. ALTER TABLE ar ALTER COLUMN netamount TYPE numeric(10,2);
  30. ALTER TABLE ar ALTER COLUMN paid TYPE numeric(10,2);
  31. ALTER TABLE ap ALTER COLUMN amount TYPE numeric(10,2);
  32. ALTER TABLE ap ALTER COLUMN netamount TYPE numeric(10,2);
  33. ALTER TABLE ap ALTER COLUMN paid TYPE numeric(10,2);
  34. ALTER TABLE tax ALTER COLUMN rate TYPE numeric;
  35. ALTER TABLE oe ALTER COLUMN amount TYPE numeric(10,2);
  36. ALTER TABLE oe ALTER COLUMN netamount TYPE numeric(10,2);
  37. ALTER TABLE orderitems ALTER COLUMN qty TYPE numeric;
  38. ALTER TABLE orderitems ALTER COLUMN sellprice TYPE numeric(10,2);
  39. ALTER TABLE orderitems ALTER COLUMN discount TYPE numeric;
  40. ALTER TABLE orderitems ALTER COLUMN ship TYPE numeric;
  41. ALTER TABLE exchangerate ALTER COLUMN buy TYPE numeric;
  42. ALTER TABLE exchangerate ALTER COLUMN sell TYPE numeric;
  43. ALTER TABLE vendor ALTER COLUMN discount TYPE numeric;
  44. ALTER TABLE vendor ALTER COLUMN creditlimit TYPE numeric;
  45. ALTER TABLE project ALTER COLUMN production TYPE numeric;
  46. ALTER TABLE project ALTER COLUMN completed TYPE numeric;
  47. ALTER TABLE business ALTER COLUMN discount TYPE numeric;
  48. ALTER TABLE inventory ALTER COLUMN qty TYPE numeric;
  49. ALTER TABLE partsvendor ALTER COLUMN lastcost TYPE numeric(10,2);
  50. ALTER TABLE partscustomer ALTER COLUMN pricebreak TYPE numeric;
  51. ALTER TABLE partscustomer ALTER COLUMN sellprice TYPE numeric(10,2);
  52. ALTER TABLE jcitems ALTER COLUMN qty TYPE numeric;
  53. ALTER TABLE jcitems ALTER COLUMN allocated TYPE numeric;
  54. ALTER TABLE jcitems ALTER COLUMN sellprice TYPE numeric(10,2);
  55. ALTER TABLE jcitems ALTER COLUMN fxsellprice TYPE numeric(10,2);
  56. -- The query rewrite rule necessary to notify the email app that a new report
  57. -- needs to be sent to the designated administrator.
  58. -- By Chris Travers
  59. -- chris@metatrontech.com
  60. -- Licensed under the GNU GPL 2.0 or later at your option. See accompanying
  61. -- GPL.txt
  62. CREATE OR REPLACE FUNCTION trigger_parts_short() RETURNS TRIGGER
  63. AS
  64. '
  65. BEGIN
  66. IF NEW.onhand >= NEW.rop THEN
  67. NOTIFY parts_short;
  68. END IF;
  69. RETURN NEW;
  70. END;
  71. ' LANGUAGE PLPGSQL;
  72. CREATE TRIGGER parts_short AFTER UPDATE ON parts
  73. FOR EACH ROW EXECUTE PROCEDURE trigger_parts_short();
  74. create table id_tracker (
  75. id int PRIMARY KEY,
  76. table_name text
  77. );
  78. insert into id_tracker (id, table_name) SELECT id, 'ap' FROM ap;
  79. CREATE RULE ap_id_track_i AS ON insert TO ap
  80. DO ALSO INSERT INTO id_tracker (id, table_name) VALUES (new.id, 'ap');
  81. CREATE RULE ap_id_track_u AS ON update TO ap
  82. DO ALSO UPDATE id_tracker SET id = new.id WHERE id = old.id;
  83. insert into id_tracker (id, table_name) SELECT id, 'ar' FROM ap;
  84. CREATE RULE ar_id_track_i AS ON insert TO ar
  85. DO ALSO INSERT INTO id_tracker (id, table_name) VALUES (new.id, 'ar');
  86. CREATE RULE ar_id_track_u AS ON update TO ar
  87. DO ALSO UPDATE id_tracker SET id = new.id WHERE id = old.id;
  88. INSERT INTO id_tracker (id, table_name) SELECT id, 'business' FROM business;
  89. CREATE RULE business_id_track_i AS ON insert TO business
  90. DO ALSO INSERT INTO id_tracker (id, table_name) VALUES (new.id, 'business');
  91. CREATE RULE business_id_track_u AS ON update TO business
  92. DO ALSO UPDATE id_tracker SET id = new.id WHERE id = old.id;
  93. INSERT INTO id_tracker (id, table_name) SELECT id, 'chart' FROM chart;
  94. CREATE RULE chart_id_track_i AS ON insert TO chart
  95. DO ALSO INSERT INTO id_tracker (id, table_name) VALUES (new.id, 'chart');
  96. CREATE RULE chart_id_track_u AS ON update TO chart
  97. DO ALSO UPDATE id_tracker SET id = new.id WHERE id = old.id;
  98. INSERT INTO id_tracker (id, table_name) SELECT id, 'customer' FROM customer;
  99. CREATE RULE customer_id_track_i AS ON insert TO customer
  100. DO ALSO INSERT INTO id_tracker (id, table_name) VALUES (new.id, 'customer');
  101. CREATE RULE customer_id_track_u AS ON update TO customer
  102. DO ALSO UPDATE id_tracker SET id = new.id WHERE id = old.id;
  103. INSERT INTO id_tracker (id, table_name) SELECT id, 'department' FROM department;
  104. CREATE RULE department_id_track_i AS ON insert TO department
  105. DO ALSO INSERT INTO id_tracker (id, table_name) VALUES (new.id, 'department');
  106. CREATE RULE department_id_track_u AS ON update TO department
  107. DO ALSO UPDATE id_tracker SET id = new.id WHERE id = old.id;
  108. INSERT INTO id_tracker (id, table_name) SELECT id, 'employee' FROM employee;
  109. CREATE RULE employee_id_track_i AS ON insert TO employee
  110. DO ALSO INSERT INTO id_tracker (id, table_name) VALUES (new.id, 'employee');
  111. CREATE RULE employee_id_track_u AS ON update TO employee
  112. DO ALSO UPDATE id_tracker SET id = new.id WHERE id = old.id;
  113. INSERT INTO id_tracker (id, table_name) SELECT id, 'gl' FROM gl;
  114. CREATE RULE gl_id_track_i AS ON insert TO gl
  115. DO ALSO INSERT INTO id_tracker (id, table_name) VALUES (new.id, 'gl');
  116. CREATE RULE gl_id_track_u AS ON update TO gl
  117. DO ALSO UPDATE id_tracker SET id = new.id WHERE id = old.id;
  118. INSERT INTO id_tracker (id, table_name) SELECT id, 'oe' FROM oe;
  119. CREATE RULE oe_id_track_i AS ON insert TO oe
  120. DO ALSO INSERT INTO id_tracker (id, table_name) VALUES (new.id, 'oe');
  121. CREATE RULE oe_id_track_u AS ON update TO oe
  122. DO ALSO UPDATE id_tracker SET id = new.id WHERE id = old.id;
  123. INSERT INTO id_tracker (id, table_name) SELECT id, 'parts' FROM parts;
  124. CREATE RULE parts_id_track_i AS ON insert TO parts
  125. DO ALSO INSERT INTO id_tracker (id, table_name) VALUES (new.id, 'parts');
  126. CREATE RULE parts_id_track_u AS ON update TO parts
  127. DO ALSO UPDATE id_tracker SET id = new.id WHERE id = old.id;
  128. INSERT INTO id_tracker (id, table_name) SELECT id, 'partsgroup' FROM partsgroup;
  129. CREATE RULE partsgroup_id_track_i AS ON insert TO partsgroup
  130. DO ALSO INSERT INTO id_tracker (id, table_name) VALUES (new.id, 'partsgroup');
  131. CREATE RULE partsgroup_id_track_u AS ON update TO partsgroup
  132. DO ALSO UPDATE id_tracker SET id = new.id WHERE id = old.id;
  133. INSERT INTO id_tracker (id, table_name) SELECT id, 'pricegroup' FROM pricegroup;
  134. CREATE RULE pricegroup_id_track_i AS ON insert TO pricegroup
  135. DO ALSO INSERT INTO id_tracker (id, table_name) VALUES (new.id, 'pricegroup');
  136. CREATE RULE pricegroup_id_track_u AS ON update TO pricegroup
  137. DO ALSO UPDATE id_tracker SET id = new.id WHERE id = old.id;
  138. INSERT INTO id_tracker (id, table_name) SELECT id, 'project' FROM project;
  139. CREATE RULE project_id_track_i AS ON insert TO project
  140. DO ALSO INSERT INTO id_tracker (id, table_name) VALUES (new.id, 'project');
  141. CREATE RULE project_id_track_u AS ON update TO project
  142. DO ALSO UPDATE id_tracker SET id = new.id WHERE id = old.id;
  143. INSERT INTO id_tracker (id, table_name) SELECT id, 'vendor' FROM vendor;
  144. CREATE RULE vendor_id_track_i AS ON insert TO vendor
  145. DO ALSO INSERT INTO id_tracker (id, table_name) VALUES (new.id, 'vendor');
  146. CREATE RULE employee_id_track_u AS ON update TO vendor
  147. DO ALSO UPDATE id_tracker SET id = new.id WHERE id = old.id;
  148. INSERT INTO id_tracker (id, table_name) SELECT id, 'warehouse' FROM warehouse;
  149. CREATE RULE warehouse_id_track_i AS ON insert TO warehouse
  150. DO ALSO INSERT INTO id_tracker (id, table_name) VALUES (new.id, 'employee');
  151. CREATE RULE warehouse_id_track_u AS ON update TO warehouse
  152. DO ALSO UPDATE id_tracker SET id = new.id WHERE id = old.id;
  153. COMMIT;