@0ti.me/postgres-db-versioning 中文文档教程

发布于 4年前 浏览 21 项目主页 更新于 3年前

PostgresDB Versioning

如果关系数据库的数据和表要随着时间的推移正确迁移,则必须对其进行版本控制。 该项目旨在生成一个 NPM 模块,该模块使用依赖于该模块的任何内容提供的配置和版本数据来完成繁重的工作。

Configuration

{
  "connectionConfig": {
    "user": "postgres",
    "password": "postgres",
    "host": "localhost",
    "database": "postgres",
    "port": 54231
  },
  "dryRun": false
  "eventHandler": (...eventArgs) => console.error(event),
  "migrations": [
    {"version": 1, "description": "create the table a_table", "sql": "CREATE TABLE a_table (id INT);"},
    {"version": 2, "description": "add the table another_table", "sql": "CREATE TABLE another_table (id INT);"}
  ],
  "pool": {
    query: sqlString => console.error(sqlString)
  },
  "requestedVersion": 2,
  "scratch": [
    "CREATE TABLE table_name"
  ],
  "tableNameForVersions": "DatabaseVersion",
  "version": 1
}

Connection Config

如果此模块应管理连接,则使用此 API 传入配置对象:https://node-postgres.com/api/client/。

Dry Run

启用此功能后,将要执行的步骤将作为事件发送到事件处理程序。

Event Handler

如果设置了此项,它将发出事件(用于查询的字符串或从将应用于查询函数调用的 (...args) 制作的数组。

例如,如果使用多个参数调用 eventHandler,例如: 'SELECT * FROM x WHERE xy = ?;' 和 ['fred'],那么当计划执行时,它会执行如下内容:

pool.query('SELECT * FROM x WHERE x.y = ?;', ['fred']);

如果仅使用字符串调用 eventHandler,例如 'SELECT * FROM x;' ,它会执行这样的事情:

pool.query('SELECT * FROM x');

Migrations

迁移应该是一个包含版本号的对象数组,一个可选的更改描述,以及应该执行的 sql。

Pool

如果包含,这将被盲目地用于向 DB 发送查询。假设它实现了这个 API:https://node-postgres.com/api/pool/。这个选项优先于 "connectionConfig",所以不要提供 " pool" 如果您想使用 "connectionConfig"

Requested Version

如果提供,它将停止尝试迁移到请求的版本。这将禁用使用 如果 "version" 大于 "options"."requestedVersion",则从头开始。

如果未提供,则 "options"."version""options"."migrations" 中最高 "version" 的最大值将会被使用。

Scratch

如果提供,scratch 数组将用于在 "tableNameForVersions" 表尚不存在时填充数据库。 "version" 大于 "options"."version" 的迁移也将通过假设 "options"."version"< /code>版本已经实现。

如果未提供,将使用迁移数组。

Table Name for Versions

这允许您更改用于跟踪数据库版本的名称。

Version

这应该与 "scratch" 创建的版本相同。

PostgresDB Versioning

Relational databases must be versioned if their data and tables are to be migrated properly over time. This project seeks to produce an NPM module which does the heavy lifting using configuration and version data supplied by whatever depends on this module.

Configuration

{
  "connectionConfig": {
    "user": "postgres",
    "password": "postgres",
    "host": "localhost",
    "database": "postgres",
    "port": 54231
  },
  "dryRun": false
  "eventHandler": (...eventArgs) => console.error(event),
  "migrations": [
    {"version": 1, "description": "create the table a_table", "sql": "CREATE TABLE a_table (id INT);"},
    {"version": 2, "description": "add the table another_table", "sql": "CREATE TABLE another_table (id INT);"}
  ],
  "pool": {
    query: sqlString => console.error(sqlString)
  },
  "requestedVersion": 2,
  "scratch": [
    "CREATE TABLE table_name"
  ],
  "tableNameForVersions": "DatabaseVersion",
  "version": 1
}

Connection Config

If this module should manage the connection, then pass in a config object with this API: https://node-postgres.com/api/client/.

Dry Run

With this feature enabled, the steps which would be performed will be sent as events to the event handler.

Event Handler

If this is set, it will emit events (either a string for the query or an array crafted from the (…args) which will be applied to the query function call.

For example, if the eventHandler is called with multiple arguments, such as: 'SELECT * FROM x WHERE x.y = ?;' and ['fred'], then when the plan executes, it would execute something like this:

pool.query('SELECT * FROM x WHERE x.y = ?;', ['fred']);

If the eventHandler is called with just a string, like 'SELECT * FROM x;', it would execute something like this:

pool.query('SELECT * FROM x');

Migrations

Migrations should be an array of objects containing a version number, an optional description of the changes, and the sql that should be executed.

Pool

If included, this will be used blindly to send queries to the DB. It will be assumed that it implements this API: https://node-postgres.com/api/pool/. This option takes priority over "connectionConfig", so don't provide "pool" if you want to use "connectionConfig".

Requested Version

If provided, it will stop trying to migrate at the requested version. This will disable use of scratch if the "version" is greater than the "options"."requestedVersion".

If not provided, the max of "options"."version" and of the highest "version" in "options"."migrations" will be used.

Scratch

If provided, the scratch array will be used to populate the database if the "tableNameForVersions" table is not yet present. Migrations with a "version" greater than "options"."version" will be also executed by making the assumption that the "options"."version" version has been achieved.

If not provided, the migrations array will be used.

Table Name for Versions

This allows you change the name used to track versions of the DB.

Version

This should be the same as the version which is created by "scratch".

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文