summaryrefslogtreecommitdiff
path: root/sql/Pg-upgrade-2.6.18-2.6.19.sql
blob: b2328844843997cc904b71f8fd5420ec8163e088 (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 defaults ADD PRIMARY KEY (version);
  9. ALTER TABLE department ADD PRIMARY KEY (id);
  10. ALTER TABLE dpt_trans ADD PRIMARY KEY (trans_id);
  11. ALTER TABLE employee ADD PRIMARY KEY (id);
  12. ALTER TABLE exchangerate ADD PRIMARY KEY (curr, transdate);
  13. ALTER TABLE gifi ADD PRIMARY KEY (accno);
  14. ALTER TABLE gl ADD PRIMARY KEY (id);
  15. ALTER TABLE invoice ADD PRIMARY KEY (id);
  16. ALTER TABLE jcitems ADD PRIMARY KEY (id);
  17. ALTER TABLE language ADD PRIMARY KEY (code);
  18. ALTER TABLE makemodel ADD PRIMARY KEY (parts_id);
  19. ALTER TABLE oe ADD PRIMARY KEY (id);
  20. ALTER TABLE orderitems ADD PRIMARY KEY (id);
  21. ALTER TABLE parts ADD PRIMARY KEY (id);
  22. ALTER TABLE partsgroup ADD PRIMARY KEY (id);
  23. ALTER TABLE partstax ADD PRIMARY KEY (parts_id, chart_id);
  24. ALTER TABLE pricegroup ADD PRIMARY KEY (id);
  25. ALTER TABLE project ADD PRIMARY KEY (id);
  26. ALTER TABLE recurringemail ADD PRIMARY KEY (id);
  27. ALTER TABLE recurring ADD PRIMARY KEY (id);
  28. ALTER TABLE recurringprint ADD PRIMARY KEY (id);
  29. ALTER TABLE sic ADD PRIMARY KEY (code);
  30. ALTER TABLE status ADD PRIMARY KEY (trans_id);
  31. ALTER TABLE tax ADD PRIMARY KEY (chart_id);
  32. ALTER TABLE tax ADD FOREIGN KEY (chart_id) REFERENCES chart (id);
  33. ALTER TABLE translation ADD PRIMARY KEY (trans_id, language_code);
  34. ALTER TABLE vendor ADD PRIMARY KEY (id);
  35. ALTER TABLE vendortax ADD PRIMARY KEY (vendor_id, chart_id);
  36. ALTER TABLE warehouse ADD PRIMARY KEY (id);
  37. ALTER TABLE yearend ADD PRIMARY KEY (trans_id);
  38. LOCK inventory in EXCLUSIVE mode;
  39. ALTER TABLE inventory ADD COLUMN entry_id bigint;
  40. CREATE SEQUENCE inventory_entry_id_seq;
  41. ALTER TABLE inventory ALTER COLUMN entry_id
  42. SET DEFAULT nextval('inventory_entry_id_seq');
  43. UPDATE inventory SET entry_id = nextval('inventory_entry_id_seq');
  44. ALTER TABLE inventory ADD PRIMARY key (entry_id);
  45. LOCK partscustomer IN EXCLUSIVE MODE;
  46. ALTER TABLE partscustomer ADD COLUMN entry_id int;
  47. CREATE SEQUENCE partscustomer_entry_id_seq;
  48. ALTER TABLE partscustomer ALTER COLUMN entry_id
  49. SET DEFAULT nextval('partscustomer_entry_id_seq');
  50. UPDATE partscustomer SET entry_id = nextval('partscustomer_entry_id_seq');
  51. ALTER TABLE partscustomer ADD PRIMARY KEY (entry_id);
  52. LOCK partsvendor IN EXCLUSIVE MODE;
  53. ALTER TABLE partsvendor ADD COLUMN entry_id int;
  54. CREATE SEQUENCE partsvendor_entry_id_seq;
  55. ALTER TABLE partsvendor ALTER COLUMN entry_id
  56. SET DEFAULT nextval('partsvendor_entry_id_seq');
  57. UPDATE partsvendor SET entry_id = nextval('partsvendor_entry_id_seq');
  58. ALTER TABLE partsvendor ADD PRIMARY KEY (entry_id);
  59. LOCK audittrail IN EXCLUSIVE MODE;
  60. ALTER TABLE audittrail ADD COLUMN entry_id int;
  61. CREATE SEQUENCE audittrail_entry_id_seq ;
  62. ALTER TABLE audittrail ALTER COLUMN entry_id
  63. SET DEFAULT nextval('audittrail_entry_id_seq');
  64. UPDATE audittrail SET entry_id = nextval('audittrail_entry_id_seq');
  65. ALTER TABLE audittrail ADD PRIMARY KEY (entry_id);
  66. LOCK shipto IN EXCLUSIVE MODE;
  67. ALTER TABLE shipto ADD COLUMN entry_id int;
  68. CREATE SEQUENCE shipto_entry_id_seq ;
  69. ALTER TABLE shipto ALTER COLUMN entry_id
  70. SET DEFAULT nextval('shipto_entry_id_seq');
  71. UPDATE shipto SET entry_id = nextval('shipto_entry_id_seq');
  72. ALTER TABLE shipto ADD PRIMARY KEY (entry_id);
  73. CREATE TABLE taxmodule (
  74. taxmodule_id serial PRIMARY KEY,
  75. taxmodulename text NOT NULL
  76. );
  77. INSERT INTO taxmodule (
  78. taxmodule_id, taxmodulename
  79. ) VALUES (
  80. 1, 'Simple'
  81. );
  82. LOCK tax IN EXCLUSIVE MODE;
  83. ALTER TABLE tax ADD COLUMN pass int DEFAULT 0;
  84. UPDATE tax SET pass = 0;
  85. ALTER TABLE tax ALTER COLUMN pass SET NOT NULL;
  86. ALTER TABLE tax ADD COLUMN taxmodule_id int REFERENCES taxmodule DEFAULT 1;
  87. UPDATE tax SET taxmodule_id = 1;
  88. ALTER TABLE tax ALTER COLUMN taxmodule_id SET NOT NULL;
  89. -- Fixed session table and add users table --
  90. BEGIN;
  91. LOCK session in EXCLUSIVE MODE;
  92. ALTER TABLE session ADD CONSTRAINT session_token_check check (length(token::text) = 32);
  93. ALTER TABLE session ADD column user_id integer not null references users(id);
  94. LOCK users in EXCLUSIVE MODE;
  95. CREATE TABLE users (id serial UNIQUE, username varchar(30) PRIMARY KEY);
  96. COMMENT ON TABLE users 'username is the primary key because we don't want duplicate users';
  97. LOCK users_conf in EXCLUSIVE MODE;
  98. CREATE TABLE users_conf(id integer primary key references users(id) deferrable initially deferred,
  99. acs text,
  100. address text,
  101. businessnumber text,
  102. company text,
  103. countrycode text,
  104. currency text,
  105. dateformat text,
  106. dbconnect text,
  107. dbdriver text default 'Pg',
  108. dbhost text default 'localhost',
  109. dbname text,
  110. dboptions text,
  111. dbpasswd text,
  112. dbport text,
  113. dbuser text,
  114. email text,
  115. fax text,
  116. menuwidth text,
  117. name text,
  118. numberformat text,
  119. password varchar(32) check(length(password) = 32),
  120. print text,
  121. printer text,
  122. role text,
  123. sid text,
  124. signature text,
  125. stylesheet text,
  126. tel text,
  127. templates text,
  128. timeout numeric,
  129. vclimit numeric);
  130. COMMENT ON TABLE users_conf IS 'This is a completely dumb table that is a place holder to get usersconf into the database. Next major release will have a much more sane implementation';
  131. COMMENT ON COLUMN users_conf.id IS 'Yes primary key with a FOREIGN KEY to users(id) is correct';
  132. COMMENT ON COLUMN users_conf.password IS 'This means we have to get rid of the current password stuff and move to presumably md5()';
  133. COMMIT;
  134. -- Admin user --
  135. BEGIN;
  136. INSERT INTO users(username) VALUES ('admin');
  137. INSERT INTO users_conf(id,password) VALUES (currval('users_id_seq'),NULL);
  138. COMMIT;
  139. -- Functions
  140. CREATE FUNCTION create_user(text) RETURNS int4 AS $$
  141. INSERT INTO users(username) VALUES ('$1');
  142. SELECT currval('user_id_seq');
  143. $$ LANGUAGE 'SQL';
  144. COMMENT ON FUNCTION create_user(text) IS $$ Function to create user Returns users.id if successful, else it is an error. $$;
  145. CREATE FUNCTION update_user(int4,text) RETURNS int4 AS $$
  146. UPDATE users SET username = '$2' WHERE id = $1;
  147. SELECT 1;
  148. $$ LANGUAGE 'SQL';
  149. COMMENT ON FUNCTION update_user(int4,text) IS $$ Takes int4 which is users.id and text which is username. Will update username based on id. Username is unique $$;
  150. ALTER TABLE defaults RENAME TO old_defaults;
  151. CREATE TABLE defaults (
  152. setting_key TEXT PRIMARY KEY,
  153. value TEXT
  154. );
  155. COMMENT ON TABLE defaults IS $$This table replaces the old one column per value system with a simple key => value table$$;
  156. INSERT INTO defaults (setting_key, value)
  157. SELECT 'inventory_accno_id', inventory_accno_id FROM old_defaults
  158. UNION
  159. SELECT 'income_accno_id', income_accno_id FROM old_defaults
  160. UNION
  161. SELECT 'expense_accno_id', expense_accno_id FROM old_defaults
  162. UNION
  163. SELECT 'fxloss_accno_id', fxloss_accno_id FROM old_defaults
  164. UNION
  165. SELECT 'fxgain_accno_id', fxgain_accno_id FROM old_defaults
  166. UNION
  167. SELECT 'sinumber', sinumber FROM old_defaults
  168. UNION
  169. SELECT 'sonumber', sonumber FROM old_defaults
  170. UNION
  171. SELECT 'yearend', yearend FROM old_defaults
  172. UNION
  173. SELECT 'weightunit', weightunit FROM old_defaults
  174. UNION
  175. SELECT 'businessnumber', businessnumber FROM old_defaults
  176. UNION
  177. SELECT 'version', '1.2.0'
  178. UNION
  179. SELECT 'curr', curr FROM old_defaults
  180. UNION
  181. SELECT 'closedto', closedto FROM old_defaults
  182. UNION
  183. SELECT 'revtrans', revtrans FROM old_defaults
  184. UNION
  185. SELECT 'ponumber', ponumber FROM old_defaults
  186. UNION
  187. SELECT 'sqnumber', sqnumber FROM old_defaults
  188. UNION
  189. SELECT 'rfqnumber', rfqnumber FROM old_defaults
  190. UNION
  191. SELECT 'audittrail', audittrail FROM old_defaults
  192. UNION
  193. SELECT 'vinumber', vinumber FROM old_defaults
  194. UNION
  195. SELECT 'employeenumber', employeenumber FROM old_defaults
  196. UNION
  197. SELECT 'partnumber', partnumber FROM old_defaults
  198. UNION
  199. SELECT 'customernumber', customernumber FROM old_defaults
  200. UNION
  201. SELECT 'vendornumber', vendornumber FROM old_defaults
  202. UNION
  203. SELECT 'glnumber', glnumber FROM old_defaults
  204. UNION
  205. SELECT 'projectnumber', projectnumber FROM old_defaults
  206. UNION
  207. SELECT 'appname', 'LedgerSMB';
  208. DROP TABLE old_defaults;
  209. COMMIT;