-
Notifications
You must be signed in to change notification settings - Fork 18
feat(nestjs): migration #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
WalkthroughThe changes introduce a robust database migration workflow to the NestJS template. This includes new scripts for generating and running migrations, a shell script to coordinate startup sequencing, migration scripts, and updates to Docker configuration. The application now conditionally generates migrations and applies them before launching, with improved test coverage and dependency updates. Changes
Sequence Diagram(s)sequenceDiagram
participant Dev as Developer
participant Container as Docker Container
participant Endpoint as endpoint.sh
participant DB as MySQL
participant Migrator as migrate.js
participant App as NestJS App
Dev->>Container: Build & run container
Container->>Endpoint: Start endpoint.sh
Endpoint->>DB: Wait for MySQL (wait4x)
DB-->>Endpoint: Ready
Endpoint->>Migrator: Run migrate.js
Migrator->>DB: Apply pending migrations
Migrator-->>Endpoint: Migration complete
Endpoint->>App: Start main app (node dist/main.js)
App->>DB: Connect and serve
sequenceDiagram
participant Dev as Developer
participant App as NestJS App
participant DB as MySQL
participant Gen as generateMigration.ts
Dev->>App: Set GEN_MIGRATION and run app
App->>Gen: generateMigration(connection, name)
Gen->>DB: Inspect schema changes
Gen->>Gen: Generate migration file
Gen-->>App: Done
App-->>Dev: Migration file created, app exits
Poem
✨ Finishing Touches🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 7
🧹 Nitpick comments (4)
template/nestJs/libs/db/src/db.service.ts (1)
18-18: Consider removing commented code instead of leaving it in place.The commented
synchronizeoption should be removed entirely since the application now uses explicit migrations. Commented code can create confusion and clutter.- // synchronize: process.env.DATABASE_SYNCHRONIZE === 'true',template/nestJs/migrate.js (1)
10-10: Add input validation for database port.The
parseInt()call should handle invalid port values gracefully.- port: parseInt(process.env.DATABASE_PORT), + port: parseInt(process.env.DATABASE_PORT) || 3306,template/nestJs/migrations/1751959540264-TinyPro.js (1)
14-16: Review user table schema for potential improvements.The user table has some inconsistencies and potential issues:
- Both
createTime/updateTimeandcreate_time/update_timecolumns exist- Consider normalizing the schema to avoid redundancy
Consider consolidating the timestamp columns to use a consistent naming convention.
template/nestJs/src/generateMigration.ts (1)
63-63: Ensure consistent timestamp usage.The migration name uses
new Date().getTime()while the filename usesDate.now(). For consistency, consider using the same timestamp for both.-const code = getTemplate(name, new Date().getTime(), upSqls, downSqls); +const timestamp = Date.now(); +const code = getTemplate(name, timestamp, upSqls, downSqls);And then use the same timestamp for the filename:
-const fileName = `migrations/${Date.now()}-${name}.js`; +const fileName = `migrations/${timestamp}-${name}.js`;Also applies to: 79-79
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (1)
pnpm-lock.yamlis excluded by!**/pnpm-lock.yaml
📒 Files selected for processing (10)
template/nestJs/docker-compose.yml(1 hunks)template/nestJs/dockerfile(1 hunks)template/nestJs/endpoint.sh(1 hunks)template/nestJs/libs/db/src/db.service.ts(1 hunks)template/nestJs/migrate.js(1 hunks)template/nestJs/migrations/1751959540264-TinyPro.js(1 hunks)template/nestJs/package.json(4 hunks)template/nestJs/src/generateMigration.ts(1 hunks)template/nestJs/src/main.ts(2 hunks)template/nestJs/src/user/__tests__/user.service.spec.ts(2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
template/nestJs/src/main.ts (1)
template/nestJs/src/generateMigration.ts (1)
generateMigration(39-83)
🪛 Shellcheck (0.10.0)
template/nestJs/endpoint.sh
[error] 1-1: Tips depend on target shell and yours is unknown. Add a shebang or a 'shell' directive.
(SC2148)
🪛 Hadolint (2.12.0)
template/nestJs/dockerfile
[error] 3-3: Use COPY instead of ADD for files and folders
(DL3020)
🪛 Biome (1.9.4)
template/nestJs/src/generateMigration.ts
[error] 53-53: Use a regular expression literal instead of the RegExp constructor.
Regular expression literals avoid some escaping required in a string literal, and are easier to analyze statically.
Safe fix: Use a literal notation instead.
(lint/complexity/useRegexLiterals)
🔇 Additional comments (9)
template/nestJs/docker-compose.yml (1)
12-13: Good addition for MySQL compatibility.Enabling the native password authentication plugin improves compatibility with older MySQL client libraries and authentication methods.
template/nestJs/src/user/__tests__/user.service.spec.ts (2)
71-71: Test properly updated to match schema changes.The test correctly includes the additional user fields (
passwordandname) that align with the database schema defined in the migration.
88-88: Consistent test data structure.The test data includes all required user fields matching the updated schema.
template/nestJs/dockerfile (1)
1-1: Verify Alpine compatibility in DockerfilePlease confirm that your application and all of its dependencies build and run correctly against Alpine’s musl libc:
- Location:
template/nestJs/dockerfileline 1 (FROM node:alpine as BUILDER)- No
.gypfiles detected in the repo- No direct native modules (
sharp,bcrypt,canvas,sqlite3) found inpackage.json- Transitive references to
node-gypexist inpnpm-lock.yaml, indicating possible native buildsRun your full build and test suite on the Alpine image (or switch back to a glibc-based image if any dependency fails).
template/nestJs/package.json (2)
7-8: Well-structured migration scripts.The migration scripts are well-designed:
migrate:genproperly builds the project before generating migrationsmigrate:rundirectly executes the migration runner- Good use of
cross-envfor cross-platform compatibility
38-38: Verified: No breaking changes found for mysql2 ^3.14.0 or TypeORM ^0.3.22I reviewed the official changelogs and release notes—neither mysql2 (3.4.3→3.14.0) nor TypeORM (0.3.20→0.3.22) introduce documented breaking changes between these patch releases. You can proceed with these updates; just run your full test suite (especially any integration tests) to confirm end-to-end compatibility.
template/nestJs/src/main.ts (1)
5-6: Good imports for migration functionality.The imports are appropriate for the migration generation feature.
template/nestJs/src/generateMigration.ts (2)
39-83: Well-implemented migration generation logic.The migration generation function is well-structured with proper:
- Schema change detection
- SQL-to-JavaScript conversion
- Code formatting with Prettier
- Directory creation with error handling
- Logging for success/failure
72-78: Good error handling for directory creation.The directory creation logic properly handles the case where the migrations directory doesn't exist, with appropriate error catching and recursive directory creation.
|
@GaoNeng-wWw 这是用来生成 SQL 语句,从来实现数据库迁移对吗?是否会影响现有业务功能呢?可否写个使用文档呢? |
功能不会影响,影响的是部署方法。我待会写个文档吧 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 4
🧹 Nitpick comments (7)
docs/tiny-pro.md (2)
7-9: Add a language identifier to the fenced code block.
markdownlint(MD040) warns because the fence opened on line 7 has no language tag.
Usebashso syntax highlighting and tooling work correctly.-```bash +```bash tiny init pro--- `81-86`: **Specify a language for the `docker ps` output block or mark it as `text`.** Another MD040 hit; adding a tag keeps the lint pipeline clean. ```diff -``` +```text CONTAINER ID IMAGE ...docs/tiny-pro-backend-dev-guideline.md (3)
57-60: Add a language identifier to the fenced log snippet.-``` +```text LOG [NestApplication] ...
70-78: Correct the migration generation diff – the added line should have a trailing semicolon for TS/ESLint compliance.- @Column() -+ nickName: string; + @Column() + nickName: string;
85-86: Typo: “歉意文件生成成功” → “迁移文件生成成功”.Minor but confusing typo in user-facing docs.
docs/tiny-pro-front-dev-guideline.md (2)
11-17: Add a language tag (plaintext) for the directory-tree fence.-``` +```plaintext web src views test-page index.vue
30-35: Provide alt-text for images to satisfy MD045 and improve accessibility.- +Repeat for the other inline images.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (22)
docs/images/feature_2012x914.pngis excluded by!**/*.pngdocs/images/tiny-pro-show.pngis excluded by!**/*.pngdocs/images/为测试用户绑定菜单.pngis excluded by!**/*.pngdocs/images/只勾选测试页面即可.pngis excluded by!**/*.pngdocs/images/左侧测试页面 - 中文.pngis excluded by!**/*.pngdocs/images/左侧测试页面 - 英文.pngis excluded by!**/*.pngdocs/images/新增权限.pngis excluded by!**/*.pngdocs/images/新增权限成功.pngis excluded by!**/*.pngdocs/images/新增词条.pngis excluded by!**/*.pngdocs/images/最终页面效果.pngis excluded by!**/*.pngdocs/images/权限绑定展示.pngis excluded by!**/*.pngdocs/images/查看菜单页.pngis excluded by!**/*.pngdocs/images/测试用户登录.pngis excluded by!**/*.pngdocs/images/添加菜单.pngis excluded by!**/*.pngdocs/images/添加角色完全体.pngis excluded by!**/*.pngdocs/images/点击添加词条.pngis excluded by!**/*.pngdocs/images/登陆测试账号.pngis excluded by!**/*.pngdocs/images/绑定权限.pngis excluded by!**/*.pngdocs/images/绑定菜单.pngis excluded by!**/*.pngdocs/images/绑定角色.pngis excluded by!**/*.pngdocs/images/退出登录.pngis excluded by!**/*.pngdocs/images/选择语言.pngis excluded by!**/*.png
📒 Files selected for processing (3)
docs/tiny-pro-backend-dev-guideline.md(1 hunks)docs/tiny-pro-front-dev-guideline.md(1 hunks)docs/tiny-pro.md(1 hunks)
🧰 Additional context used
🪛 markdownlint-cli2 (0.17.2)
docs/tiny-pro-front-dev-guideline.md
11-11: Fenced code blocks should have a language specified
(MD040, fenced-code-language)
30-30: Images should have alternate text (alt text)
(MD045, no-alt-text)
34-34: Images should have alternate text (alt text)
(MD045, no-alt-text)
42-42: Images should have alternate text (alt text)
(MD045, no-alt-text)
59-59: Images should have alternate text (alt text)
(MD045, no-alt-text)
65-65: Images should have alternate text (alt text)
(MD045, no-alt-text)
104-104: Images should have alternate text (alt text)
(MD045, no-alt-text)
114-114: Images should have alternate text (alt text)
(MD045, no-alt-text)
123-123: Images should have alternate text (alt text)
(MD045, no-alt-text)
133-133: Images should have alternate text (alt text)
(MD045, no-alt-text)
137-137: Images should have alternate text (alt text)
(MD045, no-alt-text)
175-175: Images should have alternate text (alt text)
(MD045, no-alt-text)
179-179: Images should have alternate text (alt text)
(MD045, no-alt-text)
docs/tiny-pro-backend-dev-guideline.md
57-57: Fenced code blocks should have a language specified
(MD040, fenced-code-language)
98-98: Fenced code blocks should have a language specified
(MD040, fenced-code-language)
docs/tiny-pro.md
13-13: Fenced code blocks should have a language specified
(MD040, fenced-code-language)
32-32: Fenced code blocks should have a language specified
(MD040, fenced-code-language)
81-81: Fenced code blocks should have a language specified
(MD040, fenced-code-language)
151-151: Fenced code blocks should have a language specified
(MD040, fenced-code-language)
209-209: Fenced code blocks should have a language specified
(MD040, fenced-code-language)
🪛 LanguageTool
docs/tiny-pro-backend-dev-guideline.md
[uncategorized] ~185-~185: 数词与名词之间一般应存在量词,可能缺少量词。
Context: ...有policy::get::list权限的角色访问,其余角色访问则会返回一个403错误 但有些时候我们需要一个接口允许未登陆的用户访问。例如我们在登陆的时候经常需要...
(wa5)
| 后端服务支持`docker启动`与`命令启动`, 执行操作前请先确保所处位置为`tiny-pro/nestJS` | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Path spelling is inconsistent with the real directory (nestJs vs nestJS).
The guide tells users to cd tiny-pro/nestJS, but the scaffold generates nestJs.
Mismatch will lead to “No such file or directory” errors in copy-paste scenarios.
🤖 Prompt for AI Agents
In docs/tiny-pro.md around lines 40 to 41, the directory path spelling
`tiny-pro/nestJS` is inconsistent with the actual scaffold directory `nestJs`.
Update the path in the documentation to match the real directory name
`tiny-pro/nestJs` to prevent "No such file or directory" errors when users
follow the guide.
| 在运行`docker compose up -d`之前,请先修改`.env`环境变量文件,示例如下 | ||
|
|
||
| ```properties | ||
| # 数据库IP | ||
| DATABASE_HOST = 'mysql' | ||
| # 数据库端口 | ||
| DATABASE_PORT = 3306 | ||
| # 数据库用户名 | ||
| DATABASE_USERNAME = 'root' | ||
| # 数据库密码 | ||
| DATABASE_PASSWORD = 'root' | ||
| # 数据库名 (请确保该库存在) | ||
| DATABASE_NAME = 'ospp-nest' | ||
| # 请阅读: https://www.typeorm.org/migrations | ||
| # 线上环境请关闭 | ||
| DATABASE_SYNCHRONIZE = false | ||
| DATABASE_AUTOLOADENTITIES = true | ||
| # jwt secret | ||
| AUTH_SECRET = 'secret' | ||
| REDIS_SECONDS = 7200 | ||
| # redis ip | ||
| REDIS_HOST = 'redis' | ||
| # redis 端口 | ||
| REDIS_PORT = 6379 | ||
| # token过期时间 | ||
| EXPIRES_IN = '2h' | ||
| # 分页默认起始页 (一般可以不修改) | ||
| PAGINATION_PAGE = 1 | ||
| # 分页默认大小 | ||
| PAGINATION_LIMIT = 10 | ||
| ``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Strip the single quotes in the .env sample – they are parsed as part of the value.
Most .env parsers (dotenv, env‐cmd, etc.) treat quotes literally.
Values like DATABASE_HOST = 'mysql' become "'mysql'", which breaks TypeORM
connection options.
-DATABASE_HOST = 'mysql'
+DATABASE_HOST = mysql
...
-DATABASE_SYNCHRONIZE = false
+DATABASE_SYNCHRONIZE = falseCommittable suggestion skipped: line range outside the PR's diff.
🤖 Prompt for AI Agents
In docs/tiny-pro.md between lines 44 and 74, the .env sample uses single quotes
around values, which causes the quotes to be included in the parsed values and
breaks configuration. Remove all single quotes around the values in the .env
example so that entries like DATABASE_HOST = mysql and DATABASE_PASSWORD = root
are unquoted, ensuring correct parsing by dotenv and similar tools.
| # 线上环境请关闭 | ||
| DATABASE_SYNCHRONIZE = 'true' | ||
| DATABASE_AUTOLOADENTITIES = 'true' | ||
| # jwt secret |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Boolean env flags should not be wrapped in quotes.
TypeORM treats 'true' (string) as truthy but not boolean, which can lead to
unexpected behaviour when parsed via class-validator or custom config loaders.
-DATABASE_SYNCHRONIZE = 'true'
-DATABASE_AUTOLOADENTITIES = 'true'
+DATABASE_SYNCHRONIZE = true
+DATABASE_AUTOLOADENTITIES = true📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| # 线上环境请关闭 | |
| DATABASE_SYNCHRONIZE = 'true' | |
| DATABASE_AUTOLOADENTITIES = 'true' | |
| # jwt secret | |
| # 线上环境请关闭 | |
| DATABASE_SYNCHRONIZE = true | |
| DATABASE_AUTOLOADENTITIES = true | |
| # jwt secret |
🤖 Prompt for AI Agents
In docs/tiny-pro-backend-dev-guideline.md around lines 25 to 28, the boolean
environment variables DATABASE_SYNCHRONIZE and DATABASE_AUTOLOADENTITIES are set
as strings with quotes. Remove the quotes around 'true' so these flags are set
as actual boolean values true, preventing type issues with TypeORM and config
validation.
|
|
||
| 1. 请确保你在`.env`文件中设置的`DATABASE_HOST`为开发数据库。 | ||
| 2. 运行 `pnpm run migrate:gen` | ||
| 3. 当出现`Success! Migration file created at migrations/1752296660591-TinyPro.js`命令后则表示歉意文件生成成功 | ||
| 4. 运行 `pnpm run mirgate:run`指令或`node migrate.js`来应用迁移文件。当出现了 `Now you can safely launched the project` 字样。表示迁移文件已经被安全的应用到了数据库中。 | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mirgate:run is misspelled and will 404 in package.json.
Replace with the correct script name:
-4. 运行 `pnpm run mirgate:run`指令或`node migrate.js`来应用迁移文件
+4. 运行 `pnpm run migrate:run` 指令或 `node migrate.js` 来应用迁移文件📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| 1. 请确保你在`.env`文件中设置的`DATABASE_HOST`为开发数据库。 | |
| 2. 运行 `pnpm run migrate:gen` | |
| 3. 当出现`Success! Migration file created at migrations/1752296660591-TinyPro.js`命令后则表示歉意文件生成成功 | |
| 4. 运行 `pnpm run mirgate:run`指令或`node migrate.js`来应用迁移文件。当出现了 `Now you can safely launched the project` 字样。表示迁移文件已经被安全的应用到了数据库中。 | |
| 1. 请确保你在`.env`文件中设置的`DATABASE_HOST`为开发数据库。 | |
| 2. 运行 `pnpm run migrate:gen` | |
| 3. 当出现`Success! Migration file created at migrations/1752296660591-TinyPro.js`命令后则表示迁移文件生成成功 | |
| 4. 运行 `pnpm run migrate:run` 指令或 `node migrate.js` 来应用迁移文件。当出现了 `Now you can safely launched the project` 字样。表示迁移文件已经被安全的应用到了数据库中。 |
🤖 Prompt for AI Agents
In docs/tiny-pro-backend-dev-guideline.md around lines 82 to 87, the script name
`mirgate:run` is misspelled and will cause a 404 error when run. Correct the
script name to `migrate:run` in the instructions to match the actual script
defined in package.json.
PR
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
Issue Number: N/A
What is the new behavior?
用户可以创建
migration并运行,以安全的迁移数据库. 该pr包含了一个默认的迁移, docker运行时会等待mysql镜像的启动(可能会失败, 例如60秒后mysql仍然未启动成功). 启动成功后, 镜像会执行node migrate.js来安全的迁移数据库结构, seed逻辑保持不变。如何创建迁移策略
如果修改了
model(typeorm官方似乎叫做entries). 需要执行pnpm migrate:gen. 当需要部署迁移策略时候可以执行node migrate.js或者是pnpm migrate:run--- config: theme: redux layout: dagre --- flowchart TD A(["Start"]) --> B{"MySQL启动成功"} B --是--> C[执行迁移策略] B --否(每隔2S重试, 60s超时)--> B C --> D[初始化数据]Does this PR introduce a breaking change?
Other information
Summary by CodeRabbit
New Features
Improvements
Bug Fixes
Tests