summaryrefslogtreecommitdiff
path: root/sql/legacy/Pg-upgrade-2.6.18-2.6.19.sql
blob: 32e6dc5d44c6db0cc11ff668b84fb3944dc5a08c (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. ALTER TABLE orderitems ADD PRIMARY KEY (id);
  20. ALTER TABLE parts ADD PRIMARY KEY (id);
  21. ALTER TABLE partsgroup ADD PRIMARY KEY (id);
  22. ALTER TABLE partstax ADD PRIMARY KEY (parts_id, chart_id);
  23. ALTER TABLE partstax ADD FOREIGN KEY (chart_id) REFERENCES chart (id);
  24. ALTER TABLE partstax ADD FOREIGN KEY (parts_id) REFERENCES parts (id);
  25. ALTER TABLE pricegroup ADD PRIMARY KEY (id);
  26. ALTER TABLE project ADD PRIMARY KEY (id);
  27. ALTER TABLE recurringemail ADD PRIMARY KEY (id);
  28. ALTER TABLE recurring ADD PRIMARY KEY (id);
  29. ALTER TABLE recurringprint ADD PRIMARY KEY (id);
  30. ALTER TABLE sic ADD PRIMARY KEY (code);
  31. ALTER TABLE status ADD PRIMARY KEY (trans_id);
  32. -- Removing the primary key below since this is not quite the best way
  33. -- do this. The tax table could have multiple rows per chart_id.
  34. -- We need a better fix for 1.3 (perhaps changing date to timestamp and
  35. -- and defaulting to infinity.
  36. -- ALTER TABLE tax ADD PRIMARY KEY (chart_id);
  37. -- ALTER TABLE tax ADD FOREIGN KEY (chart_id) REFERENCES chart (id);
  38. ALTER TABLE translation ADD PRIMARY KEY (trans_id, language_code);
  39. ALTER TABLE vendor ADD PRIMARY KEY (id);
  40. ALTER TABLE vendortax ADD PRIMARY KEY (vendor_id, chart_id);
  41. ALTER TABLE warehouse ADD PRIMARY KEY (id);
  42. ALTER TABLE yearend ADD PRIMARY KEY (trans_id);
  43. LOCK inventory in EXCLUSIVE mode;
  44. ALTER TABLE inventory ADD COLUMN entry_id bigint;
  45. CREATE SEQUENCE inventory_entry_id_seq;
  46. ALTER TABLE inventory ALTER COLUMN entry_id
  47. SET DEFAULT nextval('inventory_entry_id_seq');
  48. UPDATE inventory SET entry_id = nextval('inventory_entry_id_seq');
  49. ALTER TABLE inventory ADD PRIMARY key (entry_id);
  50. LOCK partscustomer IN EXCLUSIVE MODE;
  51. ALTER TABLE partscustomer ADD COLUMN entry_id int;
  52. CREATE SEQUENCE partscustomer_entry_id_seq;
  53. ALTER TABLE partscustomer ALTER COLUMN entry_id
  54. SET DEFAULT nextval('partscustomer_entry_id_seq');
  55. UPDATE partscustomer SET entry_id = nextval('partscustomer_entry_id_seq');
  56. ALTER TABLE partscustomer ADD PRIMARY KEY (entry_id);
  57. LOCK partsvendor IN EXCLUSIVE MODE;
  58. ALTER TABLE partsvendor ADD COLUMN entry_id int;
  59. CREATE SEQUENCE partsvendor_entry_id_seq;
  60. ALTER TABLE partsvendor ALTER COLUMN entry_id
  61. SET DEFAULT nextval('partsvendor_entry_id_seq');
  62. UPDATE partsvendor SET entry_id = nextval('partsvendor_entry_id_seq');
  63. ALTER TABLE partsvendor ADD PRIMARY KEY (entry_id);
  64. LOCK audittrail IN EXCLUSIVE MODE;
  65. ALTER TABLE audittrail ADD COLUMN entry_id int;
  66. CREATE SEQUENCE audittrail_entry_id_seq ;
  67. ALTER TABLE audittrail ALTER COLUMN entry_id
  68. SET DEFAULT nextval('audittrail_entry_id_seq');
  69. UPDATE audittrail SET entry_id = nextval('audittrail_entry_id_seq');
  70. ALTER TABLE audittrail ADD PRIMARY KEY (entry_id);
  71. LOCK shipto IN EXCLUSIVE MODE;
  72. ALTER TABLE shipto ADD COLUMN entry_id int;
  73. CREATE SEQUENCE shipto_entry_id_seq ;
  74. ALTER TABLE shipto ALTER COLUMN entry_id
  75. SET DEFAULT nextval('shipto_entry_id_seq');
  76. UPDATE shipto SET entry_id = nextval('shipto_entry_id_seq');
  77. ALTER TABLE shipto ADD PRIMARY KEY (entry_id);
  78. CREATE TABLE taxmodule (
  79. taxmodule_id serial PRIMARY KEY,
  80. taxmodulename text NOT NULL
  81. );
  82. INSERT INTO taxmodule (
  83. taxmodule_id, taxmodulename
  84. ) VALUES (
  85. 1, 'Simple'
  86. );
  87. CREATE TABLE taxcategory (
  88. taxcategory_id serial PRIMARY KEY,
  89. taxcategoryname text NOT NULL,
  90. taxmodule_id int NOT NULL REFERENCES taxmodule (taxmodule_id)
  91. );
  92. ALTER TABLE partstax ADD COLUMN taxcategory_id int REFERENCES taxcategory (taxcategory_id);
  93. LOCK tax IN EXCLUSIVE MODE;
  94. ALTER TABLE tax ADD COLUMN pass int DEFAULT 0;
  95. UPDATE tax SET pass = 0;
  96. ALTER TABLE tax ALTER COLUMN pass SET NOT NULL;
  97. ALTER TABLE tax ADD COLUMN taxmodule_id int REFERENCES taxmodule DEFAULT 1;
  98. UPDATE tax SET taxmodule_id = 1;
  99. ALTER TABLE tax ALTER COLUMN taxmodule_id SET NOT NULL;
  100. -- Fixed session table and add users table --
  101. --CREATE TABLE users (id serial UNIQUE, username varchar(30) PRIMARY KEY);
  102. COMMENT ON TABLE users IS 'username is the primary key because we don\'t want duplicate users';
  103. --CREATE TABLE users_conf(id integer primary key references users(id) deferrable initially deferred,
  104. -- acs text,
  105. -- address text,
  106. -- businessnumber text,
  107. -- company text,
  108. -- countrycode text,
  109. -- currency text,
  110. -- dateformat text,
  111. -- dbconnect text,
  112. -- dbdriver text default 'Pg',
  113. -- dbhost text default 'localhost',
  114. -- dbname text,
  115. -- dboptions text,
  116. -- dbpasswd text,
  117. -- dbport text,
  118. -- dbuser text,
  119. -- email text,
  120. -- fax text,
  121. -- menuwidth text,
  122. -- name text,
  123. -- numberformat text,
  124. -- password varchar(32) check(length(password) = 32),
  125. -- print text,
  126. -- printer text,
  127. -- role text,
  128. -- sid text,
  129. -- signature text,
  130. -- stylesheet text,
  131. -- tel text,
  132. -- templates text,
  133. -- crypted_password text,
  134. -- timeout numeric,
  135. -- vclimit numeric);
  136. 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';
  137. COMMENT ON COLUMN users_conf.id IS 'Yes primary key with a FOREIGN KEY to users(id) is correct';
  138. COMMENT ON COLUMN users_conf.password IS 'This means we have to get rid of the current password stuff and move to presumably md5()';
  139. --LOCK session in EXCLUSIVE MODE;
  140. --DELETE FROM session;
  141. --ALTER TABLE session ADD CONSTRAINT session_token_check check (length(token::text) = 32);
  142. --ALTER TABLE session ADD column user_id integer not null references users(id);
  143. -- comment this out when user db is working:
  144. --ALTER TABLE session ALTER COLUMN user_id DROP NOT NULL;
  145. -- Admin user --
  146. --INSERT INTO users(username) VALUES ('admin');
  147. --INSERT INTO users_conf(id,password) VALUES (currval('users_id_seq'),NULL);
  148. -- Functions
  149. --CREATE FUNCTION create_user(text) RETURNS bigint AS $$
  150. -- INSERT INTO users(username) VALUES ('$1');
  151. -- SELECT currval('users_id_seq');
  152. -- $$ LANGUAGE 'SQL';
  153. COMMENT ON FUNCTION create_user(text) IS $$ Function to create user Returns users.id if successful, else it is an error. $$;
  154. --CREATE FUNCTION update_user(int4,text) RETURNS int4 AS $$
  155. -- UPDATE users SET username = '$2' WHERE id = $1;
  156. -- SELECT 1;
  157. -- $$ LANGUAGE 'SQL';
  158. 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 $$;
  159. ALTER TABLE defaults RENAME TO old_defaults;
  160. CREATE TABLE defaults (
  161. setting_key TEXT PRIMARY KEY,
  162. value TEXT
  163. );
  164. COMMENT ON TABLE defaults IS $$This table replaces the old one column per value system with a simple key => value table$$;
  165. INSERT INTO defaults (setting_key, value)
  166. SELECT 'inventory_accno_id', inventory_accno_id::text FROM old_defaults
  167. UNION
  168. SELECT 'income_accno_id', income_accno_id::text FROM old_defaults
  169. UNION
  170. SELECT 'expense_accno_id', expense_accno_id::text FROM old_defaults
  171. UNION
  172. SELECT 'fxloss_accno_id', fxloss_accno_id::text FROM old_defaults
  173. UNION
  174. SELECT 'fxgain_accno_id', fxgain_accno_id::text FROM old_defaults
  175. UNION
  176. SELECT 'sinumber', sinumber::text FROM old_defaults
  177. UNION
  178. SELECT 'sonumber', sonumber::text FROM old_defaults
  179. UNION
  180. SELECT 'yearend', yearend::text FROM old_defaults
  181. UNION
  182. SELECT 'weightunit', weightunit::text FROM old_defaults
  183. UNION
  184. SELECT 'businessnumber', businessnumber::text FROM old_defaults
  185. UNION
  186. SELECT 'version', '1.2.0'::text
  187. UNION
  188. SELECT 'curr', curr::text FROM old_defaults
  189. UNION
  190. SELECT 'closedto', closedto::text FROM old_defaults
  191. UNION
  192. SELECT 'revtrans', (CASE WHEN revtrans IS NULL THEN NULL
  193. WHEN revtrans THEN '1'
  194. ELSE '0' END) FROM old_defaults
  195. UNION
  196. SELECT 'ponumber', ponumber::text FROM old_defaults
  197. UNION
  198. SELECT 'sqnumber', sqnumber::text FROM old_defaults
  199. UNION
  200. SELECT 'rfqnumber', rfqnumber::text FROM old_defaults
  201. UNION
  202. SELECT 'audittrail', (CASE WHEN audittrail IS NULL THEN NULL
  203. WHEN audittrail THEN '1'
  204. ELSE '0' END) FROM old_defaults
  205. UNION
  206. SELECT 'vinumber', vinumber::text FROM old_defaults
  207. UNION
  208. SELECT 'employeenumber', employeenumber::text FROM old_defaults
  209. UNION
  210. SELECT 'partnumber', partnumber::text FROM old_defaults
  211. UNION
  212. SELECT 'customernumber', customernumber::text FROM old_defaults
  213. UNION
  214. SELECT 'vendornumber', vendornumber::text FROM old_defaults
  215. UNION
  216. SELECT 'glnumber', glnumber::text FROM old_defaults
  217. UNION
  218. SELECT 'projectnumber', projectnumber::text FROM old_defaults
  219. UNION
  220. SELECT 'appname', 'LedgerSMB'::text;
  221. DROP TABLE old_defaults;
  222. CREATE OR REPLACE FUNCTION del_exchangerate() RETURNS TRIGGER AS '
  223. declare
  224. t_transdate date;
  225. t_curr char(3);
  226. t_id int;
  227. d_curr text;
  228. begin
  229. select into d_curr substr(value,1,3) from defaults where setting_key = ''curr'';
  230. if TG_RELNAME = ''ar'' then
  231. select into t_curr, t_transdate curr, transdate from ar where id = old.id;
  232. end if;
  233. if TG_RELNAME = ''ap'' then
  234. select into t_curr, t_transdate curr, transdate from ap where id = old.id;
  235. end if;
  236. if TG_RELNAME = ''oe'' then
  237. select into t_curr, t_transdate curr, transdate from oe where id = old.id;
  238. end if;
  239. if d_curr != t_curr then
  240. select into t_id a.id from acc_trans ac
  241. join ar a on (a.id = ac.trans_id)
  242. where a.curr = t_curr
  243. and ac.transdate = t_transdate
  244. except select a.id from ar a where a.id = old.id
  245. union
  246. select a.id from acc_trans ac
  247. join ap a on (a.id = ac.trans_id)
  248. where a.curr = t_curr
  249. and ac.transdate = t_transdate
  250. except select a.id from ap a where a.id = old.id
  251. union
  252. select o.id from oe o
  253. where o.curr = t_curr
  254. and o.transdate = t_transdate
  255. except select o.id from oe o where o.id = old.id;
  256. if not found then
  257. delete from exchangerate where curr = t_curr and transdate = t_transdate;
  258. end if;
  259. end if;
  260. return old;
  261. end;
  262. ' language 'plpgsql';
  263. CREATE OR REPLACE FUNCTION add_custom_field (VARCHAR, VARCHAR, VARCHAR)
  264. RETURNS BOOL AS
  265. 'BEGIN
  266. EXECUTE ''SELECT TABLE_ID FROM custom_table_catalog
  267. WHERE extends = '''''' || table_name || '''''' '';
  268. IF NOT FOUND THEN
  269. BEGIN
  270. INSERT INTO custom_table_catalog (extends)
  271. VALUES (table_name);
  272. EXECUTE ''CREATE TABLE custom_''||table_name ||
  273. '' (row_id INT PRIMARY KEY)'';
  274. EXCEPTION WHEN duplicate_table THEN
  275. -- do nothing
  276. END;
  277. END IF;
  278. EXECUTE ''INSERT INTO custom_field_catalog (field_name, table_id)
  279. VALUES ( '''''' || new_field_name ||'''''', (SELECT table_id FROM custom_table_catalog
  280. WHERE extends = ''''''|| table_name || ''''''))'';
  281. EXECUTE ''ALTER TABLE custom_''||table_name || '' ADD COLUMN ''
  282. || new_field_name || '' '' || field_datatype;
  283. RETURN TRUE;
  284. END;
  285. ' LANGUAGE PLPGSQL;
  286. COMMIT;