Skip to content

Commit

Permalink
Plugins id is now a composite primary key
Browse files Browse the repository at this point in the history
  • Loading branch information
thibaultcha committed Feb 14, 2015
1 parent 1fc84a7 commit dfeb571
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,12 @@ local Migration = {
CREATE INDEX IF NOT EXISTS ON apis(target_url);
CREATE TABLE IF NOT EXISTS plugins(
id uuid,
api_id uuid,
application_id uuid,
name text,
value text, -- We can't use a map because we don't know if the value is a text, int or a list
created_at timestamp,
PRIMARY KEY (id)
PRIMARY KEY ((api_id, application_id, name))
);
CREATE INDEX IF NOT EXISTS ON plugins(api_id);
Expand Down
9 changes: 7 additions & 2 deletions src/apenode/dao/cassandra/base_dao.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ end
function BaseDao:insert(t)
if not t then return nil, "Cannot insert a nil element" end

-- Override id and created_at by default values
t.id = uuid()
-- Override created_at by default value
t.created_at = utils.get_utc() * 1000

-- Validate schema
Expand All @@ -59,6 +58,12 @@ function BaseDao:insert(t)
local value = t[column]

if schema_field.type == "id" then
if column == "id" then
-- If it is the inserted entity's id, we generate it
-- and attach it to the table for return value
t[column] = uuid()
value = t[column]
end
value = cassandra.uuid(value)
elseif schema_field.type == "timestamp" then
value = cassandra.timestamp(value)
Expand Down
5 changes: 2 additions & 3 deletions src/apenode/dao/cassandra/plugins.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
local BaseDao = require "apenode.dao.cassandra.base_dao"

local SCHEMA = {
{ _ = "id", type = "id" },
{ _ = "api_id", type = "id", required = true, exists = true },
{ _ = "application_id", type = "id", exists = true },
{ _ = "name", required = true },
Expand All @@ -15,8 +14,8 @@ function Plugins:new(database, properties)
self._schema = SCHEMA
self._queries = {
insert = [[
INSERT INTO plugins(id, api_id, application_id, name, value, created_at)
VALUES(?, ?, ?, ?, ?, ?);
INSERT INTO plugins(api_id, application_id, name, value, created_at)
VALUES(?, ?, ?, ?, ?);
]],
exists = {
application_id = [[ SELECT id FROM applications WHERE id = ?; ]],
Expand Down

0 comments on commit dfeb571

Please sign in to comment.