12

I am trying to put together a unit test setup with Arango. For that I need to be able to reset the test database around every test.

I know we can directly delete a database from the REST API but it is mentioned in the documentation that creation and deletion can "take a while".

Would that be the recommended way to do that kind of setup or is there an AQL statement to do something similar ?

Nicolas Joseph
  • 1,694
  • 3
  • 15
  • 25

4 Answers4

9

After some struggling with similar need I have found this solution:

for (let col of db._collections()) {

    if (!col.properties().isSystem) {
        db._drop(col._name);
    }
}
tlama
  • 607
  • 1
  • 10
  • 17
  • This sometimes times out for large collections. It works great for small collections, though. – mrog Aug 26 '20 at 21:16
7

You can for example retrieve the list of all collections (excluding system ones) and drop or truncate them. The latter will remove all documents and keep indexes. Alternatively you can use AQL REMOVE statement.

yojimbo87
  • 65,684
  • 25
  • 123
  • 131
  • Is it better to use the REST API endpoints you suggested or craft an AQL query that does the same thing ? – Nicolas Joseph Jan 18 '16 at 11:10
  • I'm not aware of support for dropping or truncating collections directly in AQL, however you can use this functionality through Arango shell - https://docs.arangodb.com/Collections/CollectionMethods.html – yojimbo87 Jan 18 '16 at 11:39
  • 1
    Using a single AQL query to drop all collections won't work, as AQL is for DML (data manipulation) only, but not for DDL (data definition). As such, AQL does not support a "drop collection" operation. So if you intended to do the create/drop from a client application, you will need to use the HTTP RESET APIs or the create/drop functionality provided by a client driver. – stj Jan 18 '16 at 11:59
  • 1
    It's worth noting that the links in the post don't actually lead to helpful places anymore. – Slater Victoroff Jan 15 '17 at 18:20
  • @SlaterTyranus Thanks for letting know. I updated the links. – yojimbo87 Jan 15 '17 at 20:06
3

Execute the following AQL query deletes all documents in the collection yourcollectionname:

FOR u IN yourcollectionname
  REMOVE u IN yourcollectionname

https://docs.arangodb.com/3.0/AQL/Operations/Remove.html

musemind
  • 1,027
  • 9
  • 8
2

Creation of databases may indeed take a while (a few seconds). If that's too expensive in a unit test setup that sets up and tears down the environment for each single test, there are the following options:

  • create and drop a dedicated test database only once per test suite (that contains multiple tests), and create/drop the required collections per test. This has turned out to be fast enough in many cases, but it depends on how many tests are contained in each test suite.

  • do not create and drop a dedicated test database, but only have each test create and drop the required collections. This is the fastest option, and should be good enough if you start each test run in a fresh database. However it requires the tests to clean everything up properly. This is normally no problem, because the tests will normally use dedicated collections anyway. An exception is there for graph data: creating a named graph will store the graph description in the _graphs collection, and the graph must be deleted from there again.

stj
  • 9,037
  • 19
  • 33