@@ -5,9 +5,8 @@ import yargs from 'yargs/yargs'
55import { hideBin } from 'yargs/helpers'
66import { cosmiconfig } from 'cosmiconfig'
77import 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'
1110
1211// get a reasonable port
1312const getPort = ( args ) => {
@@ -24,40 +23,6 @@ const getPort = (args) => {
2423 }
2524}
2625
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-
6126// export the current database as a file
6227async function handleExport ( { file, type, host, database, user, password, socket, port } ) {
6328 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
6833async function handleImport ( { newStruct, type, host, database, user, password, socket, port, dry = false } ) {
6934 const current = await getStructure ( { type, host, database, user, password, socket, port } )
7035 const diff = await sqldef ( type , current , newStruct )
36+ if ( ! diff . trim ( ) ) {
37+ return ''
38+ }
7139 if ( dry ) {
72- console . log ( diff )
73- return
40+ return diff
7441 }
42+ await executeQuery ( { host, database, user, password, socket, port, query : diff } )
7543}
7644
7745const explorer = cosmiconfig ( 'sqldef' )
7846const r = ( await explorer . search ( ) ) || { config : { } }
7947const { config = { } } = r
8048yargs ( hideBin ( process . argv ) )
81- . demandCommand ( )
49+ . demandCommand ( 1 , 'You need to use a command (import/export)' )
8250 . usage ( 'Usage: $0 <command> [options]' )
8351 . help ( '?' )
8452 . alias ( '?' , 'help' )
8553
8654 . 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' )
8957
9058 . command (
9159 'export' ,
@@ -105,9 +73,9 @@ yargs(hideBin(process.argv))
10573 ( a ) => { } ,
10674 async ( args ) => {
10775 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' )
10977 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 } )
11179 const prompt = new Confirm ( 'Do you want to run this?' )
11280 if ( ! ( await prompt . run ( ) ) ) {
11381 console . error ( 'Export canceled.' )
@@ -129,6 +97,7 @@ yargs(hideBin(process.argv))
12997 alias : 'type' ,
13098 describe : 'The type of the database' ,
13199 nargs : 1 ,
100+ choices : [ 'mysql' , 'sqlite3' , 'mssql' , 'postgres' ] ,
132101 default : config . type || 'mysql'
133102 } )
134103
@@ -176,7 +145,7 @@ yargs(hideBin(process.argv))
176145
177146 . option ( 'x' , {
178147 alias : 'no-confirm' ,
179- describe : "Don't confirm before running the import/export " ,
148+ describe : "Don't confirm before running the import" ,
180149 type : 'boolean' ,
181150 default : config [ 'no-confirm' ]
182151 } ) . argv
0 commit comments