Back to blog

How to plan and execute database migration successfully with the new Appwrite CLI

Learn how to use the new Appwrite CLI to migrate your databases.

Database migration is a critical task in the lifecycle of any application. It involves making schema changes while ensuring that data remains intact, often to accommodate new features, improve performance, or ensure scalability. With the release of the new Appwrite CLI, the process of planning and executing database migrations has become significantly easier. The new CLI features allow you to manage your database schemas more efficiently, ensuring smooth transitions and minimal downtime.

The new Appwrite CLI

The new Appwrite CLI introduces several enhancements to improve the developer experience in managing database schemas. One of the standout features is the ability to use your appwrite.json file as the source of truth for your database collections' schema. This approach brings numerous benefits, including better change tracking, seamless migrations using GitOps, and simplified project replication. Let's explore these advantages in more detail:

1. Tracking changes

By using appwrite.json as the source for your collections schema, you can easily track changes over time. This file can be version-controlled with Git, allowing you to see a detailed history of modifications and collaborate effectively with your team.

2. Migrating database changes with GitOps

GitOps is a methodology that uses Git repositories to manage and deploy infrastructure changes. The new Appwrite CLI allows you to leverage GitOps for database schema changes, ensuring a smooth and consistent migration process across different environments.

3. Easy project replication

When you need to replicate a project, having the database schema defined in appwrite.json allows you to recreate the database setup effortlessly in different instances or environments, as well as in different projects within the same environment and instance.

Step-by-step guide to database migration with the new Appwrite CLI

Let's walk through the process of planning and executing a database migration using the new Appwrite CLI.

Step 1: Pull existing database configuration

If you already have a database setup, start by pulling the current configuration into your appwrite.json file. This step ensures that you have the latest schema as a baseline.

Bash
appwrite pull collections --all

Step 2: Creating and pushing collections

If you are starting from scratch, you can initialize a new collection and push it to Appwrite.

  1. Create a new collection:

    Bash
    appwrite init collection
    

  2. Push the collection to Appwrite:

    Bash
    appwrite push collections
    

Step 3: Modifying collections

To make changes to your collections, edit the appwrite.json file. For instance, you might want to change an attribute's type or add a new attribute. Here’s an example:

Original attributes

JSON
"attributes": [
  {
    "key": "a",
    "type": "integer",
    "status": "available",
    "error": "",
    "required": false,
    "array": false,
    "min": 1,
    "max": 100,
    "default": null}
]

Modified attributes

JSON
"attributes": [
  {
    "key": "a",
    "type": "string",
    "status": "available",
    "error": "",
    "required": false,
    "array": false,
    "min": 1,
    "max": 100,
    "default": null},
  {
    "key": "phone",
    "type": "integer",
    "status": "available",
    "error": "",
    "required": false,
    "array": false,
    "min": 1,
    "max": 100,
    "default": null}
]

Step 4: Pushing changes

After making the necessary changes, push the updated schema to Appwrite:

Bash
appwrite push collections

The Appwrite CLI will display a table summarizing all pending changes, allowing you to review them before applying. This ensures transparency and gives you a chance to verify changes before they take effect.

Executing database migrations

Step 5: Confirming changes

To apply the changes, type YES to confirm. This step ensures that no changes are made accidentally and gives you full control over the migration process. In the case of CI/CD pipelines, you can use the --force flag to push the changes automatically; however, exercise caution when using this option.

Conclusion

The new Appwrite CLI makes database migration a more manageable and streamlined process. By using appwrite.json as your schema’s source of truth, you can take advantage of better change tracking, seamless GitOps-based migrations, and simplified project replication. Whether you are starting from scratch or managing an existing database, these features enhance your ability to plan and execute migrations successfully.

Subscribe to our newsletter

Sign up to our company blog and get the latest insights from Appwrite. Learn more about engineering, product design, building community, and tips & tricks for using Appwrite.