Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
liyang0918 opened this issue Jun 14, 2024 · 2 comments
Open
Assignees
Labels
type:critical critical questions type:feature_request feature request

Comments

@liyang0918
Copy link

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的官方文档首例展示了使用AutoMigrate功能来自动匹配数据库表结构与Go结构体,这一做法虽简化了快速原型开发,但可能无意中鼓励了不恰当的数据库管理实践,尤其是在生产环境中。
  • 运维挑战: 直接在业务代码中变更数据库结构对数据库运维人员构成了挑战,理想情况下,数据库架构的变更应经过严格测试和审批流程。
  • 新手陷阱: 初学者可能未完全理解文档细节,直接复制示例代码,未来可能因不经意的AutoMigrate调用导致生产环境数据结构的意外变动。

改进建议:

  • 模块化AutoMigrate: 将AutoMigrate从GORM核心功能中分离,设计为一个可选插件。这样,开发者需主动选择安装和使用该插件,从而强化对数据库变更的意识和控制。
  • 提供禁用机制: 引入一个明确的配置选项,允许项目层面彻底禁用AutoMigrate功能,以防止任何意外的结构迁移操作,增强团队协作的安全性。
  • 文档指导: 更新官方文档,强调数据库迁移的最佳实践,指导用户采用更安全的数据库迁移策略,如使用版本控制的迁移脚本。

理由:

  • 提升安全性: 明确分离结构定义与结构迁移,减少生产环境中的潜在风险。
  • 增强团队协作: 有助于团队成员之间建立一致的数据库管理标准,减少因个人操作差异导致的混乱。
  • 教育引导: 强化对数据库管理最佳实践的教育,特别是对新手开发者,培养良好的开发习惯。

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

@github-actions github-actions bot added type:critical critical questions type:feature_request feature request labels Jun 14, 2024
@liyang0918
Copy link
Author

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

@jinzhu
Copy link
Member

jinzhu commented Jun 25, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:critical critical questions type:feature_request feature request
Projects
None yet
Development

No branches or pull requests

2 participants