Skip to content

Commit e7a4cb1

Browse files
authored
Merge pull request #2 from mrl5/docs-example
docs(README): document example
2 parents f13138f + 68c9e98 commit e7a4cb1

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

README.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,78 @@ A simple forwards only migration solution for [postgres.js](https://github.com/p
99
It expects a [postgres.js sql]() database object to run migrations with.
1010

1111
## `Example`
12+
13+
1. Create `migrations` directory with following structure
14+
```
15+
.
16+
└── migrations
17+
├── 00001_initial
18+
│   └── index.sql
19+
├── 00002_update
20+
│   └── index.js
21+
└── 00003_delete
22+
└── index.sql
23+
```
24+
Note that migration folder name expects 5 digits. This is very important
25+
detail.
26+
27+
2. Create actual migrations. Note that it can be either in SQL or JS syntax
28+
```sql
29+
create table test (
30+
a text,
31+
b int
32+
)
33+
```
34+
```js
35+
export default async function(sql) {
36+
await sql`
37+
alter table test add column c timestamp with time zone
38+
`
39+
40+
await sql`
41+
insert into test (a, b, c) values ('hello', 9, ${ new Date() })
42+
`
43+
}
44+
```
45+
46+
3. Create your `migrate.js` script:
47+
```js
48+
import shift from 'postgres-shift';
49+
import postgres from 'postgres';
50+
import { fileURLToPath } from 'url';
51+
52+
const {
53+
DB_HOST,
54+
DB_PORT,
55+
DB_NAME,
56+
ADMIN_DB_USER,
57+
ADMIN_DB_PASSWORD,
58+
} = process.env;
59+
60+
export const sql = postgres({
61+
host: DB_HOST,
62+
port: Number(DB_PORT),
63+
database: DB_NAME,
64+
user: ADMIN_DB_USER,
65+
pass: ADMIN_DB_PASSWORD,
66+
idle_timeout: 1,
67+
});
68+
69+
shift({
70+
sql,
71+
path: fileURLToPath(new URL('migrations', import.meta.url)),
72+
before: ({ migration_id, name }) => {
73+
console.log('Migrating', migration_id, name);
74+
},
75+
})
76+
.then(() => console.log('All good'))
77+
.catch((err) => {
78+
console.error('Failed', err);
79+
process.exit(1);
80+
});
81+
```
82+
83+
4. Run it with
84+
```console
85+
node ./migrate.js
86+
```

0 commit comments

Comments
 (0)