go-gorm / gorm

The fantastic ORM library for Golang, aims to be developer friendly
https://gorm.io
MIT License
36.91k stars 3.93k forks source link

Proposal: Enable Optional Use of AutoMigrate via Modular Plugin System #7067

Open liyang0918 opened 4 months ago

liyang0918 commented 4 months ago

Describe the feature

The proposed feature involves refactoring the AutoMigrate capability in GORM to operate as an advanced, optional module instead of being a core integrated function. This change would allow developers to import and utilize the AutoMigrate feature selectively, similar to how database drivers are included, providing a more controlled and intentional approach to database schema evolution.

Motivation

The motivation stems from several key considerations:

  1. Best Practices in Database Administration: Encouraging a clear separation of concerns between application code and database schema management aligns better with professional database administration practices. Allowing AutoMigrate as a plug-in emphasizes the importance of controlled schema changes through dedicated migration scripts or tools.
  2. Mitigating Accidental Changes: By making AutoMigrate optional, developers are prompted to think critically about when and how they modify database structures, reducing the likelihood of accidental schema updates in production environments, which can be detrimental and difficult to revert.
  3. Supporting Developer Education: Given that the official GORM documentation's first demonstration includes AutoMigrate, this change could be accompanied by updated guidance emphasizing the importance of managed schema migrations. This would help educate developers, especially beginners, on safer database management techniques.
  4. Team Collaboration and Safety: In a team environment, having a mechanism to easily disable AutoMigrate at the project level can prevent unintended migrations triggered by other team members unfamiliar with the implications, enhancing overall codebase safety.

Related Issues

This suggestion aims to address the broader topic of database migration strategies within ORM usage and does not directly link to a specific open issue. However, it is relevant to discussions around best practices in ORM design, version control for database schemas, and the balance between developer convenience and system stability.

对于母语为汉语的我来说,可能用中文更适合描述我的问题和担忧:

尊敬的GORM团队,

首先,感谢你们为Go社区提供了如此强大且易用的ORM工具。我在此提出一项建议,旨在进一步增强GORM的灵活性和对数据库管理最佳实践的支持。

问题背景:

改进建议:

理由:

我相信,通过上述改进,GORM不仅能够维持其易用性,还能在促进更专业、更安全的数据库管理实践中发挥重要作用。期待您们的考虑,并感谢您们对Go生态的持续贡献。

liyang0918 commented 4 months ago

这个方案旨在降低Gorm使用的潜在风险,以及对数据库管理的规范化,希望作者能一起讨论 @jinzhu

jinzhu commented 4 months ago

AutoMigration is more recommended for quick testing in a development environment. Additionally, we have organized migration-related documentation here https://gorm.io/docs/migration.html . Feel free to refer to or update the relevant information.