Categorygithub.com/the-medo/golang-migrate-objects
modulepackage
0.0.0-20241031160601-66a021424ec5
Repository: https://github.com/the-medo/golang-migrate-objects.git
Documentation: pkg.go.dev

# README

golang-migrate-objects

Overview

golang-migrate-objects is a Go-based tool that extends golang-migrate to manage database objects. It allows for migrations of complex database objects by applying structured steps. It includes automatic generation of files for object creation and dropping, helping maintain consistency across versions.

Main features

  • Supports both up and down migrations with database objects.
  • Can automatically generate a "sum file" that merges the latest versions of DB objects.
  • Utilizes a configuration structure to define paths and database settings.
  • Supports command-line arguments to control migration actions and file management.

Project structure

  • main.go: Initializes configuration and manages command-line interface.
  • /migrator/migrator.go: Contains core migration logic for managing DB objects.
  • /migrator/types.go: Defines data structures used in the migrator.

Configuration Structure

Config Struct (in /migrator/types.go)

FieldTypeDescription
DB*sql.DBDatabase connection instance.
PriorityLpadintPadding length for priority fields.
VersionLpadintPadding length for version fields.
MigrationFilesPathstringPath to SQL migration files.
DbObjectPathstringPath to DB object files.
CreateObjectsFilenamestringFilename for creating merged sum files.
DropObjectsFilenamestringFilename for object drop scripts.

Usage instructions

  1. Set Up Migration Paths
    • mpath: Path to migration files.
    • obj_path: Path to DB object files.
    • db_source: Database connection string.
  2. Execute commands
    • Run commands with go run main.go with flags to define migration behavior: go run main.go -mpath=<migration_path> -obj_path=<object_path> -db_source=<db_source>

Available Command-Line Flags

FlagTypeDescription
-mpathstringPath to SQL migration files. Required
-obj_pathstringPath to DB object files. Required
-db_sourcestringDatabase connection string. Required
-co_filenamestringFilename for generating the sum file. Required
-do_filenamestringFilename for object drop scripts. Required
-sumfileboolIf set, creates a sum file of all object versions.
-upboolRuns migrations in the up direction.
-downboolRuns migrations in the down direction.
-stepintNumber of steps to migrate. If 0, runs all migrations.

Core Components

Migrator Struct (in /migrator/migrator.go)

FieldTypeDescription
Migrate*migrate.MigrateInstance for migration operations.
Config*ConfigConfiguration settings from the Config struct.

Methods in Migrator

  • GetObjectList: Returns a list of DB objects based on the directory structure.
  • GetObjectsForStep: Retrieves DB objects for a specific migration step, considering direction.
  • CreateObjectsForStep: Creates objects required for a specific step.
  • CreateObjectsFile: Merges latest versions of DB objects into a single file.
  • DropObjects: Executes a script to drop DB objects.
  • RunFile: Executes SQL file at the specified path.
  • RunAll: Migrates the database to the highest available version.

Additional Helper Functions

  • parseDir and parseVersionFiles: Parses directories and files to extract object versions.
  • orderDirEntries: Orders directory entries by name.

Example usage

  1. Run All Migrations Up
go run main.go -mpath="path/to/migrations" -obj_path="path/to/objects" -db_source="postgres://user:pass@host/db" -up
  1. Generate a Sum File
go run main.go -mpath="path/to/migrations" -obj_path="path/to/objects" -db_source="postgres://user:pass@host/db" -sumfile

Error Handling

The package includes several error checks, such as:

  • ErrDifferentPriorityLength: Ensures all priority values are of equal length.
  • ErrInvalidPriority: Ensures priority values are valid integers.
  • ErrDifferentVersionLength: Checks if all version numbers have consistent lengths.

# Packages

No description provided by the author