向PostgreSQL数据库添加附加信息的适当方法(seeds.rb)(Proper methodology in adding additional info to a PostgreSQL dat

编程入门 行业动态 更新时间:2024-10-26 20:22:38
向PostgreSQL数据库添加附加信息的适当方法(seeds.rb)(Proper methodology in adding additional info to a PostgreSQL database (seeds.rb))

从概念上讲,我明白seeds.rb代表什么。 这是一种在应用程序运行之前持有处理填充数据库逻辑的脚本的手段。

但是,从我所知道的情况来看,“更新”seeds.rb是不太可能的。 它仅用于应用程序所需的绝对基础数据,因此如果需要更新它,则必须在运行seed命令之前完全清除数据库。

这使我想到了这个问题......如果我想最初为它提供一些信息,然后想要在未来添加记录呢? 似乎没有一个很好的编程方式,我已经看到使用迁移代码的警告。

Heroku提供了管理数据库的手动方法,无论是删除还是添加新记录,但我认为这种干预应该是绝对的最后手段。

道歉,如果这是一个愚蠢的问题,但我很难搜索这个特定的查询。

Conceptually, I understand what seeds.rb represents. It's a means to hold a script that handles the logic of populating a database before the application should be run.

However, from what I can tell, it's not really possible to "update" seeds.rb. It's only meant for the absolute base data required for the application, so if it needs to be updated, you'd have to clear our the database entirely before running the seed command.

This leads me to the question... what if I wanted to seed it initially with some information, then wanted to subsequently add records in the future? There doesn't seem to be a good programmatic way of doing this, and I've seen warning against doing so using migration code.

Heroku offers manual means of managing a database, be it removing or adding new records, but this intervention, I think, should be an absolute last resort.

Apologies if this is a dumb question, but I'm pretty bad at searching this particular inquiry.

最满意答案

我们通过运行db/data和migrations目录来解决这个问题,该目录将从数据目录加载最新文件并将其加载到数据库中。

例如,我们这样做是为了让我们的邮政编码保持最新。 每当我们找到缺失的邮政编码时,我们将其添加到db/data/postcodes.yaml文件中,然后创建一个迁移,该迁移运行从文件加载邮政编码的rake任务。

数据目录位于git中,因此您可以随时间看到数据更改,并且当前的db/data/postcodes.yaml始终是最新的版本。

在这种情况下,rake任务会读取文件并执行find_or_create_by邮编(如果找到现有的find_or_create_by则更新)。

We solve this problem by having a directory of db/data and migrations run that will load the latest file from the data directory and load it into the db.

eg we do this for keeping our set of postal codes up to date. Whenever we identify a missing postcode, we add it to the db/data/postcodes.yaml file then create a migration that runs the rake-task that loads the postcodes from the file.

The data directory is in git, so you can see the data-changes over time, and the current db/data/postcodes.yaml is always the most up-to-date version.

The rake task, in this case, reads through the file and does a find_or_create_by the postcode (updating if we find an existing one).

更多推荐

本文发布于:2023-08-06 13:50:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1451517.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库   方法   信息   rb   seeds

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!