GithubHelp home page GithubHelp logo

golang-multi-schema-migration's Introduction

Golang Multi Schema Migration

Use case

When creating a B2B app or more broadly an app where multiple users are split across multiple organizations, one way of securely segmenting data by organization is to have one schema per organization. This repo aims to solve the problem of database migrations for that multi-schema use case.

Why not use golang-migrate?

golang-migrate solves for a general use case well. But for my multi-schema use case, I prefer a clear segmentation in how they are created, stored, and deployed. Also, I prefer to store my migrations in .go files in case there may be a need to add go code into my migration (rare but has happened before).

How to use?

Create public and schema-specific migrations

The migrate binary can be used to create your migrations. The output directory is defined in cmd/migrate/main, as the variable const directory = "/migrations". Usage is shown below.

  $ ./cmd/migrate/migrate -h
    Usage of ./cmd/migrate/migrate:
      -isPublic
          set true if this migration is ran at the public level
      -name string
          the name of the migration (only lowercase and underscore)

How to migrate up and down

Use CreateMigrator(hostName, port, database, user, password string, lg *log.Logger) (*Migrator, error) in migrator.go. Migrator then exposes the following API.

// Public API
MigratePublicUpToLatest(ctx context.Context) error 
MigratePublicUpToDateString(ctx context.Context, dateString string) error 
MigratePublicDownFully(ctx context.Context) error
MigratePublicDownToDateString(ctx context.Context, dateString string) error 

// Schema API
MigrateSchemaUpToLatest(ctx context.Context, schema string) error 
MigrateSchemaUpToDateString(ctx context.Context, schema string, dateString string) error 
MigrateSchemaDownFully(ctx context.Context, schema string) error 
MigrateSchemaDownToDateString(ctx context.Context, schema string, dateString string) error

To summarize the functionality, for public and schema level migrations, Migrator lets you migrate up to the latest migration, up to a specific datestring, down to a specific date string, for down fully (undoing every migration including the first).

I intentionally don't expose running migrations on all schema in one go. I've seen that lead to service outages before, so if this is functionality you really want, implement it yourself with full knowledge of your infrastructure.

Testing migrations

Currently, migrations/public and migrations/schema have sample migrations. These migrations are used to test all of the API of Migrator in migration_test.go.

Run tests using $make integration_test Running tests will require docker and docker-compose. I'd personally recommend testing migrations using int tests like this, but rather than one test for all migrations, one test per migration.

Miscellaneous Notes

golang-multi-schema-migration is built with postgres in mind. It uses pgxpool from pgx to manage migrations. If you wish to work with other databases, you should be able to modify the code pretty easily.

golang-multi-schema-migration's People

Contributors

jugalde avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.