wc-checks.sql revision 251886
11096Schegar/* wc-checks.sql -- trigger-based checks for the wc-metadata database.
22362Sohair *     This is intended for use with SQLite 3
31096Schegar *
41096Schegar * ====================================================================
51096Schegar *    Licensed to the Apache Software Foundation (ASF) under one
61096Schegar *    or more contributor license agreements.  See the NOTICE file
71096Schegar *    distributed with this work for additional information
81096Schegar *    regarding copyright ownership.  The ASF licenses this file
91096Schegar *    to you under the Apache License, Version 2.0 (the
101096Schegar *    "License"); you may not use this file except in compliance
111096Schegar *    with the License.  You may obtain a copy of the License at
121096Schegar *
131096Schegar *      http://www.apache.org/licenses/LICENSE-2.0
141096Schegar *
151096Schegar *    Unless required by applicable law or agreed to in writing,
161096Schegar *    software distributed under the License is distributed on an
171096Schegar *    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
181096Schegar *    KIND, either express or implied.  See the License for the
192362Sohair *    specific language governing permissions and limitations
202362Sohair *    under the License.
212362Sohair * ====================================================================
221096Schegar */
231096Schegar
241096Schegar
251096Schegar-- STMT_VERIFICATION_TRIGGERS
261096Schegar
271096Schegar/* ------------------------------------------------------------------------- */
281096Schegar
291096SchegarCREATE TEMPORARY TRIGGER no_repository_updates BEFORE UPDATE ON repository
301096SchegarBEGIN
311096Schegar  SELECT RAISE(FAIL, 'Updates to REPOSITORY are not allowed.');
321096SchegarEND;
331096Schegar
341096Schegar/* ------------------------------------------------------------------------- */
351096Schegar
361096Schegar/* Verify: on every NODES row: parent_relpath is parent of local_relpath */
371096SchegarCREATE TEMPORARY TRIGGER validation_01 BEFORE INSERT ON nodes
381096SchegarWHEN NOT ((new.local_relpath = '' AND new.parent_relpath IS NULL)
391096Schegar          OR (relpath_depth(new.local_relpath)
401096Schegar              = relpath_depth(new.parent_relpath) + 1))
411096SchegarBEGIN
421096Schegar  SELECT RAISE(FAIL, 'WC DB validity check 01 failed');
431096SchegarEND;
441096Schegar
451096Schegar/* Verify: on every NODES row: its op-depth <= its own depth */
461096SchegarCREATE TEMPORARY TRIGGER validation_02 BEFORE INSERT ON nodes
471096SchegarWHEN NOT new.op_depth <= relpath_depth(new.local_relpath)
481096SchegarBEGIN
491096Schegar  SELECT RAISE(FAIL, 'WC DB validity check 02 failed');
501096SchegarEND;
511096Schegar
521096Schegar/* Verify: on every NODES row: it is an op-root or it has a parent with the
531096Schegar    sames op-depth. (Except when the node is a file external) */
541096SchegarCREATE TEMPORARY TRIGGER validation_03 BEFORE INSERT ON nodes
551096SchegarWHEN NOT (
561096Schegar    (new.op_depth = relpath_depth(new.local_relpath))
571096Schegar    OR
581096Schegar    (EXISTS (SELECT 1 FROM nodes
591096Schegar              WHERE wc_id = new.wc_id AND op_depth = new.op_depth
601096Schegar                AND local_relpath = new.parent_relpath))
611096Schegar  )
621096Schegar AND NOT (new.file_external IS NOT NULL AND new.op_depth = 0)
631096SchegarBEGIN
641096Schegar  SELECT RAISE(FAIL, 'WC DB validity check 03 failed');
651096SchegarEND;
661096Schegar
671096Schegar/* Verify: on every ACTUAL row (except root): a NODES row exists at its
681096Schegar * parent path. */
691096SchegarCREATE TEMPORARY TRIGGER validation_04 BEFORE INSERT ON actual_node
701096SchegarWHEN NOT (new.local_relpath = ''
711096Schegar          OR EXISTS (SELECT 1 FROM nodes
721096Schegar                       WHERE wc_id = new.wc_id
731096Schegar                         AND local_relpath = new.parent_relpath))
741096SchegarBEGIN
751096Schegar  SELECT RAISE(FAIL, 'WC DB validity check 04 failed');
761096SchegarEND;
771096Schegar
781096Schegar