Skip to content

Commit e38203c

Browse files
committed
git init
0 parents  commit e38203c

36 files changed

+2788
-0
lines changed

.dockerignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
*.md
2+
test/**
3+
.eslintrc.json
4+
apidoc.json
5+
.github*
6+
.vscode*
7+
.git
8+
.cache
9+
pgdata

.env.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
POSTGRES_USER=testHexschool
2+
POSTGRES_PASSWORD=pgStartkit4test
3+
POSTGRES_DB=test

.github/workflows/check.yml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
name: Check
2+
on:
3+
push:
4+
5+
jobs:
6+
test-migrate :
7+
name: Run Migrate Test
8+
runs-on: ubuntu-latest
9+
env:
10+
DB_USERNAME: root
11+
DB_DATABASE: test
12+
DB_PASSWORD: test
13+
DB_HOST: localhost
14+
services:
15+
postgres:
16+
image: postgres:16.4-alpine3.20
17+
ports:
18+
- 5432:5432
19+
env:
20+
POSTGRES_USER: ${{ env.DB_USERNAME }}
21+
POSTGRES_PASSWORD: ${{ env.DB_PASSWORD }}
22+
POSTGRES_DB: ${{ env.DB_DATABASE }}
23+
steps:
24+
- name: Checkout to Repo Directory
25+
uses: actions/checkout@v4
26+
- name: Use Node.js 22
27+
uses: actions/setup-node@v4
28+
with:
29+
node-version: '22'
30+
cache: 'npm'
31+
- name: Restore cache node modules
32+
id: cache
33+
uses: actions/cache/restore@v4
34+
with:
35+
path: ./node_modules
36+
key: ${{ runner.os }}-node-${{ hashFiles('package-lock.json') }}
37+
restore-keys: |
38+
${{ runner.os }}-node-
39+
- name: Install dependencies
40+
if: steps.cache.outputs.cache-hit != 'true'
41+
run: npm ci --production
42+
- name: update cache node modules
43+
if: steps.cache.outputs.cache-hit != 'true'
44+
uses: actions/cache/save@v4
45+
with:
46+
path: ./node_modules
47+
key: ${{ runner.os }}-node-${{ hashFiles('package-lock.json') }}
48+
- name: test migrate
49+
run: npm run init && npm run migrate
50+
51+
52+

.gitignore

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
lerna-debug.log*
8+
.pnpm-debug.log*
9+
10+
# Diagnostic reports (https://nodejs.org/api/report.html)
11+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
12+
13+
# Runtime data
14+
pids
15+
*.pid
16+
*.seed
17+
*.pid.lock
18+
19+
# Directory for instrumented libs generated by jscoverage/JSCover
20+
lib-cov
21+
22+
# Coverage directory used by tools like istanbul
23+
coverage
24+
*.lcov
25+
26+
# nyc test coverage
27+
.nyc_output
28+
29+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
30+
.grunt
31+
32+
# Bower dependency directory (https://bower.io/)
33+
bower_components
34+
35+
# node-waf configuration
36+
.lock-wscript
37+
38+
# Compiled binary addons (https://nodejs.org/api/addons.html)
39+
build/Release
40+
41+
# Dependency directories
42+
node_modules/
43+
jspm_packages/
44+
45+
# Snowpack dependency directory (https://snowpack.dev/)
46+
web_modules/
47+
48+
# TypeScript cache
49+
*.tsbuildinfo
50+
51+
# Optional npm cache directory
52+
.npm
53+
54+
# Optional eslint cache
55+
.eslintcache
56+
57+
# Optional stylelint cache
58+
.stylelintcache
59+
60+
# Microbundle cache
61+
.rpt2_cache/
62+
.rts2_cache_cjs/
63+
.rts2_cache_es/
64+
.rts2_cache_umd/
65+
66+
# Optional REPL history
67+
.node_repl_history
68+
69+
# Output of 'npm pack'
70+
*.tgz
71+
72+
# Yarn Integrity file
73+
.yarn-integrity
74+
75+
# dotenv environment variable files
76+
.env
77+
.env.development.local
78+
.env.test.local
79+
.env.production.local
80+
.env.local
81+
82+
# parcel-bundler cache (https://parceljs.org/)
83+
.cache
84+
.parcel-cache
85+
86+
# Next.js build output
87+
.next
88+
out
89+
90+
# Nuxt.js build / generate output
91+
.nuxt
92+
dist
93+
94+
# Gatsby files
95+
.cache/
96+
# Comment in the public line in if your project uses Gatsby and not Next.js
97+
# https://nextjs.org/blog/next-9-1#public-directory-support
98+
# public
99+
100+
# vuepress build output
101+
.vuepress/dist
102+
103+
# vuepress v2.x temp and cache directory
104+
.temp
105+
.cache
106+
107+
# Docusaurus cache and generated files
108+
.docusaurus
109+
110+
# Serverless directories
111+
.serverless/
112+
113+
# FuseBox cache
114+
.fusebox/
115+
116+
# DynamoDB Local files
117+
.dynamodb/
118+
119+
# TernJS port file
120+
.tern-port
121+
122+
# Stores VSCode versions used for testing VSCode extensions
123+
.vscode-test
124+
125+
# yarn v2
126+
.yarn/cache
127+
.yarn/unplugged
128+
.yarn/build-state.yml
129+
.yarn/install-state.gz
130+
.pnp.*
131+
pgdata

README.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# db-migrate-startkit
2+
3+
## 事前準備
4+
5+
下載安裝Docker Desktop
6+
* [Windows版](https://desktop.docker.com/win/main/amd64/Docker%20Desktop%20Installer.exe?utm_source=docker&utm_medium=webreferral&utm_campaign=dd-smartbutton&utm_location=module&_gl=1*1x0tato*_gcl_au*NDk4NzQwNjM1LjE3MjczMzQ0NDY.*_ga*MTkxMzI2NzM5NC4xNjU5OTM4NTcy*_ga_XJWPQMJYHQ*MTcyNzMzMzcxNy4xNTYuMS4xNzI3MzM0NDY4LjM3LjAuMA..)
7+
* Mac版
8+
* [Intel處理器](https://desktop.docker.com/mac/main/amd64/Docker.dmg?utm_source=docker&utm_medium=webreferral&utm_campaign=dd-smartbutton&utm_location=module&_gl=1*sjadaf*_gcl_au*NDk4NzQwNjM1LjE3MjczMzQ0NDY.*_ga*MTkxMzI2NzM5NC4xNjU5OTM4NTcy*_ga_XJWPQMJYHQ*MTcyNzMzMzcxNy4xNTYuMS4xNzI3MzM0NDY4LjM3LjAuMA..)
9+
* [M系列處理器](https://desktop.docker.com/mac/main/arm64/Docker.dmg?utm_source=docker&utm_medium=webreferral&utm_campaign=dd-smartbutton&utm_location=module&_gl=1*sjadaf*_gcl_au*NDk4NzQwNjM1LjE3MjczMzQ0NDY.*_ga*MTkxMzI2NzM5NC4xNjU5OTM4NTcy*_ga_XJWPQMJYHQ*MTcyNzMzMzcxNy4xNTYuMS4xNzI3MzM0NDY4LjM3LjAuMA..)
10+
11+
下載並安裝Node.js
12+
* [下載連結](https://nodejs.org/zh-tw)
13+
14+
下載並安裝Dbeaver Community
15+
* [Windows版](https://dbeaver.io/files/dbeaver-ce-latest-x86_64-setup.exe)
16+
* Mac版
17+
* [Intel處理器](https://dbeaver.io/files/dbeaver-ce-latest-macos-x86_64.dmg)
18+
* [M系列處理器](https://dbeaver.io/files/dbeaver-ce-latest-macos-aarch64.dmg)
19+
20+
## 開始使用
21+
22+
### 安裝套件
23+
24+
>執行前請確保已經安裝了Node.js
25+
26+
打開終端機並輸入以下指令
27+
```
28+
$ npm i
29+
```
30+
31+
### 新增一組遷移
32+
在終端機輸入以下指令來新增一組遷移,your_file_name請替換成任何你要的檔案名稱,檔案名稱之間不可有空白
33+
```
34+
$ npm run create your_file_name
35+
```
36+
執行後程式將自動產生一組遷移在migrations資料夾下,一組遷移將包含3個檔案,檔案皆以日期時間加上剛剛輸入的文字命名:
37+
38+
範例如下:
39+
```
40+
2024092600000-your_file_name-up.sql
41+
2024092600000-your_file_name-down.sql
42+
2024092600000-your_file_name.js
43+
```
44+
* 2024092600000-your_file_name-up.sql:執行遷移時會被執行的sql檔案
45+
46+
* 2024092600000-your_file_name-down.sql:執行遷移失敗時,才會被執行的sql檔案,一般作為還原up檔執行到一半的sql用,不需要可空白
47+
48+
* 2024092600000-your_file_name.js:執行此組遷移的程式檔案,請勿修改
49+
50+
那些遷移檔案內容的小技巧:
51+
* 每組遷移盡量保持單一職責,例如需要新增表並寫入資料,建議拆分成兩組遷移,一組新增表,一組寫入資料
52+
* 不修改已經執行過的遷移,如有需要,應該新增新的遷移來修改
53+
54+
### 首次在本機開啟資料庫並執行遷移
55+
56+
> 請確保Docker Desktop已經正常啟動,並能夠看到容器頁面(Containers)
57+
58+
打開終端機並輸入以下指令,程式將自動啟動資料庫,並運行遷移
59+
```
60+
$ npm run start
61+
```
62+
63+
啟動後,請檢查Docker Desktop程式界面中裡是否新增db-migrate-startkit的容器堆疊,該堆疊裡了有兩個容器:
64+
* db_migrate-1:執行遷移檔案的容器,執行後會自動關閉(狀態(STATUS)為Exited)。請務必進入檢查查看遷移的執行結果,確保遷移執行成功。
65+
* postgres-1:資料庫容器,狀態應為Running
66+
67+
啟動時,程式將自動產生pgdata資料夾,用以保存資料庫的資料,如將其刪除,將清空所有資料。
68+
69+
程式執行遷移時,將依照遷移檔案的日期,由遠至近執行,且每組遷移只會執行一次,先前執行成功過的遷移不會被再次執行。
70+
71+
### 重新啟動資料庫
72+
```
73+
$ npm run restart
74+
```
75+
### 關閉資料庫
76+
```
77+
$ npm run stop
78+
```
79+
80+
### 關閉資料庫並清除資料
81+
```
82+
$ npm run clean
83+
```

database.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"defaultEnv": "pg",
3+
"sql-file" : true,
4+
"pg": {
5+
"host": {"ENV": "DB_HOST"},
6+
"port": 5432,
7+
"user": {"ENV": "DB_USERNAME"},
8+
"password" : {"ENV": "DB_PASSWORD"},
9+
"database": {"ENV": "DB_DATABASE"},
10+
"driver": "pg",
11+
"multipleStatements": true
12+
}
13+
}

docker-compose.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
version: "3.8"
2+
volumes:
3+
pgData:
4+
services:
5+
postgres:
6+
image: postgres:16.4-alpine3.20
7+
environment:
8+
- POSTGRES_USER=${POSTGRES_USER}
9+
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
10+
- POSTGRES_DB=${POSTGRES_DB}
11+
volumes:
12+
- pgData:/var/lib/postgresql/data
13+
ports:
14+
- 5432:5432
15+
deploy:
16+
replicas: 1
17+
networks:
18+
- dbnetwork
19+
healthcheck:
20+
test: ["CMD-SHELL", "sh -c 'pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}'"]
21+
interval: 10s
22+
timeout: 3s
23+
retries: 3
24+
db_migrate:
25+
build:
26+
dockerfile: ./migrate.Dockerfile
27+
environment:
28+
- DB_HOST=postgres
29+
- DB_DATABASE=${POSTGRES_DB}
30+
- DB_USERNAME=${POSTGRES_USER}
31+
- DB_PASSWORD=${POSTGRES_PASSWORD}
32+
depends_on:
33+
postgres:
34+
condition: service_healthy
35+
deploy:
36+
replicas: 1
37+
networks:
38+
- dbnetwork
39+
networks:
40+
dbnetwork:
41+
name: db-network

migrate.Dockerfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
FROM node:20-alpine3.19
2+
WORKDIR /
3+
COPY . /
4+
RUN npm ci --production
5+
CMD npm run init && npm run migrate

0 commit comments

Comments
 (0)