Package: migrado
Migrado🥑 ArangoDB migrations and batch processing manager.Migrado is a command-line client that can help build and run schema or data migrations against your ArangoDB instance.Migrado utilizes ArangoDB Transactions when running data migrations to ensure failed scripts are rolled back automatically.arangoshfrom theArangoDB Client Toolsis required to run schema migrations, however no transaction safety is available at this point.Migrado should be considered beta software,but it is well tested, and used in production settings. Make sure you understand how it operates.If you have trouble, open an issue. Contributions are welcome.InstallationMigrado requires Python 3.6 or higher, and the ArangoDBarangoshclient.$pipinstall--usermigradoIt is also available as a Docker image, seeDocker usage.UsageMigrado can create a migrations directory and generate an initial set of collections from a given schema file:$migradoinit--schemaschema.ymlMigrado can also construct an initial migration from the current database structure (and automatically store it as the current state/schema):$migradoinit--inferSeeYAML schemasfor details. If neither option is specified, Migrado will create an empty initial migration.To autogenerate a schema migration script based on an updated schema:$migradomake--schemaupdated_schema.ymlTo make a new template data migration script:$migradomake--namerewrite_field_namesThis will create a new file,migrations/0002_rewrite_field_names.js(--nameis optional), which you can edit as you see fit. SeeMigration scriptsfor details.When you are ready, run all migrations (not previously ran) against the database:$migradorunMigrado stores migration state in a configurable collection, see--helporEnvironment varsfor details.If you wrote areverse()migration, you can revert to an earlier point by specifying a target migration id. To revert to the initial migration:$migradorun--target0001You can inspect the current migration state with:$migradoinspectYou can inspect the current schema (explicit or inferred) with:$migradoexportUse the--helpoption for help on any command when using the client.Docker usageIf you're using Migrado in a Docker context, you might as well use theDocker image.migradois set as entrypoint, so the image can be used like the Python client:$dockerrunprotojour/migrado--helpYou'd want to volume in your migrations folder:$dockerrun-v/path/to/migrations:/app/migrationsprotojour/migradoOr, an example using docker-compose:migrado:image:protojour/migrado:latestenvironment:MIGRADO_DB:# ...MIGRADO_HOST:# ...volumes:-./migrations:/app/migrationsThen either add acommand:(with a migrado sub-command, e.g.command: run ...), or use this as a starting point for a scripted migration strategy.You may also use the baseDockerfileas a starting point.Environment varsThe following environment variables are employed by Migrado:MIGRADO_PATH: Specifies the path to the migrations directory, replaces-p,--path(default:migrations).MIGRADO_DB: Specifies the ArangoDB database name for generated migrations to interact with, replaces-d,--db(no default, but required for theruncommand).MIGRADO_COLL: Specifies ArangoDb collection name to store migration state in, replaces-c,--state-coll(default:migrado).MIGRADO_TLS: Use TLS for connection when running migrations, replaces-T,--tls(default:False).MIGRADO_HOST: Specifies the database host for running migrations, replaces-H,--host(default:localhost).MIGRADO_PORT: Specifies the database port for running migrations, replaces-P,--port(default:8529).MIGRADO_USER: Specifies the database username for running migrations, replaces-U,--username(no default).MIGRADO_PASS: Specifies the database password for running migrations, replaces-W,--password(no default).YAML schemasArangoDB may be schemaless, but in a larger project it still makes sense to keep a schema spec up to date, both for an overview of collections and their data structures, and as a basis fornative collection-level validation(see the-v/--validationoption).Migrado uses a schema model based on JSON Schema, in YAML, and can use this to generate an initial migration for the collections available in your database.Example schema:all:&all_id:type:stringreadOnly:true_key:type:stringreadOnly:true_rev:type:stringreadOnly:trueedges:&edges_from:type:string_to:type:stringcollections:books:type:objectproperties:<<:*alltitle:type:stringisbn:type:stringrequired:-title-isbnauthors:# Note: you do not actually need to specify an object schema,# but the schema can be used with ArangoDB's native validation# using the -v/--validation optionedge_collections:# authors --> booksauthor_of:type:objectproperties:<<:*all<<:*edgesrequired:-_from-_toMigration scriptsMigration scripts are structured so they may be parsed and run easily by both Migrado and ArangoDB. In addition, they are structured so they may be run manually against ArangoDB usingarangosh.There are two types of script,dataandschemamigration scripts.Data migrationsYou need to declare all collections subject to write operations using the syntax// write collection_name, because ArangoDB needs this information for locking during transactions. We've made the declaration explicit to reduce errors.Attempting to write to collections not declared in this way will cause the migration to fail.In general, a reverse migration should do the logical opposite of a forward migration.forward()andreverse()functions can contain anything that the ArangoDB V8 engine understands, but must be fully self-contained.Anything outside these functions is ignored and unavailable when running migrations.Here's an example migration script for addingnew_fieldin collectionthings:// write thingsfunctionforward(){vardb=require("@arangodb").dbdb._query(`FOR thing IN thingsUPDATE thing WITH { new_field: "some value" } IN things`)}functionreverse(){vardb=require("@arangodb").dbdb._query(`FOR thing IN thingsREPLACE thing WITH UNSET(thing, "new_field") IN things`)}Please make sure you readlimitations when running transactionsin the ArangoDB documentation. In particular,creation and deletion of databases, collections, and indexesis not allowed in transactions.Schema migrationsSchema migrations are stuctured in the same way as data migrations, but are run againstarangoshas opposed to the HTTP API. There is no transaction safety when running schema migrations.Schema migrations are structured the same way as data migrations, but// writedeclarations are not required. All operations are allowed.Here's an example migration script generated from the YAML schema above (with no validation):functionforward(){vardb=require("@arangodb").dbvarschema=// schema to be written to diskdb._create("books",{},"document")db._create("authors",{},"document")db._create("author_of",{},"edge")}functionreverse(){vardb=require("@arangodb").dbdb._drop("books")db._drop("authors")db._drop("author_of")}Please be careful when running schema migrations in reverse. As you can see, thereverse()function above would drop your collections (and lose your data) if you were to reverse beyond this point. Currently, you will not be able to do so for an initial migration.LicenseMigrado is copyright © 2019 Protojour AS, and is licensed under MIT. SeeLICENSE.txtfor details.