Skip to content

Umzug Migration setup #776

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

Merged
merged 14 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: 2.1
python_env: &python_env
docker:
- image: cimg/python:3.11.7-browsers
- image: cimg/python:3.11.11-browsers
install_awscli: &install_awscli
name: "Install awscli"
command: |
Expand Down Expand Up @@ -32,13 +32,13 @@ deploy_steps: &deploy_steps
source awsenvconf
./buildenv.sh -e $DEPLOY_ENV -b ${LOGICAL_ENV}-${APPNAME}-deployvar
source buildenvvar
./master_deploy.sh -d ECS -e $DEPLOY_ENV -t latest -s ${LOGICAL_ENV}-global-appvar,${LOGICAL_ENV}-${APPNAME}-appvar -i ${APPNAME}
./master_deploy.sh -d ECS -e $DEPLOY_ENV -t latest -s ${LOGICAL_ENV}-global-appvar,${LOGICAL_ENV}-${APPNAME}-appvar -i ${APPNAME} -p FARGATE

echo "======= Running Masterscript - deploy projects-api-consumers ==========="
if [ -e ${LOGICAL_ENV}-${APPNAME}-appvar.json ]; then sudo rm -vf ${LOGICAL_ENV}-${APPNAME}-appvar.json; fi
./buildenv.sh -e $DEPLOY_ENV -b ${LOGICAL_ENV}-${APPNAME}-consumers-deployvar
source buildenvvar
./master_deploy.sh -d ECS -e $DEPLOY_ENV -t latest -s ${LOGICAL_ENV}-global-appvar,${LOGICAL_ENV}-${APPNAME}-appvar -i ${APPNAME}
./master_deploy.sh -d ECS -e $DEPLOY_ENV -t latest -s ${LOGICAL_ENV}-global-appvar,${LOGICAL_ENV}-${APPNAME}-appvar -i ${APPNAME} -p FARGATE

jobs:
UnitTests:
Expand Down Expand Up @@ -149,7 +149,7 @@ workflows:
context : org-global
filters:
branches:
only: ['develop', 'connect-performance-testing', 'feature/new-milestone-concept', 'permission_fixes']
only: ['develop', 'migration-setup']
- deployProd:
context : org-global
filters:
Expand Down
73 changes: 72 additions & 1 deletion docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ securityDefinitions:
type: apiKey
name: Authorization
in: header
paths:
paths:
"/projects":
get:
tags:
Expand Down Expand Up @@ -244,6 +244,38 @@ paths:
description: Internal Server Error
schema:
$ref: "#/definitions/ErrorModel"
"/projects/{projectId}/copilots/request":
post:
tags:
- projects copilot request
operationId: createCopilotRequest
security:
- Bearer: []
description: "create copilot request"
responses:
"200":
description: Created copilot request
schema:
$ref: "#/definitions/Copilot"
"401":
description: Unauthorized
schema:
$ref: "#/definitions/ErrorModel"
"400":
description: Bad request
schema:
$ref: "#/definitions/ErrorModel"
"500":
description: Internal Server Error
schema:
$ref: "#/definitions/ErrorModel"
parameters:
- $ref: "#/parameters/projectIdParam"
- name: body
in: body
required: true
schema:
$ref: "#/definitions/NewCopilotRequest"
"/projects/{projectId}/attachments":
get:
tags:
Expand Down Expand Up @@ -5459,6 +5491,14 @@ definitions:
type: string
address:
type: string
NewCopilotRequest:
type: object
required:
- data
properties:
data:
type: object
description: copilot request data
NewProject:
type: object
required:
Expand Down Expand Up @@ -5581,6 +5621,37 @@ definitions:
type: integer
format: int64

Copilot:
type: object
properties:
id:
description: unique identifier
type: integer
format: int64
data:
description: copilot data
type: object
status:
description: status of the copilot request
type: string
createdAt:
type: string
description: Datetime (GMT) when task was created
readOnly: true
createdBy:
type: integer
format: int64
description: READ-ONLY. User who created this task
readOnly: true
updatedAt:
type: string
description: READ-ONLY. Datetime (GMT) when task was updated
readOnly: true
updatedBy:
type: integer
format: int64
description: READ-ONLY. User that last updated this task
readOnly: true
Project:
type: object
properties:
Expand Down
34 changes: 34 additions & 0 deletions migrations/20212902_copilot_requests.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
--
-- CREATE NEW TABLE:
-- copilot_requests
--
CREATE TABLE copilot_requests (
id bigint NOT NULL,
"data" json NOT NULL,
"status" character varying(16) NOT NULL,
"projectId" bigint NOT NULL,
"deletedAt" timestamp with time zone,
"createdAt" timestamp with time zone,
"updatedAt" timestamp with time zone,
"deletedBy" bigint,
"createdBy" bigint NOT NULL,
"updatedBy" bigint NOT NULL
);

CREATE SEQUENCE copilot_requests_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;

ALTER SEQUENCE copilot_requests_id_seq OWNED BY copilot_requests.id;

ALTER TABLE copilot_requests
ALTER COLUMN id SET DEFAULT nextval('copilot_requests_id_seq');

ALTER TABLE ONLY copilot_requests
ADD CONSTRAINT "copilot_requests_pkey" PRIMARY KEY (id);

ALTER TABLE ONLY copilot_requests
ADD CONSTRAINT "copilot_requests_projectId_fkey" FOREIGN KEY ("projectId") REFERENCES projects(id) ON UPDATE CASCADE ON DELETE SET NULL;
89 changes: 89 additions & 0 deletions migrations/umzug/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Migration Guide

This project uses **Sequelize** with **Umzug** for managing database migrations.

## **📌 How to Add a New Migration**

1. **Generate a new migration file**

cd into `migrations/umzug/` directory and run:

```sh
npx sequelize-cli migration:generate --name your_migration_name
```

This will create a new migration file inside `umzug/migrations/`.

2. **Modify the generated migration file**

- Open the file inside `umzug/migrations/`.
- Define the required table changes inside the `up` method.
- Define how to revert the changes in the `down` method.

**Example:** Creating a `users` table

```javascript
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.createTable("users", {
id: {
type: Sequelize.BIGINT,
allowNull: false,
primaryKey: true,
autoIncrement: true,
},
name: {
type: Sequelize.STRING,
allowNull: false,
},
createdAt: {
type: Sequelize.DATE,
allowNull: true,
},
updatedAt: {
type: Sequelize.DATE,
allowNull: true,
}
});
},
down: async (queryInterface, Sequelize) => {
await queryInterface.dropTable("users");
}
};
```

3. **Test Migrations**

```sh
npm run migrate
```

This will apply all pending migrations.

4. **Rollback Migrations (If Needed)**

```sh
npm run migrate:down
```

This will revert the last applied migration.

5. **Revert All Migrations (If Needed)**

If you need to revert all applied migrations, run:

```sh
npm run migrate:reset
```

This will undo all migrations in reverse order.

---

## **📌 How Migrations Work in This Project**

- All migration files are stored in `umzug/migrations/`.
- The migration runner is inside `umzug/index.js`.
- After installing dependencies (`npm install`), migrations will **automatically run** via `postinstall`.

---
45 changes: 45 additions & 0 deletions migrations/umzug/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const config = require('config');
const { Sequelize } = require('sequelize');
const { Umzug, SequelizeStorage } = require('umzug');

// Initialize Sequelize
const sequelize = new Sequelize(config.get('dbConfig.masterUrl'), {
dialect: 'postgres',
});

console.log('Umzug migration script:', __dirname);

// Initialize Umzug
const umzug = new Umzug({
migrations: {
glob: 'migrations/umzug/migrations/*.js',
resolve: ({ name, path, context }) => {
console.log('Loading migration:', name, path);
const migration = require(path);
return {
name,
up: async () => migration.up(context, Sequelize),
down: async () => migration.down(context, Sequelize),
};
},
},
context: sequelize.getQueryInterface(),
storage: new SequelizeStorage({ sequelize }),
logger: console,
});

// Run migrations
if (require.main === module) {
umzug
.up()
.then(() => {
console.log('Migrations executed successfully');
process.exit(0);
})
.catch((err) => {
console.error('Migration failed', err);
process.exit(1);
});
}

module.exports = umzug;
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
'use strict';

module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.createTable("copilot_requests", {
id: {
type: Sequelize.BIGINT,
allowNull: false,
primaryKey: true,
autoIncrement: true,
},
data: {
type: Sequelize.JSON,
allowNull: false,
},
status: {
type: Sequelize.STRING(16),
allowNull: false,
},
projectId: {
type: Sequelize.BIGINT,
allowNull: false,
references: {
model: "projects",
key: "id",
},
onUpdate: "CASCADE",
onDelete: "SET NULL",
},
deletedAt: {
type: Sequelize.DATE,
allowNull: true,
},
createdAt: {
type: Sequelize.DATE,
allowNull: true,
},
updatedAt: {
type: Sequelize.DATE,
allowNull: true,
},
deletedBy: {
type: Sequelize.BIGINT,
allowNull: true,
},
createdBy: {
type: Sequelize.BIGINT,
allowNull: false,
},
updatedBy: {
type: Sequelize.BIGINT,
allowNull: false,
},
});
},

down: async (queryInterface, Sequelize) => {
await queryInterface.dropTable("copilot_requests");
}
};
Loading