-
Notifications
You must be signed in to change notification settings - Fork 0
Migrations
The script will take the sql.js
.
The migration files needs to be in the directory migrations
.
The name of the migration files needs to start with a number and can have also a name, e.g. 201908221621-create-table-users(.sql/.js)
. The numbers of all migration files needs to be in an ascending order to get executed in the right order.
There is a migrate_make
(recommended to name it migrate:make
in your user scripts) command available to create new migration files which get prefixed with the current unix timestamp.
There are two different formats for the migration files supported.
-
.sql
with plain SQL queries, e.g.:
CREATE TABLE "users" {
"id" SERIAL NOT NULL PRIMARY KEY,
"name" VARCHAR (255) NOT NULL,
"email" VARCHAR (255) UNIQUE NOT NULL,
"password" CHAR (60),
"validated" BOOLEAN DEFAULT 0
};
-
.js
exporting an async function getting theclient
, e.g.:
module.exports = async sql => sql.query(
sql`CREATE TABLE "users" {
"id" SERIAL NOT NULL PRIMARY KEY,
"name" VARCHAR (255) NOT NULL,
"email" VARCHAR (255) UNIQUE NOT NULL,
"password" CHAR (60),
"validated" BOOLEAN DEFAULT 0
}`
)
Every table should have an id
column defined as SERIAL NOT NULL PRIMARY KEY
and also created_at
and updated_at
defined as TIMESTAMPTZ NOT NULL DEFAULT NOW()
. The updated_at
should also be automatically updated with the current timestamp if the row is changed, which can be tricky with PostgreSQL.
For these requirements there are helpers which can be used in .js
based migrations, e.g.:
module.exports = async (sql, { columns, updatedAt }) => {
await sql.query(
sql`CREATE TABLE "users" {
${columns.id},
${columns.created_at},
${columns.updated_at},
"name" VARCHAR (255) NOT NULL,
"email" VARCHAR (255) UNIQUE NOT NULL,
"password" CHAR (60),
"validated" BOOLEAN DEFAULT 0
}`
)
await sql.query(updatedAt('users'))
}
{
"scripts": {
"migrate": "migrate",
"migrate:make": "migrate_name"
}
}
For local usage, if dotenv-safe
is available, it will be used.
If the npm package debug
is installed, it will be used and all outputs are labeled with migrate
. Otherwise console.log
is used.
The migration script will create and maintain the table migrations
to check which migration files are already executed.
Also a trigger function named trigger_set_timestamp
will be created and for every table which will use the Updated At Helper (including the migrations
table itself) there will be a trigger created named set_timestamp_${table}
.
So be careful there are no name conflicts.
Found a bug or missing a feature? -> Create a new Issue
Found a security issue? -> Look at the Security Policy
Having questions, want to give feedback or talk to me? -> E-Mail me [email protected]