diff --git a/database/migrations/cassandra/2015-01-12-175310_init_schema.lua b/database/migrations/cassandra/2015-01-12-175310_init_schema.lua index b4e0c56ee5a7..6ca3f2ab7dd4 100644 --- a/database/migrations/cassandra/2015-01-12-175310_init_schema.lua +++ b/database/migrations/cassandra/2015-01-12-175310_init_schema.lua @@ -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); diff --git a/src/apenode/dao/cassandra/base_dao.lua b/src/apenode/dao/cassandra/base_dao.lua index 15cdbcae5c31..d922b9add9d0 100644 --- a/src/apenode/dao/cassandra/base_dao.lua +++ b/src/apenode/dao/cassandra/base_dao.lua @@ -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 @@ -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) diff --git a/src/apenode/dao/cassandra/plugins.lua b/src/apenode/dao/cassandra/plugins.lua index 871b412371d2..466707a268dc 100644 --- a/src/apenode/dao/cassandra/plugins.lua +++ b/src/apenode/dao/cassandra/plugins.lua @@ -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 }, @@ -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 = ?; ]],