Skip to content

Commit 07b2a25

Browse files
committed
create index and tests
1 parent 781ea28 commit 07b2a25

File tree

6 files changed

+68
-2
lines changed

6 files changed

+68
-2
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ verdb status
2424
verdb up all
2525
```
2626

27+
```
28+
verdb gen trigger students --table=students --func=log_students
29+
verdb gen table instructors --table=instructors
30+
verdb gen index instructors-index --table=instructors --fields=name,location
31+
```
32+
2733
# install
2834

2935
With [npm](https://npmjs.org) do:

lib/generate.coffee

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
trigger = require './generators/trigger'
22
table = require './generators/table'
3+
index = require './generators/index'
34

45
exports.dispatch = (nconf, params) ->
56
type = params?[0] or throw "type required"
@@ -8,5 +9,6 @@ exports.dispatch = (nconf, params) ->
89
switch type
910
when 'trigger' then trigger.generate batch, params, nconf
1011
when 'table' then table.generate batch, params, nconf
12+
when 'index' then index.generate batch, params, nconf
1113
else
1214
throw "unknown type #{type}"

lib/generators/index.coffee

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
fs = require 'fs'
2+
utils = require '../utils'
3+
4+
exports.inject = (_utils) ->
5+
utils = _utils
6+
7+
exports.index_name = (nconf, table, fields) ->
8+
if nconf.get("index")
9+
nconf.get("index")
10+
else
11+
"#{table}_#{fields.replace(new RegExp(',','gi'), '_')}_idx"
12+
13+
exports.generate = (batch, params, nconf) ->
14+
table_name = nconf.get("table") or throw "table required"
15+
fields = nconf.get("fields") or throw "fields required"
16+
name = exports.index_name(nconf, table_name, fields)
17+
18+
up_buf = "CREATE INDEX #{name} ON #{table_name}(#{fields});"
19+
down_buf = "DROP INDEX #{name};"
20+
21+
utils.create_batch batch, up_buf, down_buf, nconf, (err) ->
22+
console.log err if err?

lib/generators/table.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ DROP TABLE __TABLE__;
1616
exports.inject = (_utils) ->
1717
utils = _utils
1818

19-
exports.generate = (batch, params, nconf, persist) ->
19+
exports.generate = (batch, params, nconf) ->
2020
table_name = nconf.get("table") or throw "table required"
2121

2222
up_buf = up_template

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "verdb",
33
"description": "database version control",
4-
"version": "0.0.7",
4+
"version": "0.0.8",
55
"author": "Aubrey Keus <[email protected]>",
66
"homepage": "https://github.com/aekeus/verdb",
77
"bugs": "https://github.com/aekeus/verdb/issues",

test/gen.coffee

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,42 @@
22
test = require('tap').test
33
trg = require '../lib/generators/trigger'
44
tbl = require '../lib/generators/table'
5+
idx = require '../lib/generators/index'
6+
7+
test "generate index", (t) ->
8+
t.plan 5
9+
nconf =
10+
get: (k) ->
11+
12+
t.throws ->
13+
idx.generate "blah", [], nconf, ->
14+
, "table required"
15+
16+
nconf =
17+
get: (k) ->
18+
switch k
19+
when table then 'instructors'
20+
21+
t.throws ->
22+
idx.generate "blah", [], nconf, ->
23+
, "fields required"
24+
25+
nconf =
26+
get: (k) ->
27+
switch k
28+
when "table" then "instructors"
29+
when "fields" then "a,b,c"
30+
31+
utils =
32+
create_batch: (batch, up_buf, down_buf, nconf, cb) ->
33+
t.equal batch, "instructor-batch", "correct batch"
34+
t.equal up_buf, "CREATE INDEX instructors_a_b_c_idx ON instructors(a,b,c);", "create index"
35+
t.ok down_buf.match(/DROP INDEX instructors_a_b_c_idx;/), "drop index"
36+
37+
idx.inject utils
38+
idx.generate "instructor-batch", [], nconf, ->
39+
40+
t.end()
541

642
test "generate table", (t) ->
743
t.plan 4

0 commit comments

Comments
 (0)