From 8449cce4451570fc8a0cd2df3565e0d0de0b7fae Mon Sep 17 00:00:00 2001
From: einhverfr <einhverfr@4979c152-3d1c-0410-bac9-87ea11338e46>
Date: Tue, 19 Sep 2006 02:46:01 +0000
Subject: Committing db fixes from Seneca

git-svn-id: https://ledger-smb.svn.sourceforge.net/svnroot/ledger-smb/trunk@115 4979c152-3d1c-0410-bac9-87ea11338e46
---
 sql/Pg-upgrade-2.6.17-2.6.18.sql | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

(limited to 'sql/Pg-upgrade-2.6.17-2.6.18.sql')

diff --git a/sql/Pg-upgrade-2.6.17-2.6.18.sql b/sql/Pg-upgrade-2.6.17-2.6.18.sql
index 8144f1df..75cd02ab 100644
--- a/sql/Pg-upgrade-2.6.17-2.6.18.sql
+++ b/sql/Pg-upgrade-2.6.17-2.6.18.sql
@@ -1,3 +1,4 @@
+ALTER TABLE chart ADD PRIMARY KEY (id);
 -- linuxpoet:
 -- adding primary key to acc_trans
 -- We are using standard postgresql names for the sequence for consistency as we move forward
@@ -110,7 +111,7 @@ DO ALSO INSERT INTO transaction_ledger (id, table_name) VALUES (new.id, 'ap');
 CREATE RULE ap_id_track_u AS ON update TO ap 
 DO ALSO UPDATE transaction_ledger SET id = new.id WHERE id = old.id;
 
-insert into transaction_ledger (id, table_name) SELECT id, 'ar' FROM ap;
+insert into transaction_ledger (id, table_name) SELECT id, 'ar' FROM ar;
 
 CREATE RULE ar_id_track_i AS ON insert TO ar 
 DO ALSO INSERT INTO transaction_ledger (id, table_name) VALUES (new.id, 'ar');
@@ -246,8 +247,13 @@ BEGIN
 	EXECUTE ''SELECT TABLE_ID FROM custom_table_catalog 
 		WHERE extends = '''''' || table_name || '''''' '';
 	IF NOT FOUND THEN
-		INSERT INTO custom_table_catalog (extends) VALUES (table_name);
-		EXECUTE ''CREATE TABLE custom_''||table_name || '' ()'';
+		BEGIN
+			INSERT INTO custom_table_catalog (extends) VALUES (table_name);
+			EXECUTE ''CREATE TABLE custom_''||table_name || 
+				'' (row_id INT)'';
+		EXCEPTION WHEN duplicate_table THEN
+			-- do nothing
+		END;
 	END IF;
 	EXECUTE ''INSERT INTO custom_field_catalog (field_name, table_id)
 	VALUES ( '''''' || new_field_name ||'''''', (SELECT table_id FROM custom_table_catalog
@@ -270,7 +276,7 @@ BEGIN
 		table_id = (SELECT table_id FROM custom_table_catalog 
 			WHERE extends = table_name);
 	EXECUTE ''ALTER TABLE custom_'' || table_name || 
-		'' DROP COLUMN '' || field_name;
+		'' DROP COLUMN '' || custom_field_name;
 	RETURN TRUE;	
 END;
 ' LANGUAGE PLPGSQL;
-- 
cgit v1.2.3

