Skip to content

Commit af1f715

Browse files
authored
♻️ refactor: move database to packages (#8874)
* move db * refactor db import * refactor eval types * fix tests * fix tests * fix tests * fix db migration issues * fix docker issue * fix tests * update alias * fix tests * update db test for client and server * refactor db * update codecov * update codecov * update codecov * add docker pr comments
1 parent 7e68cc5 commit af1f715

File tree

187 files changed

+336
-181
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

187 files changed

+336
-181
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/**
2+
* Generate or update PR comment with Docker build info
3+
*/
4+
module.exports = async ({
5+
github,
6+
context,
7+
dockerMetaJson,
8+
image,
9+
version,
10+
dockerhubUrl,
11+
platforms,
12+
}) => {
13+
const COMMENT_IDENTIFIER = '<!-- DOCKER-BUILD-COMMENT -->';
14+
15+
const parseTags = () => {
16+
try {
17+
if (dockerMetaJson) {
18+
const parsed = JSON.parse(dockerMetaJson);
19+
if (Array.isArray(parsed.tags) && parsed.tags.length > 0) {
20+
return parsed.tags;
21+
}
22+
}
23+
} catch (e) {
24+
// ignore parsing error, fallback below
25+
}
26+
if (image && version) {
27+
return [`${image}:${version}`];
28+
}
29+
return [];
30+
};
31+
32+
const generateCommentBody = () => {
33+
const tags = parseTags();
34+
const buildTime = new Date().toISOString();
35+
36+
// Use the first tag as the main version
37+
const mainTag = tags.length > 0 ? tags[0] : `${image}:${version}`;
38+
const tagVersion = mainTag.includes(':') ? mainTag.split(':')[1] : version;
39+
40+
return [
41+
COMMENT_IDENTIFIER,
42+
'',
43+
'### 🐳 Database Docker Build Completed!',
44+
`**Version**: \`${tagVersion || 'N/A'}\``,
45+
`**Build Time**: \`${buildTime}\``,
46+
'',
47+
dockerhubUrl ? `🔗 View all tags on Docker Hub: ${dockerhubUrl}` : '',
48+
'',
49+
'### Pull Image',
50+
'Download the Docker image to your local machine:',
51+
'',
52+
'```bash',
53+
`docker pull ${mainTag}`,
54+
'```',
55+
'> [!IMPORTANT]',
56+
'> This build is for testing and validation purposes.',
57+
]
58+
.filter(Boolean)
59+
.join('\n');
60+
};
61+
62+
const body = generateCommentBody();
63+
64+
// List comments on the PR
65+
const { data: comments } = await github.rest.issues.listComments({
66+
issue_number: context.issue.number,
67+
owner: context.repo.owner,
68+
repo: context.repo.repo,
69+
});
70+
71+
const existing = comments.find((c) => c.body && c.body.includes(COMMENT_IDENTIFIER));
72+
if (existing) {
73+
await github.rest.issues.updateComment({
74+
comment_id: existing.id,
75+
owner: context.repo.owner,
76+
repo: context.repo.repo,
77+
body,
78+
});
79+
return { updated: true, id: existing.id };
80+
}
81+
82+
const result = await github.rest.issues.createComment({
83+
issue_number: context.issue.number,
84+
owner: context.repo.owner,
85+
repo: context.repo.repo,
86+
body,
87+
});
88+
return { updated: false, id: result.data.id };
89+
};

.github/workflows/docker-database.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,24 @@ jobs:
159159
- name: Inspect image
160160
run: |
161161
docker buildx imagetools inspect ${{ env.REGISTRY_IMAGE }}:${{ steps.meta.outputs.version }}
162+
163+
164+
- name: Comment on PR with Docker build info
165+
if: github.event_name == 'pull_request'
166+
uses: actions/github-script@v7
167+
with:
168+
github-token: ${{ secrets.GITHUB_TOKEN }}
169+
script: |
170+
const prComment = require('${{ github.workspace }}/.github/scripts/docker-pr-comment.js');
171+
const result = await prComment({
172+
github,
173+
context,
174+
dockerMetaJson: ${{ toJSON(steps.meta.outputs.json) }},
175+
image: "${{ env.REGISTRY_IMAGE }}",
176+
version: "${{ steps.meta.outputs.version }}",
177+
dockerhubUrl: "https://hub.docker.com/r/${{ env.REGISTRY_IMAGE }}/tags",
178+
platforms: "linux/amd64, linux/arm64",
179+
});
180+
core.info(`Status: ${result.updated ? 'Updated' : 'Created'}, ID: ${result.id}`);
181+
182+

.github/workflows/test.yml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ jobs:
7575
files: ./coverage/app/lcov.info
7676
flags: app
7777

78-
test-server:
78+
test-databsae:
7979
name: Test Database
8080

8181
runs-on: ubuntu-latest
@@ -109,8 +109,10 @@ jobs:
109109
- name: Lint
110110
run: bun run lint
111111

112-
- name: Test Server Coverage
113-
run: bun run test-server:coverage
112+
- uses: pnpm/action-setup@v4
113+
114+
- name: Test Coverage
115+
run: pnpm --filter @lobechat/database test:coverage
114116
env:
115117
DATABASE_TEST_URL: postgresql://postgres:postgres@localhost:5432/postgres
116118
DATABASE_DRIVER: node
@@ -119,9 +121,9 @@ jobs:
119121
S3_PUBLIC_DOMAIN: https://example.com
120122
APP_URL: https://home.com
121123

122-
- name: Upload Server coverage to Codecov
124+
- name: Upload Database coverage to Codecov
123125
uses: codecov/codecov-action@v4
124126
with:
125127
token: ${{ secrets.CODECOV_TOKEN }}
126-
files: ./coverage/server/lcov.info
127-
flags: server
128+
files: ./packages/database/coverage/lcov.info
129+
flags: database

Dockerfile.database

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ COPY --from=base /distroless/ /
120120
COPY --from=builder /app/.next/standalone /app/
121121

122122
# Copy database migrations
123-
COPY --from=builder /app/src/database/migrations /app/migrations
123+
COPY --from=builder /app/packages/database/migrations /app/migrations
124124
COPY --from=builder /app/scripts/migrateServerDB/docker.cjs /app/docker.cjs
125125
COPY --from=builder /app/scripts/migrateServerDB/errorHint.js /app/errorHint.js
126126

codecov.yml

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,39 @@
1+
component_management:
2+
individual_components:
3+
# App architecture layers
4+
- component_id: app_store
5+
name: "Store"
6+
paths:
7+
- src/store/**
8+
- component_id: app_services
9+
name: "Services"
10+
paths:
11+
- src/services/**
12+
- component_id: app_server
13+
name: "Server"
14+
paths:
15+
- src/server/**
16+
- component_id: app_libs
17+
name: "Libs"
18+
paths:
19+
- src/libs/**
20+
- component_id: app_utils
21+
name: "Utils"
22+
paths:
23+
- src/utils/**
24+
125
coverage:
226
status:
327
project:
428
default: off
5-
server:
29+
database:
630
flags:
7-
- server
31+
- database
832
app:
933
flags:
1034
- app
1135
patch: off
36+
37+
38+
comment:
39+
layout: "header, diff, flags, components" # show component info in the PR comment

drizzle.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ export default {
2222
url: connectionString,
2323
},
2424
dialect: 'postgresql',
25-
out: './src/database/migrations',
25+
out: './packages/database/migrations',
2626

27-
schema: './src/database/schemas',
27+
schema: './packages/database/src/schemas',
2828
strict: true,
2929
} satisfies Config;

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,6 @@
7373
"test": "npm run test-app && npm run test-server",
7474
"test-app": "vitest run --config vitest.config.ts",
7575
"test-app:coverage": "vitest run --config vitest.config.ts --coverage",
76-
"test-server": "vitest run --config vitest.config.server.ts",
77-
"test-server:coverage": "vitest run --config vitest.config.server.ts --coverage",
7876
"test:update": "vitest -u",
7977
"type-check": "tsgo --noEmit",
8078
"webhook:ngrok": "ngrok http http://localhost:3011",
@@ -141,6 +139,7 @@
141139
"@icons-pack/react-simple-icons": "9.6.0",
142140
"@khmyznikov/pwa-install": "0.3.9",
143141
"@langchain/community": "^0.3.50",
142+
"@lobechat/database": "workspace:*",
144143
"@lobechat/electron-client-ipc": "workspace:*",
145144
"@lobechat/electron-server-ipc": "workspace:*",
146145
"@lobechat/file-loaders": "workspace:*",
File renamed without changes.

0 commit comments

Comments
 (0)