2023-02-11 06:21:06 -08:00
|
|
|
## Info
|
|
|
|
|
|
|
|
https://knexjs.org/guide/migrations.html#knexfile-in-other-languages
|
|
|
|
|
2023-08-11 07:17:31 -07:00
|
|
|
## Basic rules
|
|
|
|
- All tables must have a primary key named `id`
|
2023-08-31 14:19:21 -07:00
|
|
|
- Filename format: `YYYY-MM-DD-HHMM-patch-name.js`
|
|
|
|
- Avoid native SQL syntax, use knex methods, because Uptime Kuma supports SQLite and MariaDB.
|
2023-02-11 06:21:06 -08:00
|
|
|
|
|
|
|
## Template
|
|
|
|
|
|
|
|
```js
|
|
|
|
exports.up = function(knex) {
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.down = function(knex) {
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
// exports.config = { transaction: false };
|
|
|
|
```
|
|
|
|
|
|
|
|
## Example
|
|
|
|
|
2023-08-11 07:17:31 -07:00
|
|
|
Filename: 2023-06-30-1348-create-user-and-product.js
|
2023-02-11 06:21:06 -08:00
|
|
|
|
|
|
|
```js
|
|
|
|
exports.up = function(knex) {
|
|
|
|
return knex.schema
|
2023-08-11 07:17:31 -07:00
|
|
|
.createTable('user', function (table) {
|
2023-02-11 06:21:06 -08:00
|
|
|
table.increments('id');
|
|
|
|
table.string('first_name', 255).notNullable();
|
|
|
|
table.string('last_name', 255).notNullable();
|
|
|
|
})
|
2023-08-11 07:17:31 -07:00
|
|
|
.createTable('product', function (table) {
|
2023-02-11 06:21:06 -08:00
|
|
|
table.increments('id');
|
|
|
|
table.decimal('price').notNullable();
|
|
|
|
table.string('name', 1000).notNullable();
|
2023-06-30 11:48:42 -07:00
|
|
|
}).then(() => {
|
|
|
|
knex("products").insert([
|
|
|
|
{ price: 10, name: "Apple" },
|
|
|
|
{ price: 20, name: "Orange" },
|
|
|
|
]);
|
2023-02-11 06:21:06 -08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.down = function(knex) {
|
|
|
|
return knex.schema
|
2023-08-11 07:17:31 -07:00
|
|
|
.dropTable("product")
|
|
|
|
.dropTable("user");
|
2023-02-11 06:21:06 -08:00
|
|
|
};
|
|
|
|
```
|
2023-06-30 02:26:37 -07:00
|
|
|
|
|
|
|
https://knexjs.org/guide/migrations.html#transactions-in-migrations
|