且构网

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

Rails,PostgreSQL和历史触发器

更新时间:2022-12-17 19:58: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

其他所有内容都应照常进行(当然,假设您是

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.