Skip to content

Commit

Permalink
feat(db) add operation to truncate single table in new DAO
Browse files Browse the repository at this point in the history
Introduces the ability to truncate a single table in the new DAO, using
either `entities:truncate()` or `db.truncate("entities")`.

Adjusts all tests to avoid truncating all tables at once deliberately.
This is done to speed up the test suite.

Note that the tests still truncate the entire database once at the
beginning of each file when they use `helpers.get_db_utils` in the
top-level `setup` directive.
  • Loading branch information
hishamhm authored and thibaultcha committed Aug 2, 2018
1 parent b7611f0 commit e5d2113
Show file tree
Hide file tree
Showing 40 changed files with 319 additions and 186 deletions.
3 changes: 1 addition & 2 deletions kong/dao/factory.lua
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,7 @@ local function create_legacy_wrappers(self, constraints)

truncate = function(_)
log.debug(debug.traceback("[legacy wrapper] using legacy wrapper"))
log.err("[legacy wrapper] truncate not implemented")
return nil
return new_dao:truncate()
end,
}
end
Expand Down
5 changes: 5 additions & 0 deletions kong/db/dao/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,11 @@ function _M.new(db, schema, strategy, errors)
end


function DAO:truncate()
return self.strategy:truncate()
end


function DAO:select(primary_key)
if type(primary_key) ~= "table" then
error("primary_key must be a table", 2)
Expand Down
14 changes: 12 additions & 2 deletions kong/db/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,18 @@ function DB:reset()
end


function DB:truncate()
local ok, err = self.connector:truncate()
function DB:truncate(table_name)
if table_name ~= nil and type(table_name) ~= "string" then
error("table_name must be a string", 2)
end
local ok, err

if table_name then
ok, err = self.connector:truncate_table(table_name)
else
ok, err = self.connector:truncate()
end

if not ok then
return nil, prefix_err(self, err)
end
Expand Down
24 changes: 24 additions & 0 deletions kong/db/strategies/cassandra/connector.lua
Original file line number Diff line number Diff line change
Expand Up @@ -301,4 +301,28 @@ function CassandraConnector:truncate()
end


function CassandraConnector:truncate_table(table_name)
local ok, err = self:connect()
if not ok then
return nil, err
end

local cql = string.format("TRUNCATE TABLE %s.%s",
self.keyspace, table_name)

local ok, err = self:query(cql)
if not ok then
self:setkeepalive()
return nil, err
end

ok, err = self:setkeepalive()
if not ok then
return nil, err
end

return true
end


return CassandraConnector
5 changes: 5 additions & 0 deletions kong/db/strategies/cassandra/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -986,4 +986,9 @@ function _mt:delete_by_field(field_name, field_value)
end


function _mt:truncate()
return self.connector:truncate_table(self.schema.name)
end


return _M
14 changes: 14 additions & 0 deletions kong/db/strategies/postgres/connector.lua
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,20 @@ function _mt:truncate()
end


function _mt:truncate_table(table_name)
local truncate_statement = {
"TRUNCATE TABLE ", table_name, " RESTART IDENTITY CASCADE;"
}

local ok, err = self:query(truncate_statement)
if not ok then
return nil, err
end

return true
end


local _M = {}


Expand Down
4 changes: 2 additions & 2 deletions kong/db/strategies/postgres/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ end


function _mt:truncate()
local res, err = execute(self, "drop")
local res, err = execute(self, "truncate")
if not res then
return toerror(self, err)
end
Expand Down Expand Up @@ -1276,7 +1276,7 @@ function _M.new(connector, schema, errors)
}

local truncate_statement = concat {
"TRUNCATE ", table_name_escaped, ";"
"TRUNCATE TABLE ", table_name_escaped, " RESTART IDENTITY CASCADE;"
}

local drop_statement
Expand Down
14 changes: 6 additions & 8 deletions spec-old-api/02-integration/03-dao/04-constraints_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ for _, strategy in helpers.each_strategy() do
bp, db, dao = helpers.get_db_utils(strategy)
apis = dao.apis
plugins = dao.plugins
assert(dao:run_migrations())

dao:truncate_tables()
assert(db:truncate())
end)
before_each(function()
plugin_fixture = utils.shallow_copy(plugin_tbl)
Expand All @@ -34,8 +30,9 @@ for _, strategy in helpers.each_strategy() do
api_fixture = api
end)
after_each(function()
dao:truncate_tables()
assert(db:truncate())
dao:truncate_table("apis")
dao:truncate_table("plugins")
assert(db:truncate("consumers"))
end)

-- Check behavior just in case
Expand Down Expand Up @@ -177,8 +174,9 @@ for _, strategy in helpers.each_strategy() do
assert.falsy(err)
end)
after_each(function()
dao:truncate_tables()
assert(db:truncate())
dao:truncate_table("apis")
dao:truncate_table("plugins")
assert(db:truncate("consumers"))
end)

it("delete", function()
Expand Down
10 changes: 5 additions & 5 deletions spec-old-api/02-integration/03-dao/05-use_cases_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ local helpers = require "spec.helpers"

for _, strategy in helpers.each_strategy() do
describe("Real use-cases with DB: #" .. strategy, function()
local dao
local bp
local bp, db, dao
setup(function()
local _
bp, _, dao = helpers.get_db_utils(strategy)
bp, db, dao = helpers.get_db_utils(strategy)
end)

before_each(function()
dao:truncate_tables()
dao:truncate_table("apis")
dao:truncate_table("plugins")
db:truncate("consumers")
end)

it("retrieves plugins for plugins_iterator", function()
Expand Down
41 changes: 20 additions & 21 deletions spec-old-api/02-integration/04-admin_api/02-apis_routes_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ local cjson = require "cjson"
local utils = require "kong.tools.utils"

local dao_helpers = require "spec.02-integration.03-dao.helpers"
local DAOFactory = require "kong.dao.factory"
local DB = require "kong.db"

local function it_content_types(title, fn)
local test_form_encoded = fn("application/x-www-form-urlencoded")
Expand All @@ -16,18 +14,10 @@ end
dao_helpers.for_each_dao(function(kong_config)
describe("Admin API #" .. kong_config.database, function()
local client
local dao
local db
local dao, _

setup(function()
db = assert(DB.new(kong_config))
assert(db:init_connector())

dao = assert(DAOFactory.new(kong_config, db))
assert(dao:run_migrations())

dao:truncate_tables()
db:truncate()
_, _, dao = helpers.get_db_utils(kong_config.database)

assert(helpers.start_kong{
database = kong_config.database
Expand All @@ -40,7 +30,8 @@ describe("Admin API #" .. kong_config.database, function()
describe("/apis", function()
describe("POST", function()
before_each(function()
dao:truncate_tables()
dao:truncate_table("apis")
dao:truncate_table("plugins")
client = assert(helpers.admin_client())
end)
after_each(function()
Expand Down Expand Up @@ -175,7 +166,8 @@ describe("Admin API #" .. kong_config.database, function()

describe("PUT", function()
before_each(function()
dao:truncate_tables()
dao:truncate_table("apis")
dao:truncate_table("plugins")
client = assert(helpers.admin_client())
end)
after_each(function()
Expand Down Expand Up @@ -352,7 +344,8 @@ describe("Admin API #" .. kong_config.database, function()

describe("GET", function()
setup(function()
dao:truncate_tables()
dao:truncate_table("apis")
dao:truncate_table("plugins")

for i = 1, 10 do
assert(dao.apis:insert {
Expand All @@ -363,7 +356,8 @@ describe("Admin API #" .. kong_config.database, function()
end
end)
teardown(function()
dao:truncate_tables()
dao:truncate_table("apis")
dao:truncate_table("plugins")
end)
before_each(function()
client = assert(helpers.admin_client())
Expand Down Expand Up @@ -435,7 +429,8 @@ describe("Admin API #" .. kong_config.database, function()

describe("empty results", function()
setup(function()
dao:truncate_tables()
dao:truncate_table("apis")
dao:truncate_table("plugins")
end)

it("data property is an empty array", function()
Expand All @@ -452,7 +447,8 @@ describe("Admin API #" .. kong_config.database, function()

describe("DELETE", function()
before_each(function()
dao:truncate_tables()
dao:truncate_table("apis")
dao:truncate_table("plugins")
client = assert(helpers.admin_client())
end)
after_each(function()
Expand All @@ -479,7 +475,8 @@ describe("Admin API #" .. kong_config.database, function()
describe("/apis/{api}", function()
local api
setup(function()
dao:truncate_tables()
dao:truncate_table("apis")
dao:truncate_table("plugins")
end)
before_each(function()
api = assert(dao.apis:insert {
Expand All @@ -489,7 +486,8 @@ describe("Admin API #" .. kong_config.database, function()
})
end)
after_each(function()
dao:truncate_tables()
dao:truncate_table("apis")
dao:truncate_table("plugins")
end)

describe("GET", function()
Expand Down Expand Up @@ -739,7 +737,8 @@ describe("Admin API #" .. kong_config.database, function()
describe("/apis/{api}/plugins", function()
local api
setup(function()
dao:truncate_tables()
dao:truncate_table("apis")
dao:truncate_table("plugins")

api = assert(dao.apis:insert {
name = "my-api",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ describe("Admin API", function()
assert(helpers.start_kong())
client = assert(helpers.admin_client())

helpers.dao:truncate_tables()
helpers.dao:truncate_table("upstreams")
helpers.dao:truncate_table("targets")

upstream = assert(helpers.dao.upstreams:insert {
name = upstream_name,
Expand Down
10 changes: 6 additions & 4 deletions spec-old-api/02-integration/05-proxy/01-router_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ local function insert_apis(arr)
return error("expected arg #1 to be a table", 2)
end

helpers.dao:truncate_tables()
helpers.dao:truncate_table("apis")

for i = 1, #arr do
assert(helpers.dao.apis:insert(arr[i]))
Expand All @@ -29,7 +29,9 @@ describe("Router", function()
describe("no APIs match", function()

setup(function()
helpers.dao:truncate_tables()
helpers.dao:truncate_table("apis")
helpers.db:truncate("routes")
helpers.db:truncate("services")
helpers.dao:run_migrations()
assert(helpers.start_kong())
end)
Expand Down Expand Up @@ -180,7 +182,7 @@ describe("Router", function()

describe("URI regexes order of evaluation", function()
setup(function()
helpers.dao:truncate_tables()
helpers.dao:truncate_table("apis")

assert(helpers.dao.apis:insert {
name = "api-1",
Expand Down Expand Up @@ -714,7 +716,7 @@ describe("Router", function()
}

setup(function()
helpers.dao:truncate_tables()
helpers.dao:truncate_table("apis")

for i, args in ipairs(checks) do
assert(helpers.dao.apis:insert {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ local function insert_apis(arr)
return error("expected arg #1 to be a table", 2)
end

helpers.dao:truncate_tables()
helpers.dao:truncate_table("apis")

for i = 1, #arr do
assert(helpers.dao.apis:insert(arr[i]))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ describe("Plugins triggering", function()
end

helpers.stop_kong()
helpers.dao:truncate_tables()
helpers.dao:truncate_table("apis")
helpers.dao:truncate_table("plugins")


do
Expand Down Expand Up @@ -389,7 +390,8 @@ describe("Plugins triggering", function()

helpers.stop_kong()

helpers.dao:truncate_tables()
helpers.dao:truncate_table("apis")
helpers.dao:truncate_table("plugins")

local api = assert(helpers.dao.apis:insert {
name = "example",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe("Proxy errors Content-Type", function()
local client

setup(function()
helpers.dao:truncate_tables()
helpers.dao:truncate_table("apis")

assert(helpers.dao.apis:insert {
name = "api-1",
Expand Down
Loading

0 comments on commit e5d2113

Please sign in to comment.