Edgewall Software

Changes between Initial Version and Version 1 of RelationalModel


Ignore:
Timestamp:
Jul 4, 2010, 4:56:58 PM (14 years ago)
Author:
tracdev@…
Comment:

Initial submission

Legend:

Unmodified
Added
Removed
Modified
  • RelationalModel

    v1 v1  
     1== Relational Model for Trac ==
     2
     3As outlined in my post to trac-dev, we should use the relational features of the database to arrive at clean, generic solutions. The first step would be to eliminate Ticket and Milestone from the model and focus on the common denominator: Work Efforts.
     4
     5[[Image(workeffort.jpg)]]
     6
     7Source: The Data Model Resource Book Vol 3 p 178 (ISBN 978-0-470-17845-4)
     8
     9Work Effort is a unit of work, like a ticket, project, milestone, timebox
     10or similar entity that represent work with a start..end time (enumerated
     11in Effort Type). Rows in Association table connect *any* two work efforts
     12together with a specified (or not) relationship type + rule (enumerated in
     13the right-hand tables). This allows very flexible aggregation, and we can
     14support all kinds of evolving/plug-in relationships, breakdown structures
     15and entity types without changing the model. This is not "normalized" per se,
     16but the idea is to model the real entities and relationships.
     17
     18The big problem with GenericTrac, is that the core of the model is a
     19''nonexisting'' entity (resource), in practice emulating the database system
     20within itself. We'll end up reinventing relational algebra in middleware,
     21bit by bit, to compensate for this fact; If you still think that will be
     22simpler long-term than *using* the established algebra to work with data,
     23I can only suggest you consider it carefully before starting ;-)
     24
     25(it is possible to port the model to older versions using triggers)
     26
     27== SQL to consider (SQLite 3.6.19 or newer) ==
     28{{{
     29#!sql
     30PRAGMA foreign_keys = ON;
     31BEGIN EXCLUSIVE;
     32
     33-- custom field *data* from EAV system points here ("resource_prop")
     34CREATE TABLE WorkEfforts (
     35  effort_id     INTEGER PRIMARY KEY AUTOINCREMENT,
     36  effort_type   INTEGER,
     37  -- notice how useful these two date columns are within
     38  -- the "hierarchy" of work, when compared to ticket-milestone:
     39  from_date     JULIAN DEFAULT 0
     40    CHECK(from_date <= thru_date),
     41  thru_date     JULIAN DEFAULT 0
     42    CHECK(thru_date >= from_date),
     43  name          VARCHAR(128) NOT NULL,
     44  -- properties common to all work efforts here
     45  FOREIGN KEY(effort_type) REFERENCES WorkEffortTypes(type_id)
     46);
     47
     48-- custom field *defs* from EAV system points here ("resource_schema")
     49CREATE TABLE WorkEffortTypes (
     50  type_id       INTEGER PRIMARY KEY AUTOINCREMENT,
     51  parent_id     INTEGER DEFAULT NULL,
     52  name          VARCHAR(128) NOT NULL,
     53  -- common properties and hints to middleware here.
     54  FOREIGN KEY(parent_id) REFERENCES WorkEffortTypes(type_id)
     55    ON UPDATE CASCADE
     56    ON DELETE SET DEFAULT
     57);
     58
     59CREATE TABLE WorkEffortAssocs (
     60  from_eff_id   INTEGER NOT NULL REFERENCES WorkEfforts(effort_id),
     61  to_eff_id     INTEGER NOT NULL REFERENCES WorkEfforts(effort_id),
     62  assoc_type    INTEGER DEFAULT NULL,
     63  assoc_rule    INTEGER DEFAULT NULL,
     64  -- further details on the relationship here, when was it established,
     65  -- by who, for what time period is it valid etc.
     66  FOREIGN KEY(assoc_type) REFERENCES WorkEffortAssocTypes(type_id)
     67    ON UPDATE CASCADE
     68    ON DELETE CASCADE,
     69  FOREIGN KEY(assoc_rule) REFERENCES WorkEffortAssocRules(rule_id)
     70    ON UPDATE CASCADE
     71    ON DELETE CASCADE
     72);
     73
     74CREATE TABLE WorkEffortAssocRules (
     75  rule_id       INTEGER PRIMARY KEY AUTOINCREMENT,
     76  name          VARCHAR(128) NOT NULL CONSTRAINT assocrule_name UNIQUE
     77  -- further hints to the middleware about business rules here
     78);
     79
     80CREATE TABLE WorkEffortAssocTypes (
     81  type_id       INTEGER PRIMARY KEY AUTOINCREMENT,
     82  parent_id     INTEGER DEFAULT NULL,
     83  name          VARCHAR(128) NOT NULL,
     84  -- further hints to the middleware about business rules here
     85  FOREIGN KEY(parent_id) REFERENCES WorkEffortAssocTypes(type_id)
     86    ON UPDATE CASCADE
     87    ON DELETE SET DEFAULT
     88);
     89
     90-- Test data
     91INSERT INTO WorkEffortTypes (name) VALUES ('Project');
     92INSERT INTO WorkEffortTypes (parent_id, name) VALUES (1, 'Phase');
     93INSERT INTO WorkEffortTypes (parent_id, name) VALUES (2, 'Task');
     94
     95INSERT INTO WorkEffortAssocRules (name) VALUES ('Precedent');
     96INSERT INTO WorkEffortAssocRules (name) VALUES ('Concurrent');
     97INSERT INTO WorkEffortAssocRules (name) VALUES ('Complement');
     98INSERT INTO WorkEffortAssocRules (name) VALUES ('Substitute');
     99
     100INSERT INTO WorkEffortAssocTypes (name) VALUES ('Work Breakdown');
     101INSERT INTO WorkEffortAssocTypes (name) VALUES ('Program');
     102INSERT INTO WorkEffortAssocTypes (name) VALUES ('Peer to Peer');
     103
     104COMMIT;
     105}}}