wc-checks.sql revision 251881
1/* wc-checks.sql -- trigger-based checks for the wc-metadata database.
2 *     This is intended for use with SQLite 3
3 *
4 * ====================================================================
5 *    Licensed to the Apache Software Foundation (ASF) under one
6 *    or more contributor license agreements.  See the NOTICE file
7 *    distributed with this work for additional information
8 *    regarding copyright ownership.  The ASF licenses this file
9 *    to you under the Apache License, Version 2.0 (the
10 *    "License"); you may not use this file except in compliance
11 *    with the License.  You may obtain a copy of the License at
12 *
13 *      http://www.apache.org/licenses/LICENSE-2.0
14 *
15 *    Unless required by applicable law or agreed to in writing,
16 *    software distributed under the License is distributed on an
17 *    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18 *    KIND, either express or implied.  See the License for the
19 *    specific language governing permissions and limitations
20 *    under the License.
21 * ====================================================================
22 */
23
24
25-- STMT_VERIFICATION_TRIGGERS
26
27/* ------------------------------------------------------------------------- */
28
29CREATE TEMPORARY TRIGGER no_repository_updates BEFORE UPDATE ON repository
30BEGIN
31  SELECT RAISE(FAIL, 'Updates to REPOSITORY are not allowed.');
32END;
33
34/* ------------------------------------------------------------------------- */
35
36/* Verify: on every NODES row: parent_relpath is parent of local_relpath */
37CREATE TEMPORARY TRIGGER validation_01 BEFORE INSERT ON nodes
38WHEN NOT ((new.local_relpath = '' AND new.parent_relpath IS NULL)
39          OR (relpath_depth(new.local_relpath)
40              = relpath_depth(new.parent_relpath) + 1))
41BEGIN
42  SELECT RAISE(FAIL, 'WC DB validity check 01 failed');
43END;
44
45/* Verify: on every NODES row: its op-depth <= its own depth */
46CREATE TEMPORARY TRIGGER validation_02 BEFORE INSERT ON nodes
47WHEN NOT new.op_depth <= relpath_depth(new.local_relpath)
48BEGIN
49  SELECT RAISE(FAIL, 'WC DB validity check 02 failed');
50END;
51
52/* Verify: on every NODES row: it is an op-root or it has a parent with the
53    sames op-depth. (Except when the node is a file external) */
54CREATE TEMPORARY TRIGGER validation_03 BEFORE INSERT ON nodes
55WHEN NOT (
56    (new.op_depth = relpath_depth(new.local_relpath))
57    OR
58    (EXISTS (SELECT 1 FROM nodes
59              WHERE wc_id = new.wc_id AND op_depth = new.op_depth
60                AND local_relpath = new.parent_relpath))
61  )
62 AND NOT (new.file_external IS NOT NULL AND new.op_depth = 0)
63BEGIN
64  SELECT RAISE(FAIL, 'WC DB validity check 03 failed');
65END;
66
67/* Verify: on every ACTUAL row (except root): a NODES row exists at its
68 * parent path. */
69CREATE TEMPORARY TRIGGER validation_04 BEFORE INSERT ON actual_node
70WHEN NOT (new.local_relpath = ''
71          OR EXISTS (SELECT 1 FROM nodes
72                       WHERE wc_id = new.wc_id
73                         AND local_relpath = new.parent_relpath))
74BEGIN
75  SELECT RAISE(FAIL, 'WC DB validity check 04 failed');
76END;
77
78