File: kwdb.sql

package info (click to toggle)
kworkflow 1%3A0.10-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 5,212 kB
  • sloc: sh: 31,869; perl: 2,172; sql: 268; ansic: 114; xml: 84; python: 76; makefile: 27
file content (240 lines) | stat: -rw-r--r-- 6,683 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
PRAGMA foreign_keys = 1;
BEGIN TRANSACTION;
-- Tables

-- This table holds the metadata of the config files handled by kw
CREATE TABLE IF NOT EXISTS "kernel_config" (
  "id" INTEGER NOT NULL UNIQUE,
  "name" TEXT NOT NULL UNIQUE,
  "description" TEXT,
  "path" TEXT NOT NULL UNIQUE,
  "last_updated_datetime" TEXT NOT NULL,
  PRIMARY KEY("id")
);

-- This table holds the names of the commands that kw keeps track of, like
-- build and deploy
CREATE TABLE IF NOT EXISTS "command_label" (
  "id" INTEGER NOT NULL UNIQUE,
  "name" TEXT NOT NULL UNIQUE,
  PRIMARY KEY("id")
);

-- Table containing the possible exit status of an executed commmand
CREATE TABLE IF NOT EXISTS "status" (
  "id" INTEGER NOT NULL UNIQUE,
  "name" TEXT NOT NULL UNIQUE,
  PRIMARY KEY("id")
);

-- Table containing user created tags
CREATE TABLE IF NOT EXISTS "tag" (
  "id" INTEGER NOT NULL UNIQUE,
  "name" TEXT NOT NULL UNIQUE,
  -- The 'active' attribute is a boolean to denote if the tag is active
  -- or not. This is useful for listing just a subset of tags in the DB
  "active" INTEGER NOT NULL CHECK ("active" IN (0, 1)) DEFAULT 1,
  PRIMARY KEY("id")
);

-- This is the table that holds the events triggered by kw, currently pertains
-- to the executed commands that are saved and the pomodoro sessions created
CREATE TABLE IF NOT EXISTS "event" (
  "id" INTEGER NOT NULL UNIQUE,
  "date" TEXT NOT NULL,
  "time" TEXT,
  PRIMARY KEY("id")
);

-- This is the relationship between an "event" that executes a given
-- "command_label"
CREATE TABLE IF NOT EXISTS "executed" (
  "id" INTEGER NOT NULL UNIQUE,
  "command_label_id" INTEGER NOT NULL,
  "elapsed_time_in_secs" INTEGER NOT NULL,
  "status_id" INTEGER NOT NULL,
  PRIMARY KEY("id"),
  FOREIGN KEY("id") REFERENCES "event"("id")
    ON UPDATE CASCADE ON DELETE CASCADE,
  FOREIGN KEY("command_label_id") REFERENCES "command_label"("id")
    ON UPDATE CASCADE ON DELETE RESTRICT,
  FOREIGN KEY("status_id") REFERENCES "status"("id")
    ON UPDATE CASCADE ON DELETE RESTRICT
);

-- Table containing the information related to each pomodoro timebox
CREATE TABLE IF NOT EXISTS "timebox" (
  "id" INTEGER NOT NULL UNIQUE,
  "duration" INTEGER NOT NULL,
  "description" TEXT,
  "tag_id" INTEGER NOT NULL,
  PRIMARY KEY("id"),
  FOREIGN KEY("id") REFERENCES "event"("id")
    ON UPDATE CASCADE ON DELETE CASCADE,
  FOREIGN KEY("tag_id") REFERENCES "tag"("id")
    ON UPDATE CASCADE ON DELETE RESTRICT
);

-- Populate commands and status tables
INSERT OR IGNORE INTO "status" ("name") VALUES
  ('success'),
  ('failure'),
  ('interrupted'),
  ('unknown');

INSERT OR IGNORE INTO "command_label" ("name") VALUES
  ('backup'),
  ('bd'),
  ('build'),
  ('clear-cache'),
  ('codestyle'),
  ('config'),
  ('debug'),
  ('deploy'),
  ('device'),
  ('diff'),
  ('drm'),
  ('env'),
  ('explore'),
  ('init'),
  ('kernel_config_manager'),
  ('list'),
  ('mail'),
  ('maintainers'),
  ('man'),
  ('modules_deploy'),
  ('pomodoro'),
  ('remote'),
  ('report'),
  ('self-update'),
  ('ssh'),
  ('uninstall'),
  ('vm');

-- Views
-- This view shows the currently active pomodoro sessions
CREATE VIEW IF NOT EXISTS "active_timebox"
AS
SELECT
  "e_id" AS "id",
  "tag",
  "date",
  "time",
  "duration",
  "description"
FROM
  (SELECT "id" AS "e_id", "date", "time" FROM "event" ORDER BY "date")
JOIN "timebox" ON "e_id" IS "timebox"."id"
JOIN (SELECT "id" AS "g_id", "name" AS "tag" FROM "tag") ON "g_id" IS "timebox"."tag_id"
WHERE
  CAST (strftime('%s',"date"||'T'||"time",'utc') + "duration" AS INTEGER) >= CAST (strftime('%s','now') AS INTEGER);

-- This view aggregates all the pomodoro sessions on record
CREATE VIEW IF NOT EXISTS "pomodoro_report"
AS
SELECT
  "e_id" AS "id",
  "tag_id",
  "name" AS "tag_name",
  "date",
  "time",
  "duration",
  "description"
FROM
  (SELECT "id" AS "e_id", "date", "time" FROM "event")
JOIN "timebox" ON "timebox"."id" IS "e_id"
JOIN "tag" ON "tag"."id" IS "timebox"."tag_id";

-- This view aggregates all data relevant to the statistics reports
CREATE VIEW IF NOT EXISTS "statistics_report"
AS
SELECT
  "e_id" AS "id",
  "name" AS "label_name",
  "status_name" AS "status",
  "date",
  "time",
  "elapsed_time_in_secs"
FROM
  (SELECT "id" AS "e_id", "date", "time" FROM "event")
JOIN "executed" ON "executed"."id" IS "e_id"
JOIN "command_label" ON "command_label"."id" IS "executed"."command_label_id"
JOIN (SELECT "id" AS "s_id", "name" AS "status_name" FROM "status") ON "s_id" IS "executed"."status_id";

-- Indexes
CREATE INDEX IF NOT EXISTS "command_label_idx" ON "command_label" (
  "name" ASC,
  "id"
);

CREATE INDEX IF NOT EXISTS "tag_idx" ON "tag" (
  "active" DESC,
  "name" ASC,
  "id"
);

CREATE INDEX IF NOT EXISTS "chronological_events" ON "event" (
  "date" ASC,
  "time" ASC,
  "id"
);

CREATE INDEX IF NOT EXISTS "executed_idx" ON "executed" (
  "command_label_id" ASC,
  "status_id" ASC,
  "elapsed_time_in_secs" DESC,
  "id"
);

CREATE INDEX IF NOT EXISTS "timebox_idx" ON "timebox" (
  "tag_id" ASC,
  "duration" DESC,
  "id",
  "description"
);

-- Triggers
CREATE TRIGGER IF NOT EXISTS "delete_pomodoro" INSTEAD OF DELETE ON "pomodoro_report"
  BEGIN
    DELETE FROM "timebox" WHERE "timebox"."id" IS "OLD"."id";
    DELETE FROM "event" WHERE "event"."id" IS "OLD"."id";
  END;

CREATE TRIGGER IF NOT EXISTS "delete_statistics" INSTEAD OF DELETE ON "statistics_report"
  BEGIN
    DELETE FROM "executed" WHERE "executed"."id" IS "OLD"."id";
    DELETE FROM "event" WHERE "event"."id" IS "OLD"."id";
  END;

CREATE TRIGGER IF NOT EXISTS "insert_pomodoro" INSTEAD OF INSERT ON "pomodoro_report"
  BEGIN
    INSERT OR IGNORE INTO "tag" ("name") VALUES ("NEW"."tag_name");

    INSERT INTO "event" ("date", "time") VALUES ("NEW"."date", "NEW"."time");

    INSERT INTO "timebox" ("id","duration","description","tag_id")
      VALUES(
        last_insert_rowid(),
        "NEW"."duration",
        "NEW"."description",
        (SELECT "id" FROM "tag" AS "g" WHERE "g"."name" IS "NEW"."tag_name")
      );
  END;

CREATE TRIGGER IF NOT EXISTS "insert_statistics" INSTEAD OF INSERT ON "statistics_report"
  BEGIN
    INSERT OR IGNORE INTO "command_label" ("name") VALUES ("NEW"."label_name");
    INSERT OR IGNORE INTO "status" ("name") VALUES ("NEW"."status");

    INSERT INTO "event" ("date", "time") VALUES ("NEW"."date", "NEW"."time");

    INSERT INTO "executed" ("id", "command_label_id", "status_id", "elapsed_time_in_secs")
      VALUES (
        last_insert_rowid(),
        (SELECT "id" FROM "command_label" AS "c" WHERE "c"."name" IS "NEW"."label_name"),
        (SELECT "id" FROM "status" AS "s" WHERE "s"."name" IS "NEW"."status"),
        "NEW"."elapsed_time_in_secs"
      );
  END;

COMMIT;