View Source ArangoXEcto.Migration (ArangoX Ecto v1.3.1)
Defines Ecto Migrations for ArangoDB
note
NOTEArangoXEcto dynamically creates collections for you by default. Depending on your project architecture you may decide to use static migrations instead in which case this module will be useful.
Migrations must use this module, otherwise migrations will not work. To do this, replace
use Ecto.Migration
with use ArangoXEcto.Migration
.
Since ArangoDB is schemaless, no fields need to be provided, only the collection name. First create
a collection struct using the collection/3
function. Then pass the collection struct to the
create/1
function. To create indexes it is a similar process using the index/3
function.
Order matters!! Make sure you create collections before indexes and views, and analyzers before views if they are used. In general this is a good order to follow:
Analyzers > Collections > Indexes > Views
To drop the collection on a migration down, do the same as creation except use the drop/1
function
instead of the create/1
function. Indexes are automatically removed when the collection is removed
and cannot be deleted using the drop/1
function.
example
Example
defmodule MyProject.Repo.Migrations.CreateUsers do
use ArangoXEcto.Migration
def up do
create(MyProject.Analyzers)
create(collection(:users))
create(index("users", [:email]))
create(MyProject.UsersView)
end
def down do
drop(collection(:users))
end
end
Link to this section Summary
Functions
Creates a collection struct
Creates an object
Deletes an object
Creates an edge collection struct
Creates an index struct
Link to this section Functions
@spec collection(String.t(), atom(), [ ArangoXEcto.Migration.Collection.collection_option() ]) :: ArangoXEcto.Migration.Collection.t()
Creates a collection struct
Used to in functions that perform actions on the database.
Accepts a collection type parameter that can either be :document
or :edge
, otherwise it will
raise an error. The default option is :document
.
options
Options
Accepts an options parameter as the third argument. For available keys please refer to the ArangoDB API doc.
examples
Examples
iex> collection("users")
%ArangoXEcto.Migration.Collection{name: "users", type: 2)
iex> collection("users", :edge)
%ArangoXEcto.Migration.Collection{name: "users", type: 3)
iex> collection("users", :document, keyOptions: %{type: :uuid})
%ArangoXEcto.Migration.Collection{name: "users", type: 2, keyOptions: %{type: :uuid})
@spec create( ArangoXEcto.Migration.Collection.t() | ArangoXEcto.Migration.Index.t() | atom(), Keyword.t() ) :: :ok | {:error, binary()}
Creates an object
Will create the passed object, one of a collection, an index, analyzers or a view.
Since view schemas and analyzers are just module definitions we can use them directly here. Therefore the module is just passed directly.
There is the ability to pass additional options as the second argument
options
Options
:repo
- The repo or connection to use for the migration create action:prefix
- The prefix to use for tenant creation
examples
Examples
Create a collection
iex> create(collection("users"))
:ok
Create an index
iex> create(index("users", [:email])
:ok
Create a view
iex> create(MyProject.UsersView)
:ok
Deletes an object
Will delete an object passed, can only be a collection, indexes cannot be deleted here. This is because indexes have a randomly generated id and this needs to be known to delete the index, for now this is outside the scope of this project.
example
Example
iex> drop(collection("users"))
:ok
@spec edge(String.t(), [ArangoXEcto.Migration.Collection.collection_option()]) :: ArangoXEcto.Migration.Collection.t()
Creates an edge collection struct
Same as passing :edge
as the second parameter to collection/3
.
@spec index(String.t(), [atom() | String.t()], [ ArangoXEcto.Migration.Index.index_option() ]) :: ArangoXEcto.Migration.Index.t()
Creates an index struct
Default index type is a hash. To change this pass the :type
option in options.
options
Options
Options only apply to the creation of indexes and has no effect when using the drop/1
function.
:type
- The type of index to create- Accepts:
:fulltext
,:geo
,:hash
,:persistent
,:skiplist
or:ttl
- Accepts:
:unique
- If the index should be unique, defaults to false (hash, persistent & skiplist only):sparse
- If index should be spares, defaults to false (hash, persistent & skiplist only):deduplication
- If duplication of array values should be turned off, defaults to true (hash & skiplist only):minLength
- Minimum character length of words to index (fulltext only):geoJson
- If a geo-spatial index on a location is constructed and geoJson is true, then the order within the array is longitude followed by latitude (geo only):expireAfter
- Time in seconds after a document's creation it should count asexpired
(ttl only):name
- The name of the index (usefull for phoenix constraints)
examples
Examples
Create index on email field
iex> index("users", [:email])
%ArangoXEcto.Migration.Index{collection_name: "users", fields: [:email]}
Create dual index on email and ph_number fields
iex> index("users", [:email, :ph_number])
%ArangoXEcto.Migration.Index{collection_name: "users", fields: [:email, :ph_number]}
Create unique email index
iex> index("users", [:email], unique: true)
%ArangoXEcto.Migration.Index{collection_name: "users", fields: [:email], unique: true}