Add a primary_key to the addressbook_addresses_* tables

This is part of the fix for #276.
This commit is contained in:
Andrew Ruthven 2023-02-04 20:26:21 +13:00
parent c3970f3be2
commit 6819d6d7d1
7 changed files with 92 additions and 35 deletions

View File

@ -369,6 +369,7 @@ CREATE TABLE addressbook_resource (
CREATE TABLE addressbook_address_adr (
dav_id INT8 NOT NULL REFERENCES caldav_data(dav_id) ON UPDATE CASCADE ON DELETE CASCADE,
count INT,
type TEXT,
box_no TEXT,
unit_no TEXT,
@ -377,21 +378,26 @@ CREATE TABLE addressbook_address_adr (
region TEXT,
postcode TEXT,
country TEXT,
property TEXT -- The full text of the property
property TEXT, -- The full text of the property
UNIQUE (dav_id, count)
);
CREATE TABLE addressbook_address_tel (
dav_id INT8 NOT NULL REFERENCES caldav_data(dav_id) ON UPDATE CASCADE ON DELETE CASCADE,
count INT,
type TEXT,
tel TEXT,
property TEXT -- The full text of the property
property TEXT, -- The full text of the property
UNIQUE (dav_id, count)
);
CREATE TABLE addressbook_address_email (
dav_id INT8 NOT NULL REFERENCES caldav_data(dav_id) ON UPDATE CASCADE ON DELETE CASCADE,
count INT,
type TEXT,
email TEXT,
property TEXT -- The full text of the property
property TEXT, -- The full text of the property
UNIQUE (dav_id, count)
);
@ -489,4 +495,4 @@ CREATE SEQUENCE metrics_count_delticket;
CREATE SEQUENCE metrics_count_bind;
CREATE SEQUENCE metrics_count_unknown;
SELECT new_db_revision(1,3,3, 'Marzec' );
SELECT new_db_revision(1,3,4, 'Kwiecień' );

51
dba/patches/1.3.4.sql Normal file
View File

@ -0,0 +1,51 @@
-- Notable enhancement: add unique constraints to tables missing them
BEGIN;
SELECT check_db_revision(1,3,3);
-- Temporary sequence so we can set the count in each of the tables.
--
-- While this doesn't give the result we really want, where count increments
-- for each dav_id, it'll give us the uniqueness we want. Then as users
-- update their records, we'll eventually up with counts per dav_id.
CREATE SEQUENCE temporary_seq;
-- addressbook_address_adr
ALTER TABLE addressbook_address_adr
ADD COLUMN count INT;
UPDATE addressbook_address_adr
SET count = nextval('temporary_seq');
CREATE UNIQUE INDEX addressbook_address_adr_pk
ON addressbook_address_adr(dav_id, count);
-- addressbook_address_email
ALTER TABLE addressbook_address_email
ADD COLUMN count INT;
CREATE UNIQUE INDEX addressbook_address_email_pk
ON addressbook_address_email(dav_id, count);
UPDATE addressbook_address_email
SET count = nextval('temporary_seq');
-- addressbook_address_tel
ALTER TABLE addressbook_address_tel
ADD COLUMN count INT;
UPDATE addressbook_address_tel
SET count = nextval('temporary_seq');
CREATE UNIQUE INDEX addressbook_address_tel_pk
ON addressbook_address_tel(dav_id, count);
-- Tidy up after ourselves.
DROP SEQUENCE temporary_seq;
-- http://blogs.transparent.com/polish/names-of-the-months-and-their-meaning/
SELECT new_db_revision(1,3,4, 'Kwiecień' );
COMMIT;
ROLLBACK;

View File

@ -66,18 +66,7 @@ VALUES( :dav_id, :version, :uid, :nickname, :fn, :name, :note, :org, :url, :fbur
/**
CREATE TABLE addressbook_address_adr (
dav_id INT8 NOT NULL REFERENCES caldav_data(dav_id) ON UPDATE CASCADE ON DELETE CASCADE,
type TEXT,
box_no TEXT,
unit_no TEXT,
street_address TEXT,
locality TEXT,
region TEXT,
postcode TEXT,
country TEXT,
property TEXT -- The full text of the property
);
* Break out addresses.
*/
function WriteAddresses( $dav_id ) {
$addresses = $this->GetProperties('ADR');
@ -89,9 +78,12 @@ CREATE TABLE addressbook_address_adr (
$params = array( ':dav_id' => $dav_id );
$qry->QDo('DELETE FROM addressbook_address_adr WHERE dav_id = :dav_id', $params );
$count = 0;
foreach( $addresses AS $adr ) {
$type = $adr->GetParameterValue('TYPE');
if ( is_array($type) ) $type = implode('~|~',$type);
$params[':type'] = $type;
//explode on ; that is not preceeded by an \
$address = preg_split( '{(?<!\\\\);}', $adr->Value());
@ -105,21 +97,18 @@ CREATE TABLE addressbook_address_adr (
@$params[':region'] = $address[4];
@$params[':postcode'] = $address[5];
@$params[':country'] = $address[6];
$params[':property'] = $adr->Render();
$qry->QDo( 'INSERT INTO addressbook_address_adr (dav_id, type, box_no, unit_no, street_address, locality, region, postcode, country, property)
VALUES( :dav_id, :type, :box_no, :unit_no, :street_address, :locality, :region, :postcode, :country, :property)', $params );
$params[':property'] = $adr->Render();
$params[':count'] = $count++;
$qry->QDo( 'INSERT INTO addressbook_address_adr (dav_id, count, type, box_no, unit_no, street_address, locality, region, postcode, country, property)
VALUES ( :dav_id, :count, :type, :box_no, :unit_no, :street_address, :locality, :region, :postcode, :country, :property)', $params );
}
if ( ! $in_transaction ) $qry->Commit();
}
/**
CREATE TABLE addressbook_address_tel (
dav_id INT8 NOT NULL REFERENCES caldav_data(dav_id) ON UPDATE CASCADE ON DELETE CASCADE,
type TEXT,
tel TEXT,
property TEXT -- The full text of the property
);
* Break out phones.
*/
function WritePhones( $dav_id ) {
$telephones = $this->GetProperties('TEL');
@ -131,14 +120,20 @@ CREATE TABLE addressbook_address_tel (
$params = array( ':dav_id' => $dav_id );
$qry->QDo('DELETE FROM addressbook_address_tel WHERE dav_id = :dav_id', $params );
$count = 0;
foreach( $telephones AS $tel ) {
$type = $tel->GetParameterValue('TYPE');
if ( is_array($type) ) $type = implode('~|~',$type);
$params[':type'] = $type;
if ( ! isset($params[':type']) ) $params[':type'] = 'voice';
$params[':tel'] = $tel->Value();
$params[':tel'] = $tel->Value();
$params[':property'] = $tel->Render();
$qry->QDo( 'INSERT INTO addressbook_address_tel (dav_id, type, tel, property) VALUES( :dav_id, :type, :tel, :property)', $params );
$params[':count'] = $count++;
$qry->QDo( 'INSERT INTO addressbook_address_tel (dav_id, count, type, tel, property) VALUES ( :dav_id, :count, :type, :tel, :property)', $params );
}
if ( ! $in_transaction ) $qry->Commit();
}
@ -162,15 +157,20 @@ CREATE TABLE addressbook_address_email (
$params = array( ':dav_id' => $dav_id );
$qry->QDo('DELETE FROM addressbook_address_email WHERE dav_id = :dav_id', $params );
$count = 0;
foreach( $emails AS $email ) {
$type = $email->GetParameterValue('TYPE');
if ( is_array($type) ) $type = implode('~|~',$type);
$params[':type'] = $type;
$params[':email'] = $email->Value();
$params[':type'] = $type;
$params[':email'] = $email->Value();
$params[':property'] = $email->Render();
$qry->QDo( 'INSERT INTO addressbook_address_email (dav_id, type, email, property) VALUES( :dav_id, :type, :email, :property)', $params );
$params[':count'] = $count++;
$qry->QDo( 'INSERT INTO addressbook_address_email (dav_id, count, type, email, property) VALUES ( :dav_id, :count, :type, :email, :property)', $params );
}
if ( ! $in_transaction ) $qry->Commit();
}
}
}

View File

@ -1,4 +1,4 @@
The database is version XX currently at revision 1.3.3.
The database is version XX currently at revision 1.3.4.
No patches were applied.
Supported locales updated.
Updated view: dav_principal.sql applied.

View File

@ -1,4 +1,4 @@
The database is version XX currently at revision 1.3.3.
The database is version XX currently at revision 1.3.4.
No patches were applied.
Supported locales updated.
Updated view: dav_principal.sql applied.

View File

@ -1,4 +1,4 @@
The database is version XX currently at revision 1.3.3.
The database is version XX currently at revision 1.3.4.
No patches were applied.
Supported locales updated.
Updated view: dav_principal.sql applied.

View File

@ -1,4 +1,4 @@
The database is version XX currently at revision 1.3.3.
The database is version XX currently at revision 1.3.4.
No patches were applied.
Supported locales updated.
Updated view: dav_principal.sql applied.