且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Rails、PostgreSQL 和历史触发器

更新时间:2022-12-17 19:45:21

如果您需要或想要 ActiveRecord 不了解的特定于数据库的功能,那么您应该切换到 db/structure.sql跟踪您的架构.db/structure.sql 几乎是使用数据库的本机工具制作的模式的原始转储,因此它将包含触发器、CHECK 约束、函数结果索引以及其他所有内容.

If you need or want database-specific features that ActiveRecord doesn't understand then you should switch to db/structure.sql for keeping track of your schema. db/structure.sql is pretty much a raw dump of your schema made using the database's native tools so it will contain triggers, CHECK constraints, indexes on function results, and everything else.

切换很容易:

  1. 更新您的 config/application.rb 以包含 config.active_record.schema_format = :sql.
  2. 执行 rake db:structure:dump 以获得初始 db/structure.sql.
  3. 从目录树和版本控制中删除 db/schema.rb.
  4. db/structure.sql 添加到版本控制.
  5. 调整您的佣金习惯:
    • 使用 db:structure:dump 而不是 db:schema:dump
    • 使用 db:structure:load 而不是 db:schema:load
  1. Update your config/application.rb to contain config.active_record.schema_format = :sql.
  2. Do a rake db:structure:dump to get an initial db/structure.sql.
  3. Delete db/schema.rb from your directory tree and revision control.
  4. Add db/structure.sql to revision control.
  5. Adjust your rake habits:
    • Use db:structure:dump instead of db:schema:dump
    • Use db:structure:load instead of db:schema:load

其他一切都应该像往常一样工作(当然,假设您是理智的并且使用 PostgreSQL 进行开发、测试和生产).

Everything else should work as usual (assuming, of course, that you're sane and using PostgreSQL for development, testing, and production).

进行此更改后,您的触发器将在 db/structure.sql 中进行跟踪,并且重新创建数据库不会丢失它们.

With this change made, your triggers will be tracked in db/structure.sql and recreating the database won't lose them.