1
- const {
2
- dbV3,
3
- dbV4,
4
- isPGSQL,
5
- isMYSQL,
6
- isSQLITE,
7
- } = require ( "../../config/database" ) ;
8
- const { BATCH_SIZE } = require ( "./constants" ) ;
9
- const { migrateItems } = require ( "./migrateFields" ) ;
10
- const { pick } = require ( "lodash" ) ;
1
+ const { dbV3, dbV4, isPGSQL, isMYSQL, isSQLITE } = require ( '../../config/database' ) ;
2
+ const { BATCH_SIZE } = require ( './constants' ) ;
3
+ const { migrateItems } = require ( './migrateFields' ) ;
4
+ const { pick } = require ( 'lodash' ) ;
5
+ const { resolveDestTableName, resolveSourceTableName } = require ( './tableNameHelpers' ) ;
11
6
12
7
async function migrate ( source , destination , itemMapper = undefined ) {
13
8
if ( isMYSQL ) {
14
- const sourceNotExists =
15
- ( await dbV3 . raw ( `SHOW TABLES LIKE '%${ source } %';` ) ) [ 0 ] . length === 0 ;
9
+ const sourceNotExists = ( await dbV3 . raw ( `SHOW TABLES LIKE '%${ source } %';` ) ) [ 0 ] . length === 0 ;
16
10
const destinationNotExists =
17
11
( await dbV4 . raw ( `SHOW TABLES LIKE '%${ destination } %';` ) ) [ 0 ] . length === 0 ;
18
12
@@ -32,23 +26,23 @@ async function migrate(source, destination, itemMapper = undefined) {
32
26
33
27
const sourceNotExists =
34
28
(
35
- await dbV3 ( " sqlite_master" )
36
- . select ( " name" )
37
- . where ( " type" , " table" )
38
- . where ( " name" , source )
29
+ await dbV3 ( ' sqlite_master' )
30
+ . select ( ' name' )
31
+ . where ( ' type' , ' table' )
32
+ . where ( ' name' , source )
39
33
. first ( )
40
34
. count ( )
41
- ) [ " count(*)" ] === 0 ;
35
+ ) [ ' count(*)' ] === 0 ;
42
36
43
37
const destinationNotExists =
44
38
(
45
- await dbV4 ( " sqlite_master" )
46
- . select ( " name" )
47
- . where ( " type" , " table" )
48
- . where ( " name" , destination )
39
+ await dbV4 ( ' sqlite_master' )
40
+ . select ( ' name' )
41
+ . where ( ' type' , ' table' )
42
+ . where ( ' name' , destination )
49
43
. first ( )
50
44
. count ( )
51
- ) [ " count(*)" ] === 0 ;
45
+ ) [ ' count(*)' ] === 0 ;
52
46
53
47
if ( sourceNotExists ) {
54
48
console . log ( `SOURCE TABLE ${ source } DOES NOT EXISTS` ) ;
@@ -68,18 +62,18 @@ async function migrate(source, destination, itemMapper = undefined) {
68
62
69
63
const sourceNotExists =
70
64
(
71
- await dbV3 ( " information_schema.tables" )
72
- . select ( " table_name" )
73
- . where ( " table_schema" , "public" )
74
- . where ( " table_name" , source )
65
+ await dbV3 ( ' information_schema.tables' )
66
+ . select ( ' table_name' )
67
+ . where ( ' table_schema' , process . env . DATABASE_V3_SCHEMA )
68
+ . where ( ' table_name' , source )
75
69
) . length === 0 ;
76
70
77
71
const destinationNotExists =
78
72
(
79
- await dbV4 ( " information_schema.tables" )
80
- . select ( " table_name" )
81
- . where ( " table_schema" , "public" )
82
- . where ( " table_name" , destination )
73
+ await dbV4 ( ' information_schema.tables' )
74
+ . select ( ' table_name' )
75
+ . where ( ' table_schema' , process . env . DATABASE_V4_SCHEMA )
76
+ . where ( ' table_name' , destination )
83
77
) . length === 0 ;
84
78
85
79
if ( sourceNotExists ) {
@@ -94,26 +88,31 @@ async function migrate(source, destination, itemMapper = undefined) {
94
88
}
95
89
96
90
const count =
97
- ( await dbV3 ( source ) . count ( ) . first ( ) ) . count ||
98
- ( await dbV3 ( source ) . count ( ) . first ( ) ) [ " count(*)" ] ;
99
- const columnsInfo = await dbV3 ( source ) . columnInfo ( ) ;
91
+ ( await dbV3 ( resolveSourceTableName ( source ) ) . count ( ) . first ( ) ) . count ||
92
+ ( await dbV3 ( resolveSourceTableName ( source ) ) . count ( ) . first ( ) ) [ ' count(*)' ] ;
93
+ const columnsInfo = await dbV3 ( resolveSourceTableName ( source ) ) . columnInfo ( ) ;
100
94
101
95
const jsonFields = Object . keys ( columnsInfo ) . filter ( ( column ) => {
102
- return columnsInfo [ column ] . type === " jsonb" ;
96
+ return columnsInfo [ column ] . type === ' jsonb' ;
103
97
} ) ;
104
98
105
99
console . log ( `Migrating ${ count } items from ${ source } to ${ destination } ` ) ;
106
- await dbV4 ( destination ) . del ( ) ;
100
+ await dbV4 ( resolveDestTableName ( destination ) ) . del ( ) ;
107
101
108
- console . log ( "DBV4 ITEMS" ) ;
102
+ let tableColumnsInfo = await dbV4 ( destination ) . columnInfo ( ) ;
109
103
110
- const tableColumnsInfo = await dbV4 ( destination ) . columnInfo ( ) ;
104
+ if ( isPGSQL ) {
105
+ // https://github.com/knex/knex/issues/1490
106
+ tableColumnsInfo = await dbV4 ( destination )
107
+ . withSchema ( process . env . DATABASE_V4_SCHEMA )
108
+ . columnInfo ( ) ;
109
+ }
111
110
112
111
const tableColumns = Object . keys ( tableColumnsInfo ) ;
113
112
114
113
for ( let page = 0 ; page * BATCH_SIZE < count ; page ++ ) {
115
114
console . log ( `${ source } batch #${ page + 1 } ` ) ;
116
- const items = await dbV3 ( source )
115
+ const items = await dbV3 ( resolveSourceTableName ( source ) )
117
116
. limit ( BATCH_SIZE )
118
117
. offset ( page * BATCH_SIZE ) ;
119
118
@@ -127,29 +126,24 @@ async function migrate(source, destination, itemMapper = undefined) {
127
126
return item ;
128
127
} ) ;
129
128
130
- const migratedItems = migrateItems ( withParsedJsonFields , itemMapper ) . map (
131
- ( item ) => {
132
- const filteredItems = pick ( item , tableColumns ) ;
133
-
134
- if ( Object . keys ( item ) . length !== Object . keys ( filteredItems ) . length ) {
135
- const filteredColumns = Object . keys ( item ) . filter ( function ( obj ) {
136
- return Object . keys ( filteredItems ) . indexOf ( obj ) == - 1 ;
137
- } ) ;
129
+ const migratedItems = migrateItems ( withParsedJsonFields , itemMapper ) . map ( ( item ) => {
130
+ const filteredItems = pick ( item , tableColumns ) ;
138
131
139
- console . log (
140
- "WARNING - items of " +
141
- destination +
142
- " was filtered " +
143
- JSON . stringify ( filteredColumns )
144
- ) ;
145
- }
132
+ if ( Object . keys ( item ) . length !== Object . keys ( filteredItems ) . length ) {
133
+ const filteredColumns = Object . keys ( item ) . filter ( function ( obj ) {
134
+ return Object . keys ( filteredItems ) . indexOf ( obj ) == - 1 ;
135
+ } ) ;
146
136
147
- return filteredItems ;
137
+ console . log (
138
+ 'WARNING - items of ' + destination + ' was filtered ' + JSON . stringify ( filteredColumns )
139
+ ) ;
148
140
}
149
- ) ;
141
+
142
+ return filteredItems ;
143
+ } ) ;
150
144
151
145
if ( migratedItems . length > 0 ) {
152
- await dbV4 ( destination ) . insert ( migratedItems ) ;
146
+ await dbV4 ( resolveDestTableName ( destination ) ) . insert ( migratedItems ) ;
153
147
}
154
148
}
155
149
@@ -158,12 +152,10 @@ async function migrate(source, destination, itemMapper = undefined) {
158
152
159
153
async function resetTableSequence ( destination ) {
160
154
if ( isPGSQL ) {
161
- const hasId = await dbV4 . schema . hasColumn ( destination , "id" ) ;
155
+ const hasId = await dbV4 . schema . hasColumn ( destination , 'id' ) ;
162
156
if ( hasId ) {
163
157
const seq = `${ destination . slice ( 0 , 56 ) } _id_seq` ;
164
- await dbV4 . raw (
165
- `SELECT SETVAL ('${ seq } ', (SELECT MAX(id) + 1 FROM ${ destination } ))`
166
- ) ;
158
+ await dbV4 . raw ( `SELECT SETVAL ('${ seq } ', (SELECT MAX(id) + 1 FROM ${ destination } ))` ) ;
167
159
}
168
160
}
169
161
}
0 commit comments