Skip to content

bograh/db_migrate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Database Migration Tool

A comprehensive PostgreSQL database migration suite for migrating data between databases while handling foreign key dependencies and identity columns.

🚀 Features

  • Complete Database Migration: Migrate all tables with proper foreign key dependency ordering
  • Identity Column Handling: Proper handling of PostgreSQL identity/serial columns
  • Foreign Key Management: Automatic dependency resolution and constraint handling
  • Progress Tracking: Real-time migration progress with detailed statistics

📁 Project Structure

├── main.go                     # Main database migration tool
├── drop/
│   └── drop.go                 # Drop all the tables in the target database
├── go.mod                      # Go module dependencies
├── go.sum                      # Go module checksums
├── .env                        # Environment configuration
├── env.example                 # Environment configuration example
└── README.md                   # This file

🛠️ Prerequisites

  • Go 1.21 or higher
  • PostgreSQL databases (source and target)

📦 Installation

  1. Clone the repository:
git clone https://github.com/bograh/db_migrate.git
cd db_migrate
  1. Install dependencies:
go mod tidy
  1. Create a .env file with your configuration:
# Source Database Configuration
SOURCE_DB_HOST=
SOURCE_DB_PORT=
SOURCE_DB_USER=
SOURCE_DB_PASSWORD=
SOURCE_DB_NAME=
SOURCE_DB_SSL=disable

# Target Database Configuration
TARGET_DB_HOST=
TARGET_DB_PORT=
TARGET_DB_USER=
TARGET_DB_PASSWORD=
TARGET_DB_NAME=
TARGET_DB_SSL=disable

🔄 Usage

1. Database Migration

Migrate complete database from source to target:

go run main.go

Features:

  • Migrates tables in dependency order
  • Handles foreign key constraints
  • Preserves identity column values
  • Provides detailed migration statistics
  • Continues on errors with other tables

🔧 Configuration

Environment Variables

Variable Description Default
SOURCE_DB_HOST Source database host localhost
SOURCE_DB_PORT Source database port 5432
SOURCE_DB_USER Source database user postgres
SOURCE_DB_PASSWORD Source database password ``
SOURCE_DB_NAME Source database name source_db
SOURCE_DB_SSL Source SSL mode disable
TARGET_DB_HOST Target database host localhost
TARGET_DB_PORT Target database port 5432
TARGET_DB_USER Target database user postgres
TARGET_DB_PASSWORD Target database password ``
TARGET_DB_NAME Target database name target_db
TARGET_DB_SSL Target SSL mode disable

📈 Migration Statistics

The tool provides detailed statistics including:

  • Tables migrated
  • Rows migrated per table
  • Migration duration per table
  • Total migration time
  • Error summary

Example output:

================================================================================
MIGRATION SUMMARY
================================================================================
Table                               Rows     Duration Status
--------------------------------------------------------------------------------
tbl_1                            5       125ms    SUCCESS
tbl_2                            5       125ms    SUCCESS
tbl_3                            5       125ms    SUCCESS
tbl_4                            5       125ms    SUCCESS
tbl_5                            5       125ms    SUCCESS
...
--------------------------------------------------------------------------------
TOTAL                             25      650ms
================================================================================

⚠️ Important Notes

  1. Backup your target database before running migrations
  2. Foreign key constraints are temporarily disabled during migration
  3. Identity columns are handled with OVERRIDING SYSTEM VALUE
  4. Sequences are reset to match migrated data
  5. The tool continues on errors but reports them in the summary

🐛 Troubleshooting

Common Issues

  1. Connection failures: Check database credentials and network connectivity
  2. Permission errors: Ensure database user has appropriate privileges
  3. Foreign key violations: Tables are migrated in dependency order, but check for circular references
  4. Identity column issues: The tool uses OVERRIDING SYSTEM VALUE to handle these automatically

Error Handling

  • The migration continues even if individual tables fail
  • All errors are logged and reported in the final summary
  • Check the console output for detailed error messages

📝 License

This project is licensed under the MIT License - see the LICENSE file for details.

🤝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests if applicable
  5. Submit a pull request

📞 Support

For issues and questions:

  1. Open an issue in the repository
  2. Review the error logs

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages