summaryrefslogtreecommitdiff
path: root/sql/legacy/Pg-upgrade-2.6.18-2.6.19.sql
blob: 90e7e313369f5248e0dbefb2352c5c67059fd218 (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. -- comment this out when user db is working:
  95. ALTER TABLE session ALTER COLUMN user_id DROP NOT NULL;
  96. LOCK users in EXCLUSIVE MODE;
  97. CREATE TABLE users (id serial UNIQUE, username varchar(30) PRIMARY KEY);
  98. COMMENT ON TABLE users 'username is the primary key because we don\'t want duplicate users';
  99. LOCK users_conf in EXCLUSIVE MODE;
  100. CREATE TABLE users_conf(id integer primary key references users(id) deferrable initially deferred,
  101. acs text,
  102. address text,
  103. businessnumber text,
  104. company text,
  105. countrycode text,
  106. currency text,
  107. dateformat text,
  108. dbconnect text,
  109. dbdriver text default 'Pg',
  110. dbhost text default 'localhost',
  111. dbname text,
  112. dboptions text,
  113. dbpasswd text,
  114. dbport text,
  115. dbuser text,
  116. email text,
  117. fax text,
  118. menuwidth text,
  119. name text,
  120. numberformat text,
  121. password varchar(32) check(length(password) = 32),
  122. print text,
  123. printer text,
  124. role text,
  125. sid text,
  126. signature text,
  127. stylesheet text,
  128. tel text,
  129. templates text,
  130. timeout numeric,
  131. vclimit numeric);
  132. 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';
  133. COMMENT ON COLUMN users_conf.id IS 'Yes primary key with a FOREIGN KEY to users(id) is correct';
  134. COMMENT ON COLUMN users_conf.password IS 'This means we have to get rid of the current password stuff and move to presumably md5()';
  135. COMMIT;
  136. -- Admin user --
  137. BEGIN;
  138. INSERT INTO users(username) VALUES ('admin');
  139. INSERT INTO users_conf(id,password) VALUES (currval('users_id_seq'),NULL);
  140. COMMIT;
  141. -- Functions
  142. CREATE FUNCTION create_user(text) RETURNS int4 AS $$
  143. INSERT INTO users(username) VALUES ('$1');
  144. SELECT currval('user_id_seq');
  145. $$ LANGUAGE 'SQL';
  146. COMMENT ON FUNCTION create_user(text) IS $$ Function to create user Returns users.id if successful, else it is an error. $$;
  147. CREATE FUNCTION update_user(int4,text) RETURNS int4 AS $$
  148. UPDATE users SET username = '$2' WHERE id = $1;
  149. SELECT 1;
  150. $$ LANGUAGE 'SQL';
  151. 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 $$;
  152. ALTER TABLE defaults RENAME TO old_defaults;
  153. CREATE TABLE defaults (
  154. setting_key TEXT PRIMARY KEY,
  155. value TEXT
  156. );
  157. COMMENT ON TABLE defaults IS $$This table replaces the old one column per value system with a simple key => value table$$;
  158. INSERT INTO defaults (setting_key, value)
  159. SELECT 'inventory_accno_id', inventory_accno_id FROM old_defaults
  160. UNION
  161. SELECT 'income_accno_id', income_accno_id FROM old_defaults
  162. UNION
  163. SELECT 'expense_accno_id', expense_accno_id FROM old_defaults
  164. UNION
  165. SELECT 'fxloss_accno_id', fxloss_accno_id FROM old_defaults
  166. UNION
  167. SELECT 'fxgain_accno_id', fxgain_accno_id FROM old_defaults
  168. UNION
  169. SELECT 'sinumber', sinumber FROM old_defaults
  170. UNION
  171. SELECT 'sonumber', sonumber FROM old_defaults
  172. UNION
  173. SELECT 'yearend', yearend FROM old_defaults
  174. UNION
  175. SELECT 'weightunit', weightunit FROM old_defaults
  176. UNION
  177. SELECT 'businessnumber', businessnumber FROM old_defaults
  178. UNION
  179. SELECT 'version', '1.2.0'
  180. UNION
  181. SELECT 'curr', curr FROM old_defaults
  182. UNION
  183. SELECT 'closedto', closedto FROM old_defaults
  184. UNION
  185. SELECT 'revtrans', revtrans FROM old_defaults
  186. UNION
  187. SELECT 'ponumber', ponumber FROM old_defaults
  188. UNION
  189. SELECT 'sqnumber', sqnumber FROM old_defaults
  190. UNION
  191. SELECT 'rfqnumber', rfqnumber FROM old_defaults
  192. UNION
  193. SELECT 'audittrail', audittrail FROM old_defaults
  194. UNION
  195. SELECT 'vinumber', vinumber FROM old_defaults
  196. UNION
  197. SELECT 'employeenumber', employeenumber FROM old_defaults
  198. UNION
  199. SELECT 'partnumber', partnumber FROM old_defaults
  200. UNION
  201. SELECT 'customernumber', customernumber FROM old_defaults
  202. UNION
  203. SELECT 'vendornumber', vendornumber FROM old_defaults
  204. UNION
  205. SELECT 'glnumber', glnumber FROM old_defaults
  206. UNION
  207. SELECT 'projectnumber', projectnumber FROM old_defaults
  208. UNION
  209. SELECT 'appname', 'LedgerSMB';
  210. DROP TABLE old_defaults;
  211. CREATE OR REPLACE FUNCTION add_custom_field (VARCHAR, VARCHAR, VARCHAR)
  212. RETURNS BOOL AS
  213. 'BEGIN
  214. EXECUTE ''SELECT TABLE_ID FROM custom_table_catalog
  215. WHERE extends = '''''' || table_name || '''''' '';
  216. IF NOT FOUND THEN
  217. BEGIN
  218. INSERT INTO custom_table_catalog (extends)
  219. VALUES (table_name);
  220. EXECUTE ''CREATE TABLE custom_''||table_name ||
  221. '' (row_id INT PRIMARY KEY)'';
  222. EXCEPTION WHEN duplicate_table THEN
  223. -- do nothing
  224. END;
  225. END IF;
  226. EXECUTE ''INSERT INTO custom_field_catalog (field_name, table_id)
  227. VALUES ( '''''' || new_field_name ||'''''', (SELECT table_id FROM custom_table_catalog
  228. WHERE extends = ''''''|| table_name || ''''''))'';
  229. EXECUTE ''ALTER TABLE custom_''||table_name || '' ADD COLUMN ''
  230. || new_field_name || '' '' || field_datatype;
  231. RETURN TRUE;
  232. ' LANGUAGE PLPGSQL;
  233. END;
  234. COMMIT;