@@ -5,9 +5,8 @@ import yargs from 'yargs/yargs'
5
5
import { hideBin } from 'yargs/helpers'
6
6
import { cosmiconfig } from 'cosmiconfig'
7
7
import Confirm from 'prompt-confirm'
8
- import SqliteDatabase from 'better-sqlite3'
9
- import mysql from 'mysql2/promise'
10
- import sqldef from './index.js'
8
+ import sqldef from './src/index.js'
9
+ import { getStructure , executeQuery } from './src/db.js'
11
10
12
11
// get a reasonable port
13
12
const getPort = ( args ) => {
@@ -24,40 +23,6 @@ const getPort = (args) => {
24
23
}
25
24
}
26
25
27
- // get the current structure of database, as text
28
- async function getStructure ( { type, host, database, user, password, socket, port } ) {
29
- if ( type === 'sqlite' ) {
30
- const db = new SqliteDatabase ( host )
31
- const tables = db . prepare ( 'SELECT sql FROM sqlite_master WHERE type = "table" AND name NOT LIKE "sqlite_%"' ) . all ( )
32
- return tables . map ( ( t ) => t . sql ) . join ( ';\n' ) + ';'
33
- } else if ( type === 'mysql' ) {
34
- const connection = await mysql . createConnection ( {
35
- host,
36
- user,
37
- password,
38
- database,
39
- socket,
40
- port
41
- } )
42
- const [ tables ] = await connection . query ( 'SELECT table_name FROM information_schema.tables WHERE table_schema = ?' , [ database ] )
43
- return (
44
- (
45
- await Promise . all (
46
- tables . map ( async ( table ) => {
47
- const tableName = table . table_name
48
- const [ result ] = await connection . query ( `SHOW CREATE TABLE \`${ tableName } \`` )
49
- if ( result && result [ 0 ] ) {
50
- return result [ 0 ] [ 'Create Table' ]
51
- } else {
52
- return ''
53
- }
54
- } )
55
- )
56
- ) . join ( ';\n' ) + ';'
57
- )
58
- }
59
- }
60
-
61
26
// export the current database as a file
62
27
async function handleExport ( { file, type, host, database, user, password, socket, port } ) {
63
28
const current = await getStructure ( { type, host, database, user, password, socket, port } )
@@ -68,24 +33,27 @@ async function handleExport({ file, type, host, database, user, password, socket
68
33
async function handleImport ( { newStruct, type, host, database, user, password, socket, port, dry = false } ) {
69
34
const current = await getStructure ( { type, host, database, user, password, socket, port } )
70
35
const diff = await sqldef ( type , current , newStruct )
36
+ if ( ! diff . trim ( ) ) {
37
+ return ''
38
+ }
71
39
if ( dry ) {
72
- console . log ( diff )
73
- return
40
+ return diff
74
41
}
42
+ await executeQuery ( { host, database, user, password, socket, port, query : diff } )
75
43
}
76
44
77
45
const explorer = cosmiconfig ( 'sqldef' )
78
46
const r = ( await explorer . search ( ) ) || { config : { } }
79
47
const { config = { } } = r
80
48
yargs ( hideBin ( process . argv ) )
81
- . demandCommand ( )
49
+ . demandCommand ( 1 , 'You need to use a command (import/export)' )
82
50
. usage ( 'Usage: $0 <command> [options]' )
83
51
. help ( '?' )
84
52
. alias ( '?' , 'help' )
85
53
86
54
. alias ( 'v' , 'version' )
87
- . example ( '$0 export --user=cool --password=mysecret --database=mydatabase ' , 'Save your current schema, from your mysql database, in schema.sql' )
88
- . example ( '$0 import --user=cool --password=mysecret --database=mydatabase ' , 'Update your database to match schema.sql' )
55
+ . example ( '$0 export' , 'Save your current schema, from your mysql database, in schema.sql' )
56
+ . example ( '$0 import' , 'Update your database to match schema.sql' )
89
57
90
58
. command (
91
59
'export' ,
@@ -105,9 +73,9 @@ yargs(hideBin(process.argv))
105
73
( a ) => { } ,
106
74
async ( args ) => {
107
75
const { file, type, host, database, user, password, socket, noConfirm, port = getPort ( args ) } = args
108
- const newStruct = await readFile ( file )
76
+ const newStruct = await readFile ( file , 'utf8' )
109
77
if ( ! noConfirm ) {
110
- await handleImport ( { file , type, host, database, user, password, socket, port, ...config , dry : true } )
78
+ await handleImport ( { newStruct , type, host, database, user, password, socket, port, ...config , dry : true } )
111
79
const prompt = new Confirm ( 'Do you want to run this?' )
112
80
if ( ! ( await prompt . run ( ) ) ) {
113
81
console . error ( 'Export canceled.' )
@@ -129,6 +97,7 @@ yargs(hideBin(process.argv))
129
97
alias : 'type' ,
130
98
describe : 'The type of the database' ,
131
99
nargs : 1 ,
100
+ choices : [ 'mysql' , 'sqlite3' , 'mssql' , 'postgres' ] ,
132
101
default : config . type || 'mysql'
133
102
} )
134
103
@@ -176,7 +145,7 @@ yargs(hideBin(process.argv))
176
145
177
146
. option ( 'x' , {
178
147
alias : 'no-confirm' ,
179
- describe : "Don't confirm before running the import/export " ,
148
+ describe : "Don't confirm before running the import" ,
180
149
type : 'boolean' ,
181
150
default : config [ 'no-confirm' ]
182
151
} ) . argv
0 commit comments