@@ -55,21 +55,6 @@ async function createUniqueIndex(
5555 } ) ;
5656}
5757
58- async function dropUniqueIndex (
59- collection : Collection < Document > ,
60- colName : string
61- ) {
62- if ( colName === "_id" ) return ;
63- const indexes = await collection . indexes ( ) ;
64-
65- for ( const index of indexes ) {
66- if ( ! index . name || ! index . unique || index . key [ colName ] !== 1 ) continue ;
67-
68- await collection . dropIndex ( index . name ) ;
69- break ;
70- }
71- }
72-
7358async function executeColumn (
7459 collection : Collection < Document > ,
7560 operation : ColumnOperation ,
@@ -86,15 +71,27 @@ async function executeColumn(
8671 ) ;
8772 return ;
8873
89- case "drop-column" :
90- await dropUniqueIndex ( collection , operation . name ) ;
74+ case "drop-column" : {
75+ if ( operation . name === "_id" )
76+ throw new Error ( "You cannot drop `_id` column" ) ;
77+ const indexes = await collection . indexes ( ) ;
78+
79+ // drop unique index on it
80+ for ( const index of indexes ) {
81+ if ( ! index . name || ! index . unique || index . key [ operation . name ] !== 1 )
82+ continue ;
83+
84+ await collection . dropIndex ( index . name ) ;
85+ break ;
86+ }
9187
9288 await collection . updateMany (
9389 { } ,
9490 { $unset : { [ operation . name ] : "" } } ,
9591 { session }
9692 ) ;
9793 return ;
94+ }
9895 case "create-column" : {
9996 const col = operation . value ;
10097 const defaultValue = col . generateDefaultValue ( ) ?? null ;
@@ -106,12 +103,6 @@ async function executeColumn(
106103 { session }
107104 ) ;
108105 }
109-
110- if ( col . isUnique ) {
111- await createUniqueIndex ( collection , col . getUniqueConstraintName ( ) , [
112- col . names . mongodb ,
113- ] ) ;
114- }
115106 return ;
116107 }
117108
@@ -135,16 +126,6 @@ async function executeColumn(
135126
136127 if ( bulk . batches . length > 0 ) await bulk . execute ( ) ;
137128 }
138-
139- if ( operation . updateUnique ) {
140- if ( col . isUnique ) {
141- await createUniqueIndex ( collection , col . getUniqueConstraintName ( ) , [
142- col . names . sql ,
143- ] ) ;
144- } else {
145- await dropUniqueIndex ( collection , col . names . mongodb ) ;
146- }
147- }
148129 }
149130 }
150131}
0 commit comments