vr-migrations 1.0.19 → 1.0.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,117 +1,149 @@
|
|
|
1
|
-
// migrations/XXXXXXXXXXXXXX-create-installments.js
|
|
2
1
|
"use strict";
|
|
3
2
|
|
|
4
3
|
module.exports = {
|
|
5
4
|
up: async (queryInterface, Sequelize) => {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
5
|
+
// Check if table already exists
|
|
6
|
+
const tables = await queryInterface.sequelize.query(
|
|
7
|
+
"SELECT table_name FROM information_schema.tables WHERE table_name = 'installments';"
|
|
8
|
+
);
|
|
9
|
+
|
|
10
|
+
const tableExists = tables[0].length > 0;
|
|
11
|
+
|
|
12
|
+
if (!tableExists) {
|
|
13
|
+
// Create table only if it doesn't exist
|
|
14
|
+
await queryInterface.createTable("installments", {
|
|
15
|
+
id: {
|
|
16
|
+
type: Sequelize.UUID,
|
|
17
|
+
defaultValue: Sequelize.UUIDV4,
|
|
18
|
+
primaryKey: true,
|
|
19
|
+
allowNull: false,
|
|
20
|
+
},
|
|
21
|
+
devicePaymentPlanId: {
|
|
22
|
+
type: Sequelize.UUID,
|
|
23
|
+
allowNull: false,
|
|
24
|
+
references: {
|
|
25
|
+
model: "device_payment_plans",
|
|
26
|
+
key: "id",
|
|
27
|
+
},
|
|
28
|
+
onUpdate: "CASCADE",
|
|
29
|
+
onDelete: "CASCADE",
|
|
30
|
+
},
|
|
31
|
+
installmentNumber: {
|
|
32
|
+
type: Sequelize.INTEGER,
|
|
33
|
+
allowNull: false,
|
|
34
|
+
},
|
|
35
|
+
amount: {
|
|
36
|
+
type: Sequelize.DECIMAL(10, 2),
|
|
37
|
+
allowNull: false,
|
|
38
|
+
},
|
|
39
|
+
dueDate: {
|
|
40
|
+
type: Sequelize.DATE,
|
|
41
|
+
allowNull: false,
|
|
19
42
|
},
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
installmentNumber: {
|
|
24
|
-
type: Sequelize.INTEGER,
|
|
25
|
-
allowNull: false,
|
|
26
|
-
},
|
|
27
|
-
amount: {
|
|
28
|
-
type: Sequelize.DECIMAL(10, 2),
|
|
29
|
-
allowNull: false,
|
|
30
|
-
validate: {
|
|
31
|
-
min: 0,
|
|
43
|
+
paidAt: {
|
|
44
|
+
type: Sequelize.DATE,
|
|
45
|
+
allowNull: true,
|
|
32
46
|
},
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
allowNull: false,
|
|
37
|
-
},
|
|
38
|
-
paidAt: {
|
|
39
|
-
type: Sequelize.DATE,
|
|
40
|
-
allowNull: true,
|
|
41
|
-
},
|
|
42
|
-
paidAmount: {
|
|
43
|
-
type: Sequelize.DECIMAL(10, 2),
|
|
44
|
-
allowNull: true,
|
|
45
|
-
},
|
|
46
|
-
status: {
|
|
47
|
-
type: Sequelize.ENUM("PENDING", "PAID", "OVERDUE", "SKIPPED"),
|
|
48
|
-
allowNull: false,
|
|
49
|
-
defaultValue: "PENDING",
|
|
50
|
-
},
|
|
51
|
-
paymentId: {
|
|
52
|
-
type: Sequelize.UUID,
|
|
53
|
-
allowNull: true,
|
|
54
|
-
references: {
|
|
55
|
-
model: "payments",
|
|
56
|
-
key: "id",
|
|
47
|
+
paidAmount: {
|
|
48
|
+
type: Sequelize.DECIMAL(10, 2),
|
|
49
|
+
allowNull: true,
|
|
57
50
|
},
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
51
|
+
status: {
|
|
52
|
+
type: Sequelize.ENUM("PENDING", "PAID", "OVERDUE", "SKIPPED"),
|
|
53
|
+
allowNull: false,
|
|
54
|
+
defaultValue: "PENDING",
|
|
55
|
+
},
|
|
56
|
+
paymentId: {
|
|
57
|
+
type: Sequelize.UUID,
|
|
58
|
+
allowNull: true,
|
|
59
|
+
references: {
|
|
60
|
+
model: "payments",
|
|
61
|
+
key: "id",
|
|
62
|
+
},
|
|
63
|
+
onUpdate: "CASCADE",
|
|
64
|
+
onDelete: "SET NULL",
|
|
65
|
+
},
|
|
66
|
+
isAutoGenerated: {
|
|
67
|
+
type: Sequelize.BOOLEAN,
|
|
68
|
+
allowNull: false,
|
|
69
|
+
defaultValue: true,
|
|
70
|
+
},
|
|
71
|
+
metadata: {
|
|
72
|
+
type: Sequelize.JSONB,
|
|
73
|
+
allowNull: false,
|
|
74
|
+
defaultValue: {},
|
|
75
|
+
},
|
|
76
|
+
createdAt: {
|
|
77
|
+
type: Sequelize.DATE,
|
|
78
|
+
allowNull: false,
|
|
79
|
+
defaultValue: Sequelize.literal("CURRENT_TIMESTAMP"),
|
|
80
|
+
},
|
|
81
|
+
updatedAt: {
|
|
82
|
+
type: Sequelize.DATE,
|
|
83
|
+
allowNull: false,
|
|
84
|
+
defaultValue: Sequelize.literal("CURRENT_TIMESTAMP"),
|
|
85
|
+
},
|
|
86
|
+
});
|
|
87
|
+
}
|
|
82
88
|
|
|
83
|
-
// Add unique constraint
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
+
// Add unique constraint - safely check if it exists first
|
|
90
|
+
try {
|
|
91
|
+
const constraints = await queryInterface.sequelize.query(
|
|
92
|
+
`SELECT constraint_name FROM information_schema.table_constraints
|
|
93
|
+
WHERE table_name = 'installments' AND constraint_name = 'unique_installment_number_per_plan';`
|
|
94
|
+
);
|
|
89
95
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
96
|
+
if (constraints[0].length === 0) {
|
|
97
|
+
await queryInterface.addConstraint("installments", {
|
|
98
|
+
fields: ["devicePaymentPlanId", "installmentNumber"],
|
|
99
|
+
type: "unique",
|
|
100
|
+
name: "unique_installment_number_per_plan",
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
} catch (error) {
|
|
104
|
+
console.log("Constraint might already exist, skipping:", error.message);
|
|
105
|
+
}
|
|
94
106
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
107
|
+
// Add indexes - safely check if they exist
|
|
108
|
+
const indexes = [
|
|
109
|
+
{ fields: ["devicePaymentPlanId"], name: "installments_plan_id_idx" },
|
|
110
|
+
{ fields: ["status"], name: "installments_status_idx" },
|
|
111
|
+
{ fields: ["dueDate"], name: "installments_due_date_idx" },
|
|
112
|
+
{ fields: ["paymentId"], name: "installments_payment_id_idx" },
|
|
113
|
+
];
|
|
98
114
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
115
|
+
for (const index of indexes) {
|
|
116
|
+
try {
|
|
117
|
+
// Check if index exists
|
|
118
|
+
const indexExists = await queryInterface.sequelize.query(
|
|
119
|
+
`SELECT indexname FROM pg_indexes WHERE tablename = 'installments' AND indexname = '${index.name}';`
|
|
120
|
+
);
|
|
102
121
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
122
|
+
if (indexExists[0].length === 0) {
|
|
123
|
+
await queryInterface.addIndex("installments", index.fields, {
|
|
124
|
+
name: index.name,
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
} catch (error) {
|
|
128
|
+
console.log(
|
|
129
|
+
`Index ${index.name} might already exist, skipping:`,
|
|
130
|
+
error
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
106
134
|
},
|
|
107
135
|
|
|
108
136
|
down: async (queryInterface, Sequelize) => {
|
|
109
|
-
// Drop
|
|
110
|
-
await queryInterface.
|
|
111
|
-
'DROP TYPE IF EXISTS "enum_installments_status";'
|
|
112
|
-
);
|
|
137
|
+
// Drop table (cascade will handle constraints)
|
|
138
|
+
await queryInterface.dropTable("installments", { cascade: true });
|
|
113
139
|
|
|
114
|
-
// Drop
|
|
115
|
-
|
|
140
|
+
// Drop ENUM type if it exists
|
|
141
|
+
try {
|
|
142
|
+
await queryInterface.sequelize.query(
|
|
143
|
+
'DROP TYPE IF EXISTS "enum_installments_status";'
|
|
144
|
+
);
|
|
145
|
+
} catch (error) {
|
|
146
|
+
console.log("Error dropping enum:", error);
|
|
147
|
+
}
|
|
116
148
|
},
|
|
117
149
|
};
|