@@ -11,36 +11,26 @@ module.exports = {
11
11
`SELECT id FROM public.organizations;` , { transaction }
12
12
) ;
13
13
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 } ) ;
25
24
26
25
// Add status column to each tenant's projects table
27
26
for ( let organization of organizations [ 0 ] ) {
28
27
const tenantHash = getTenantHash ( organization . id ) ;
29
-
28
+
30
29
// 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 } ) ;
44
34
45
35
// Update all existing records to have the default status
46
36
await queryInterface . sequelize . query (
@@ -49,19 +39,9 @@ module.exports = {
49
39
) ;
50
40
51
41
// 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 } ) ;
65
45
}
66
46
67
47
await transaction . commit ( ) ;
@@ -74,9 +54,6 @@ module.exports = {
74
54
async down ( queryInterface , Sequelize ) {
75
55
const transaction = await queryInterface . sequelize . transaction ( ) ;
76
56
try {
77
- // Remove status column from public.projects table
78
- await queryInterface . removeColumn ( 'projects' , 'status' , { transaction } ) ;
79
-
80
57
// Get all organizations to update their tenant schemas
81
58
const organizations = await queryInterface . sequelize . query (
82
59
`SELECT id FROM public.organizations;` , { transaction }
@@ -85,7 +62,7 @@ module.exports = {
85
62
// Remove status column from each tenant's projects table
86
63
for ( let organization of organizations [ 0 ] ) {
87
64
const tenantHash = getTenantHash ( organization . id ) ;
88
-
65
+
89
66
await queryInterface . removeColumn (
90
67
{
91
68
tableName : 'projects' ,
0 commit comments