-
Notifications
You must be signed in to change notification settings - Fork 1
/
migrator.migrations.js
164 lines (155 loc) · 5.8 KB
/
migrator.migrations.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
const fs = require('fs')
const path = require('path')
const { Table } = require('./table.js')
module.exports.Migration = async (_sqlConnection, _dir, _executeQuery, _readDir) => {
const table = Table(_sqlConnection, _executeQuery)
let batchNumber = 0
const up = async () => {
const migrationList = await table.executeRawQuery('SELECT name from migrations')
const fileList = await _readDir(path.join(_dir, '/migrations/'))
let count = 0
const promiseList = []
const migrationfileList = []
fileList.forEach(async migrationFile => {
const tf = migrationList.find(x => x.name === migrationFile)
if (!tf) {
count++
const migrationFilePath = path.join(_dir, '/migrations/', migrationFile)
const { up: migrationUp } = require(migrationFilePath)
const tmp = migrationUp(table)
promiseList.push(tmp)
migrationfileList.push(migrationFile)
}
})
if (count === 0) {
return { type: 'warning', message: 'Nothing to migrate\n' }
} else {
const paList = await Promise.all(promiseList).then(value => {
return value
})
const insertPromiseList = []
paList.forEach((pa, index) => {
if (pa !== false) {
const insertStatement = `INSERT INTO migrations VALUES(NULL,'${migrationfileList[index]}',${batchNumber},'created')`
const insertpl = _executeQuery(insertStatement)
insertPromiseList.push(insertpl)
// console.log('\x1b[32m', migrationfileList[index], '\x1b[36m', ' : migrated successfully ', '\x1b[0m')
console.log('\x1b[36m', 'Migrated successfully :', '\x1b[32m', migrationfileList[index], '\x1b[0m')
} else {
console.log('\x1b[31m', migrationfileList[index], '\x1b[31m', ' : Failed to migrate ', '\x1b[0m')
}
})
await Promise.all(insertPromiseList).then(value => {
return value
})
return { type: 'success', message: '\n Migration is done\n' }
}
}
const rollback = async (_type) => {
const sqlQuerymigrationListByBath = _type === 'rollback'
? 'SELECT id,name FROM migrations where batch in ( SELECT IFNULL((SELECT max(batch) FROM migrations),0))'
: 'SELECT id,name FROM migrations'
const migrationRecords = await _executeQuery(sqlQuerymigrationListByBath)
const rollbackPromiseList = []
migrationRecords.forEach(migrationrecord => {
try {
const migrationFilePath = path.join(_dir, '/migrations/', migrationrecord.name)
const { rollback: migrationRollBack } = require(migrationFilePath)
rollbackPromiseList.push(migrationRollBack(table))
} catch (error) {
console.log(error)
process.exit()
}
})
const promiseResults = await Promise.all(rollbackPromiseList).then(value => {
return value
})
const deletePromiseList = []
promiseResults.forEach((promiseres, index) => {
if (promiseres !== false) {
const deleteStatement = `DELETE FROM migrations WHERE id=${migrationRecords[index].id}`
deletePromiseList.push(_executeQuery(deleteStatement))
console.log('\x1b[36m', 'Rollback successfully :', '\x1b[32m', migrationRecords[index].name, '\x1b[0m')
} else {
console.log('\x1b[31m', migrationRecords[index].name, '\x1b[31m', ' : Failed to Rollback ', '\x1b[0m')
}
})
await Promise.all(deletePromiseList).then(value => {
return value
})
return { type: 'success', message: '\n Migration RollBack is done\n' }
}
const createFile = async (_filename) => {
const fileCount = await new Promise(resolve => {
const migDir = path.join(_dir, '/migrations/')
fs.readdir(migDir, (err, files) => {
if (err) {
resolve(0)
} else {
resolve(files.length)
}
})
})
_filename = _dir + '/migrations/' + (fileCount + 1) + '-' + (new Date()).getTime().toString() + '-' + _filename + '.js'
const returnMsg = await new Promise(resolve => {
const tmpFile = path.join(__dirname, '/tmp.migrations.js')
fs.copyFile(tmpFile, _filename, (err) => {
if (err) {
resolve(err)
} else {
resolve('success')
}
})
})
return returnMsg === 'success'
? { type: 'success', message: 'Successfully created' }
: { type: 'error', message: 'failed to create, pls check your directories.' }
}
const init = async () => {
let msg
let responseMessage
const createMigration = table.create('migrations', {
id: 'int NOT NULL PRIMARY KEY AUTO_INCREMENT',
name: 'varchar(254) NOT NULL',
batch: 'int NOT NULL',
created_at: 'varchar(254) NOT NULL'
})
await Promise.all([createMigration]).then(value => {
return value
})
const batchNumberResult = _executeQuery('SELECT IFNULL((SELECT max(batch) FROM migrations),0) as bathno;')
batchNumber = await Promise.all([batchNumberResult]).then(value => {
return value[0][0].bathno
})
batchNumber++
const caseStr = process.argv[2]
switch (caseStr) {
case 'migration:create':
if (process.argv.length !== 4) {
return { type: 'error', message: 'please describe table name. example -> npm run migrate migration:create tablename' }
} else {
msg = await createFile(process.argv[3])
responseMessage = msg
break
}
case 'migration:up':
responseMessage = await up()
break
case 'migration:rollback':
responseMessage = await rollback('rollback')
break
case 'migration:reset':
responseMessage = await rollback('reset')
break
case 'seeding:create':
case 'seeding:up':
case 'seeding:rollback':
console.log('seeding')
break
default:
process.exit()
}
return responseMessage
}
return init()
}