How to Use Automigrate for ORM-less SQL Migrations

Aug 14, 2021 | Programming

Welcome to the world of Automigrate, a unique command-line tool designed for handling SQL migrations without the traditional overhead of ORM systems. In this article, we will explore what makes Automigrate stand out, how to use it effectively, and how to troubleshoot any challenges you may encounter along the way. Let’s dive in!

How is This Different?

Automigrate distinguishes itself by utilizing git history to manage database migrations. Instead of relying on the ORM definitions stored in a folder, Automigrate infers the necessary changes directly from the database state reflected in your Git repository. This means you just need to maintain a schema file with your SQL statements. For example:

sql-- schema.sql
create table whatever (
  userid uuid primary key,
  age_at_birth int default 0
);

Whenever a field is added to the `create table` statement, Automigrate computes the corresponding `alter table` statement, simplifying your migration process. Think of Automigrate as a skilled detective. Instead of rummaging through piles of folders (the traditional approach), it accurately reads the clues (your Git history) to figure out what needs to change in the database.

Usage

Getting started with Automigrate is straightforward. Here’s a step-by-step guide:

  • If you are using PostgreSQL:
  • pip install automig[postgres]
  • Set an environment variable with your PostgreSQL connection details:
  • export AUTOMIG_CON=postgresql:postgres:$PGPASSWORD@host
  • Initialize the PostgreSQL DB to schema*.sql: Run this command once to create your database:
  • automig_pg --glob schema*.sql init
  • Update your DB: Whenever your schema changes, run:
  • automig_pg --glob schema*.sql update
  • Do a dry-run: To see what changes will be made without applying them, use:
  • automig_pg --preview --glob schema*.sql update

If you’re using a different database, use the automig tool for SQL actions (documentation coming soon).

Advanced Features

Automigrate offers several advanced features to enhance your migration process:

  • Kube-native migrations: Instructions can be found in the kube folder.
  • Lambda support: Bundle a binary version of git compatible with amazonlinux. Seek assistance if needed.
  • Handling errors: Use --opaque mode to skip over problematic diffs (documentation pending).
  • Manual Overrides: Specify overrides for erroring diffs using the .manualmig.yml file. Check inside that file for an example.
  • Convert existing DBs: If you need this, please raise a GitHub issue for guidance.
  • Generate ORM definitions from SQL: Experiment with the experimental SQLAlchemy generator in sa_harness.py by running:
  • python -m automig.lib.sa_harness testschema*.sql

Troubleshooting

While using Automigrate, you might face some common issues. Here are few troubleshooting ideas:

  • Connection Problems: Ensure your environment variable for the database connection string is correctly set.
  • Migrations Not Applying: Double-check your schema files for syntax errors.
  • Git SHA Errors: Make sure that your git history is intact and reflects the required migrations.
  • If you can’t resolve an issue, consider reaching out for support in the respective repositories.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Now you’re equipped to leverage Automigrate for seamless SQL migrations. Whether you’re a seasoned developer or just starting out, Automigrate simplifies your workflow by letting you focus on code instead of managing migration files. Happy coding!

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox