Skip to content

Commit 9edffc2

Browse files
committed
use a workflow for page
1 parent 9c476fc commit 9edffc2

File tree

17 files changed

+807
-52
lines changed

17 files changed

+807
-52
lines changed

.github/workflows/page.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: Page
2+
3+
on: [push]
4+
5+
jobs:
6+
build:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v4
10+
11+
- name: Build static files
12+
id: build
13+
run: cp -R src demo/sqldef
14+
15+
- name: Upload static files as artifact
16+
id: deployment
17+
uses: actions/upload-pages-artifact@v3
18+
with:
19+
path: demo
20+
21+
deploy:
22+
environment:
23+
name: github-pages
24+
url: ${{ steps.deployment.outputs.page_url }}
25+
runs-on: ubuntu-latest
26+
needs: build
27+
steps:
28+
- name: Deploy to GitHub Pages
29+
id: deployment
30+
uses: actions/deploy-pages@v4

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Node.js CI
1+
name: Test
22

33
on: [push]
44

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.DS_Store
22
node_modules
33
*.log
4+
*.sqlite

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,8 @@ npm start
4545

4646
# run all unit-tests
4747
npm t
48+
49+
# use the CLI
50+
sqlite3 test.sqlite < test/sqlite3.sql
51+
./cli.mjs import -f test/sqlite3.sql -t sqlite3 -h test.sqlite
4852
```

build.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
import { readFile, writeFile } from 'node:fs/promises'
44

5-
const buf = await readFile('./go/sqldef.wasm')
5+
const buf = await readFile('./src/go/sqldef.wasm')
66

77
await writeFile(
8-
'index.js',
8+
'src/index.js',
99
`import './go/wasm_exec.js'
1010
const buf = new Uint8Array([${[...buf].join(',')}])
1111
const go = new Go()

cli.mjs

Lines changed: 14 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ import yargs from 'yargs/yargs'
55
import { hideBin } from 'yargs/helpers'
66
import { cosmiconfig } from 'cosmiconfig'
77
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'
1110

1211
// get a reasonable port
1312
const 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
6227
async 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
6833
async 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

7745
const explorer = cosmiconfig('sqldef')
7846
const r = (await explorer.search()) || { config: {} }
7947
const { config = {} } = r
8048
yargs(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
File renamed without changes.

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@
99
"postgres"
1010
],
1111
"exports": {
12-
".": "./index.js"
12+
".": "./src/index.js",
13+
"./db": "./src/db.js"
1314
},
1415
"scripts": {
1516
"test": "node --test",
16-
"start": "npx -y live-server docs --mount=/sqldef:.",
17-
"build": "cd go && GOOS=js GOARCH=wasm tinygo build -target wasm -o sqldef.wasm sqldef-wasm.go && cp $(tinygo env TINYGOROOT)/targets/wasm_exec.js . && cd .. && node build.js"
17+
"start": "npx -y live-server demo --mount=/sqldef:src",
18+
"build": "cd src/go && GOOS=js GOARCH=wasm tinygo build -target wasm -o sqldef.wasm sqldef-wasm.go && cp $(tinygo env TINYGOROOT)/targets/wasm_exec.js . && cd ../.. && node build.js"
1819
},
1920
"author": "David Konsumer <[email protected]> (http://konsumer.js.org)",
2021
"license": "MIT",

0 commit comments

Comments
 (0)