Skip to content

Commit 0c7c3dd

Browse files
create migration script and read me
1 parent 50f844f commit 0c7c3dd

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

README.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,24 @@
1-
Project created with `starter-code-v2` as the UWBlueprint's backend repository.
1+
# Prefaces
2+
Project created with `starter-code-v2` as the UWBlueprint's backend repository.
3+
# Migrations
4+
5+
6+
## Create Migration File
7+
This repository includes a script for generating migration files in the `typescript/migrations` directory. The script automates the creation of migration files with a timestamped name and a basic template.
8+
9+
To run the script, use the following command:
10+
```bash
11+
cd backend/typescript
12+
```
13+
```bash
14+
ts-node create-migrate.ts <name of migration>
15+
```
16+
Example:
17+
```bash
18+
ts-node create-migrate.ts create-applicant-table
19+
```
20+
## Running Migrations
21+
To apply all pending migrations to the database, use the following command. This will execute the migration files sequentially and update the database schema:
22+
```bash
23+
docker exec recruitment_tools_backend node migrate up
24+
```

backend/create-migrate.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/* eslint-disable @typescript-eslint/no-var-requires */
2+
import fs from "fs";
3+
import path from "path";
4+
5+
const MIGRATIONS_DIR = path.resolve(__dirname, "../typescript/migrations");
6+
7+
const migrationName = process.argv[2];
8+
9+
if (!migrationName) {
10+
console.error(
11+
"Please provide a migration name. Usage: ts-node scripts/createMigration.ts create-reviewed-applications",
12+
);
13+
process.exit(1);
14+
}
15+
16+
const timestamp = new Date()
17+
.toISOString()
18+
.replace(/[-:.TZ]/g, "")
19+
.slice(0, 14);
20+
const fileName = `${timestamp}-${migrationName}.ts`;
21+
const filePath = path.join(MIGRATIONS_DIR, fileName);
22+
23+
const template = `import { DataType } from "sequelize-typescript";
24+
import { Migration } from "../umzug";
25+
26+
const TABLE_NAME = "your table name here";
27+
28+
export const up: Migration = async ({ context: sequelize }) => {
29+
30+
};
31+
32+
export const down: Migration = async ({ context: sequelize }) => {
33+
};
34+
`;
35+
36+
fs.writeFileSync(filePath, template);
37+
console.log(`Migration created: ${filePath}`);

0 commit comments

Comments
 (0)