Skip to content

Commit 8b4601d

Browse files
Merge pull request #2266 from bluewave-labs/hp-oct-1-fix-project-status-migration-bug
Fix project status migration bug
2 parents 94a1088 + 2396337 commit 8b4601d

File tree

2 files changed

+20
-43
lines changed

2 files changed

+20
-43
lines changed

Servers/database/migrations/20250922000001-add-project-status.js

Lines changed: 19 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,26 @@ module.exports = {
1111
`SELECT id FROM public.organizations;`, { transaction }
1212
);
1313

14-
// Add status column to public.projects table
15-
await queryInterface.addColumn(
16-
'projects',
17-
'status',
18-
{
19-
type: Sequelize.ENUM('Not started', 'In progress', 'Under review', 'Completed', 'Closed', 'On hold', 'Rejected'),
20-
allowNull: false,
21-
defaultValue: 'Not started'
22-
},
23-
{ transaction }
24-
);
14+
await queryInterface.sequelize.query(
15+
`CREATE TYPE projects_status_enum AS ENUM (
16+
'Not started',
17+
'In progress',
18+
'Under review',
19+
'Completed',
20+
'Closed',
21+
'On hold',
22+
'Rejected'
23+
);`, { transaction });
2524

2625
// Add status column to each tenant's projects table
2726
for (let organization of organizations[0]) {
2827
const tenantHash = getTenantHash(organization.id);
29-
28+
3029
// Add the column as nullable first
31-
await queryInterface.addColumn(
32-
{
33-
tableName: 'projects',
34-
schema: tenantHash
35-
},
36-
'status',
37-
{
38-
type: Sequelize.ENUM('Not started', 'In progress', 'Under review', 'Completed', 'Closed', 'On hold', 'Rejected'),
39-
allowNull: true,
40-
defaultValue: 'Not started'
41-
},
42-
{ transaction }
43-
);
30+
await queryInterface.sequelize.query(`
31+
ALTER TABLE "${tenantHash}".projects
32+
ADD COLUMN "status" projects_status_enum
33+
DEFAULT 'Not started';`, { transaction });
4434

4535
// Update all existing records to have the default status
4636
await queryInterface.sequelize.query(
@@ -49,19 +39,9 @@ module.exports = {
4939
);
5040

5141
// Then change the column to not allow null
52-
await queryInterface.changeColumn(
53-
{
54-
tableName: 'projects',
55-
schema: tenantHash
56-
},
57-
'status',
58-
{
59-
type: Sequelize.ENUM('Not started', 'In progress', 'Under review', 'Completed', 'Closed', 'On hold', 'Rejected'),
60-
allowNull: false,
61-
defaultValue: 'Not started'
62-
},
63-
{ transaction }
64-
);
42+
await queryInterface.sequelize.query(`
43+
ALTER TABLE "${tenantHash}".projects
44+
ALTER COLUMN "status" SET NOT NULL;`, { transaction });
6545
}
6646

6747
await transaction.commit();
@@ -74,9 +54,6 @@ module.exports = {
7454
async down(queryInterface, Sequelize) {
7555
const transaction = await queryInterface.sequelize.transaction();
7656
try {
77-
// Remove status column from public.projects table
78-
await queryInterface.removeColumn('projects', 'status', { transaction });
79-
8057
// Get all organizations to update their tenant schemas
8158
const organizations = await queryInterface.sequelize.query(
8259
`SELECT id FROM public.organizations;`, { transaction }
@@ -85,7 +62,7 @@ module.exports = {
8562
// Remove status column from each tenant's projects table
8663
for (let organization of organizations[0]) {
8764
const tenantHash = getTenantHash(organization.id);
88-
65+
8966
await queryInterface.removeColumn(
9067
{
9168
tableName: 'projects',

Servers/scripts/createNewTenant.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export const createNewTenant = async (organization_id: number, transaction: Tran
4141
last_updated_by integer,
4242
is_demo boolean NOT NULL DEFAULT false,
4343
is_organizational boolean NOT NULL DEFAULT false,
44-
status enum_projects_status NOT NULL DEFAULT 'Not started',
44+
status projects_status_enum NOT NULL DEFAULT 'Not started',
4545
created_at timestamp without time zone NOT NULL DEFAULT now(),
4646
CONSTRAINT projects_pkey PRIMARY KEY (id),
4747
CONSTRAINT projects_owner_fkey FOREIGN KEY (owner)

0 commit comments

Comments
 (0)