summaryrefslogtreecommitdiffstats
path: root/db/knex_migrations/README.md
blob: d2b8470b1e9e85c5fbcc4fedea2f83760d42e79d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# Info

https://knexjs.org/guide/migrations.html#knexfile-in-other-languages

## Basic rules

- All tables must have a primary key named `id`
- Filename format: `YYYY-MM-DD-HHMM-patch-name.js`
- Avoid native SQL syntax, use knex methods, because Uptime Kuma supports SQLite and MariaDB.

## Template

```js
exports.up = function(knex) {

};

exports.down = function(knex) {

};

// exports.config = { transaction: false };
```

## Example

Filename: 2023-06-30-1348-create-user-and-product.js

```js
exports.up = function(knex) {
  return knex.schema
    .createTable('user', function (table) {
        table.increments('id');
        table.string('first_name', 255).notNullable();
        table.string('last_name', 255).notNullable();
    })
    .createTable('product', function (table) {
        table.increments('id');
        table.decimal('price').notNullable();
        table.string('name', 1000).notNullable();
    }).then(() => {
        knex("products").insert([
            { price: 10, name: "Apple" },
            { price: 20, name: "Orange" },
        ]);
    });
};

exports.down = function(knex) {
  return knex.schema
      .dropTable("product")
      .dropTable("user");
};
```

https://knexjs.org/guide/migrations.html#transactions-in-migrations