spindb 0.3.6 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +62 -8
- package/cli/commands/create.ts +275 -1
- package/cli/commands/deps.ts +326 -0
- package/cli/commands/menu.ts +387 -29
- package/cli/commands/restore.ts +173 -16
- package/cli/index.ts +2 -0
- package/cli/ui/prompts.ts +133 -0
- package/config/os-dependencies.ts +358 -0
- package/config/paths.ts +39 -1
- package/core/dependency-manager.ts +429 -0
- package/core/postgres-binary-manager.ts +44 -28
- package/engines/base-engine.ts +9 -0
- package/engines/postgresql/index.ts +53 -0
- package/package.json +2 -2
- package/types/index.ts +7 -0
package/README.md
CHANGED
|
@@ -51,6 +51,8 @@ spindb connect mydb
|
|
|
51
51
|
| `spindb delete [name]` | Delete a container |
|
|
52
52
|
| `spindb config show` | Show configuration |
|
|
53
53
|
| `spindb config detect` | Auto-detect PostgreSQL tools |
|
|
54
|
+
| `spindb deps check` | Check status of client tools |
|
|
55
|
+
| `spindb deps install` | Install missing client tools |
|
|
54
56
|
|
|
55
57
|
## How It Works
|
|
56
58
|
|
|
@@ -71,15 +73,50 @@ Data is stored in `~/.spindb/`:
|
|
|
71
73
|
|
|
72
74
|
## PostgreSQL Client Tools
|
|
73
75
|
|
|
74
|
-
SpinDB bundles the PostgreSQL **server** (postgres, pg_ctl, initdb) but not client tools (psql, pg_dump, pg_restore). For `connect` and `restore` commands, you need PostgreSQL client tools installed
|
|
76
|
+
SpinDB bundles the PostgreSQL **server** (postgres, pg_ctl, initdb) but not client tools (psql, pg_dump, pg_restore). For `connect` and `restore` commands, you need PostgreSQL client tools installed.
|
|
77
|
+
|
|
78
|
+
### Automatic Installation
|
|
79
|
+
|
|
80
|
+
SpinDB can check and install client tools automatically:
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
# Check status of all client tools
|
|
84
|
+
spindb deps check
|
|
85
|
+
|
|
86
|
+
# Install missing tools (uses Homebrew, apt, yum, dnf, or pacman)
|
|
87
|
+
spindb deps install
|
|
88
|
+
|
|
89
|
+
# Install for a specific engine
|
|
90
|
+
spindb deps install --engine postgresql
|
|
91
|
+
spindb deps install --engine mysql
|
|
92
|
+
|
|
93
|
+
# Install all missing dependencies for all engines
|
|
94
|
+
spindb deps install --all
|
|
95
|
+
|
|
96
|
+
# List all supported dependencies
|
|
97
|
+
spindb deps list
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### Manual Installation
|
|
101
|
+
|
|
102
|
+
If automatic installation doesn't work, install manually:
|
|
75
103
|
|
|
76
104
|
```bash
|
|
77
105
|
# macOS (Homebrew)
|
|
78
|
-
brew install
|
|
79
|
-
brew link --
|
|
106
|
+
brew install postgresql@17
|
|
107
|
+
brew link --overwrite postgresql@17
|
|
80
108
|
|
|
81
109
|
# Ubuntu/Debian
|
|
82
|
-
apt install postgresql-client
|
|
110
|
+
sudo apt install postgresql-client
|
|
111
|
+
|
|
112
|
+
# CentOS/RHEL
|
|
113
|
+
sudo yum install postgresql
|
|
114
|
+
|
|
115
|
+
# Fedora
|
|
116
|
+
sudo dnf install postgresql
|
|
117
|
+
|
|
118
|
+
# Arch
|
|
119
|
+
sudo pacman -S postgresql-libs
|
|
83
120
|
|
|
84
121
|
# Or use Postgres.app (macOS)
|
|
85
122
|
# Client tools are automatically detected
|
|
@@ -123,16 +160,33 @@ spindb create mydb --database my_app_db
|
|
|
123
160
|
# Connection string: postgresql://postgres@localhost:5432/my_app_db
|
|
124
161
|
```
|
|
125
162
|
|
|
126
|
-
###
|
|
163
|
+
### Create and restore in one command
|
|
127
164
|
|
|
128
165
|
```bash
|
|
129
|
-
#
|
|
130
|
-
spindb
|
|
166
|
+
# Create a container and restore from a dump file
|
|
167
|
+
spindb create mydb --from ./backup.dump
|
|
168
|
+
|
|
169
|
+
# Create a container and pull from a remote database
|
|
170
|
+
spindb create mydb --from "postgresql://user:pass@remote-host:5432/production_db"
|
|
171
|
+
|
|
172
|
+
# With specific version and database name
|
|
173
|
+
spindb create mydb --pg-version 17 --database myapp --from ./backup.dump
|
|
174
|
+
```
|
|
131
175
|
|
|
132
|
-
|
|
176
|
+
The `--from` option auto-detects whether the location is a file path or connection string.
|
|
177
|
+
|
|
178
|
+
### Restore to an existing container
|
|
179
|
+
|
|
180
|
+
```bash
|
|
181
|
+
# Restore from a dump file (supports .sql, custom format, and tar format)
|
|
133
182
|
spindb restore mydb ./backup.dump -d myapp
|
|
183
|
+
|
|
184
|
+
# Or pull directly from a remote database
|
|
185
|
+
spindb restore mydb --from-url "postgresql://user:pass@remote-host:5432/production_db" -d myapp
|
|
134
186
|
```
|
|
135
187
|
|
|
188
|
+
The interactive menu (`spindb` → "Restore backup") also offers an option to create a new container as part of the restore flow.
|
|
189
|
+
|
|
136
190
|
### Clone for testing
|
|
137
191
|
|
|
138
192
|
```bash
|
package/cli/commands/create.ts
CHANGED
|
@@ -1,12 +1,45 @@
|
|
|
1
1
|
import { Command } from 'commander'
|
|
2
|
+
import { existsSync } from 'fs'
|
|
3
|
+
import { rm } from 'fs/promises'
|
|
2
4
|
import chalk from 'chalk'
|
|
3
5
|
import { containerManager } from '../../core/container-manager'
|
|
4
6
|
import { portManager } from '../../core/port-manager'
|
|
5
7
|
import { getEngine } from '../../engines'
|
|
6
8
|
import { defaults } from '../../config/defaults'
|
|
7
|
-
import {
|
|
9
|
+
import {
|
|
10
|
+
promptCreateOptions,
|
|
11
|
+
promptInstallDependencies,
|
|
12
|
+
promptContainerName,
|
|
13
|
+
} from '../ui/prompts'
|
|
8
14
|
import { createSpinner } from '../ui/spinner'
|
|
9
15
|
import { header, error, connectionBox } from '../ui/theme'
|
|
16
|
+
import { tmpdir } from 'os'
|
|
17
|
+
import { join } from 'path'
|
|
18
|
+
import { spawn } from 'child_process'
|
|
19
|
+
import { platform } from 'os'
|
|
20
|
+
import { getMissingDependencies } from '../../core/dependency-manager'
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Detect if a location string is a connection string or a file path
|
|
24
|
+
*/
|
|
25
|
+
function detectLocationType(
|
|
26
|
+
location: string,
|
|
27
|
+
): 'connection' | 'file' | 'not_found' {
|
|
28
|
+
// Check if it's a connection string
|
|
29
|
+
if (
|
|
30
|
+
location.startsWith('postgresql://') ||
|
|
31
|
+
location.startsWith('postgres://')
|
|
32
|
+
) {
|
|
33
|
+
return 'connection'
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Check if file exists
|
|
37
|
+
if (existsSync(location)) {
|
|
38
|
+
return 'file'
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return 'not_found'
|
|
42
|
+
}
|
|
10
43
|
|
|
11
44
|
export const createCommand = new Command('create')
|
|
12
45
|
.description('Create a new database container')
|
|
@@ -20,6 +53,10 @@ export const createCommand = new Command('create')
|
|
|
20
53
|
.option('-d, --database <database>', 'Database name')
|
|
21
54
|
.option('-p, --port <port>', 'Port number')
|
|
22
55
|
.option('--no-start', 'Do not start the container after creation')
|
|
56
|
+
.option(
|
|
57
|
+
'--from <location>',
|
|
58
|
+
'Restore from a dump file or connection string after creation',
|
|
59
|
+
)
|
|
23
60
|
.action(
|
|
24
61
|
async (
|
|
25
62
|
name: string | undefined,
|
|
@@ -29,8 +66,11 @@ export const createCommand = new Command('create')
|
|
|
29
66
|
database?: string
|
|
30
67
|
port?: string
|
|
31
68
|
start: boolean
|
|
69
|
+
from?: string
|
|
32
70
|
},
|
|
33
71
|
) => {
|
|
72
|
+
let tempDumpPath: string | null = null
|
|
73
|
+
|
|
34
74
|
try {
|
|
35
75
|
let containerName = name
|
|
36
76
|
let engine = options.engine
|
|
@@ -49,12 +89,80 @@ export const createCommand = new Command('create')
|
|
|
49
89
|
// Default database name to container name if not specified
|
|
50
90
|
database = database ?? containerName
|
|
51
91
|
|
|
92
|
+
// Validate --from location if provided
|
|
93
|
+
let restoreLocation: string | null = null
|
|
94
|
+
let restoreType: 'connection' | 'file' | null = null
|
|
95
|
+
|
|
96
|
+
if (options.from) {
|
|
97
|
+
const locationType = detectLocationType(options.from)
|
|
98
|
+
|
|
99
|
+
if (locationType === 'not_found') {
|
|
100
|
+
console.error(error(`Location not found: ${options.from}`))
|
|
101
|
+
console.log(
|
|
102
|
+
chalk.gray(
|
|
103
|
+
' Provide a valid file path or connection string (postgresql://...)',
|
|
104
|
+
),
|
|
105
|
+
)
|
|
106
|
+
process.exit(1)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
restoreLocation = options.from
|
|
110
|
+
restoreType = locationType
|
|
111
|
+
|
|
112
|
+
// If using --from, we must start the container
|
|
113
|
+
if (options.start === false) {
|
|
114
|
+
console.error(
|
|
115
|
+
error(
|
|
116
|
+
'Cannot use --no-start with --from (restore requires running container)',
|
|
117
|
+
),
|
|
118
|
+
)
|
|
119
|
+
process.exit(1)
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
52
123
|
console.log(header('Creating Database Container'))
|
|
53
124
|
console.log()
|
|
54
125
|
|
|
55
126
|
// Get the engine
|
|
56
127
|
const dbEngine = getEngine(engine)
|
|
57
128
|
|
|
129
|
+
// Check for required client tools BEFORE creating anything
|
|
130
|
+
const depsSpinner = createSpinner('Checking required tools...')
|
|
131
|
+
depsSpinner.start()
|
|
132
|
+
|
|
133
|
+
let missingDeps = await getMissingDependencies(engine)
|
|
134
|
+
if (missingDeps.length > 0) {
|
|
135
|
+
depsSpinner.warn(
|
|
136
|
+
`Missing tools: ${missingDeps.map((d) => d.name).join(', ')}`,
|
|
137
|
+
)
|
|
138
|
+
|
|
139
|
+
// Offer to install
|
|
140
|
+
const installed = await promptInstallDependencies(
|
|
141
|
+
missingDeps[0].binary,
|
|
142
|
+
engine,
|
|
143
|
+
)
|
|
144
|
+
|
|
145
|
+
if (!installed) {
|
|
146
|
+
process.exit(1)
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// Verify installation worked
|
|
150
|
+
missingDeps = await getMissingDependencies(engine)
|
|
151
|
+
if (missingDeps.length > 0) {
|
|
152
|
+
console.error(
|
|
153
|
+
error(
|
|
154
|
+
`Still missing tools: ${missingDeps.map((d) => d.name).join(', ')}`,
|
|
155
|
+
),
|
|
156
|
+
)
|
|
157
|
+
process.exit(1)
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
console.log(chalk.green(' ✓ All required tools are now available'))
|
|
161
|
+
console.log()
|
|
162
|
+
} else {
|
|
163
|
+
depsSpinner.succeed('Required tools available')
|
|
164
|
+
}
|
|
165
|
+
|
|
58
166
|
// Find available port
|
|
59
167
|
const portSpinner = createSpinner('Finding available port...')
|
|
60
168
|
portSpinner.start()
|
|
@@ -96,6 +204,14 @@ export const createCommand = new Command('create')
|
|
|
96
204
|
binarySpinner.succeed(`PostgreSQL ${version} binaries downloaded`)
|
|
97
205
|
}
|
|
98
206
|
|
|
207
|
+
// Check if container name already exists and prompt for new name if needed
|
|
208
|
+
while (await containerManager.exists(containerName)) {
|
|
209
|
+
console.log(
|
|
210
|
+
chalk.yellow(` Container "${containerName}" already exists.`),
|
|
211
|
+
)
|
|
212
|
+
containerName = await promptContainerName()
|
|
213
|
+
}
|
|
214
|
+
|
|
99
215
|
// Create container
|
|
100
216
|
const createSpinnerInstance = createSpinner('Creating container...')
|
|
101
217
|
createSpinnerInstance.start()
|
|
@@ -145,6 +261,102 @@ export const createCommand = new Command('create')
|
|
|
145
261
|
|
|
146
262
|
dbSpinner.succeed(`Database "${database}" created`)
|
|
147
263
|
}
|
|
264
|
+
|
|
265
|
+
// Handle --from restore if specified
|
|
266
|
+
if (restoreLocation && restoreType && config) {
|
|
267
|
+
let backupPath = ''
|
|
268
|
+
|
|
269
|
+
if (restoreType === 'connection') {
|
|
270
|
+
// Create dump from remote database
|
|
271
|
+
const timestamp = Date.now()
|
|
272
|
+
tempDumpPath = join(tmpdir(), `spindb-dump-${timestamp}.dump`)
|
|
273
|
+
|
|
274
|
+
let dumpSuccess = false
|
|
275
|
+
let attempts = 0
|
|
276
|
+
const maxAttempts = 2 // Allow one retry after installing deps
|
|
277
|
+
|
|
278
|
+
while (!dumpSuccess && attempts < maxAttempts) {
|
|
279
|
+
attempts++
|
|
280
|
+
const dumpSpinner = createSpinner(
|
|
281
|
+
'Creating dump from remote database...',
|
|
282
|
+
)
|
|
283
|
+
dumpSpinner.start()
|
|
284
|
+
|
|
285
|
+
try {
|
|
286
|
+
await dbEngine.dumpFromConnectionString(
|
|
287
|
+
restoreLocation,
|
|
288
|
+
tempDumpPath,
|
|
289
|
+
)
|
|
290
|
+
dumpSpinner.succeed('Dump created from remote database')
|
|
291
|
+
backupPath = tempDumpPath
|
|
292
|
+
dumpSuccess = true
|
|
293
|
+
} catch (err) {
|
|
294
|
+
const e = err as Error
|
|
295
|
+
dumpSpinner.fail('Failed to create dump')
|
|
296
|
+
|
|
297
|
+
// Check if this is a missing tool error
|
|
298
|
+
if (
|
|
299
|
+
e.message.includes('pg_dump not found') ||
|
|
300
|
+
e.message.includes('ENOENT')
|
|
301
|
+
) {
|
|
302
|
+
const installed = await promptInstallDependencies('pg_dump')
|
|
303
|
+
if (!installed) {
|
|
304
|
+
process.exit(1)
|
|
305
|
+
}
|
|
306
|
+
// Loop will retry
|
|
307
|
+
continue
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
console.log()
|
|
311
|
+
console.error(error('pg_dump error:'))
|
|
312
|
+
console.log(chalk.gray(` ${e.message}`))
|
|
313
|
+
process.exit(1)
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
// Safety check - should never reach here without backupPath set
|
|
318
|
+
if (!dumpSuccess) {
|
|
319
|
+
console.error(error('Failed to create dump after retries'))
|
|
320
|
+
process.exit(1)
|
|
321
|
+
}
|
|
322
|
+
} else {
|
|
323
|
+
backupPath = restoreLocation
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
// Detect backup format
|
|
327
|
+
const detectSpinner = createSpinner('Detecting backup format...')
|
|
328
|
+
detectSpinner.start()
|
|
329
|
+
|
|
330
|
+
const format = await dbEngine.detectBackupFormat(backupPath)
|
|
331
|
+
detectSpinner.succeed(`Detected: ${format.description}`)
|
|
332
|
+
|
|
333
|
+
// Restore backup
|
|
334
|
+
const restoreSpinner = createSpinner('Restoring backup...')
|
|
335
|
+
restoreSpinner.start()
|
|
336
|
+
|
|
337
|
+
const result = await dbEngine.restore(config, backupPath, {
|
|
338
|
+
database,
|
|
339
|
+
createDatabase: false, // Already created above
|
|
340
|
+
})
|
|
341
|
+
|
|
342
|
+
if (result.code === 0 || !result.stderr) {
|
|
343
|
+
restoreSpinner.succeed('Backup restored successfully')
|
|
344
|
+
} else {
|
|
345
|
+
restoreSpinner.warn('Restore completed with warnings')
|
|
346
|
+
if (result.stderr) {
|
|
347
|
+
console.log(chalk.yellow('\n Warnings:'))
|
|
348
|
+
const lines = result.stderr.split('\n').slice(0, 5)
|
|
349
|
+
lines.forEach((line) => {
|
|
350
|
+
if (line.trim()) {
|
|
351
|
+
console.log(chalk.gray(` ${line}`))
|
|
352
|
+
}
|
|
353
|
+
})
|
|
354
|
+
if (result.stderr.split('\n').length > 5) {
|
|
355
|
+
console.log(chalk.gray(' ...'))
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
}
|
|
148
360
|
}
|
|
149
361
|
|
|
150
362
|
// Show success message
|
|
@@ -157,12 +369,74 @@ export const createCommand = new Command('create')
|
|
|
157
369
|
console.log()
|
|
158
370
|
console.log(chalk.gray(' Connect with:'))
|
|
159
371
|
console.log(chalk.cyan(` spindb connect ${containerName}`))
|
|
372
|
+
|
|
373
|
+
// Copy connection string to clipboard
|
|
374
|
+
if (options.start !== false) {
|
|
375
|
+
try {
|
|
376
|
+
const cmd = platform() === 'darwin' ? 'pbcopy' : 'xclip'
|
|
377
|
+
const args =
|
|
378
|
+
platform() === 'darwin' ? [] : ['-selection', 'clipboard']
|
|
379
|
+
|
|
380
|
+
await new Promise<void>((resolve, reject) => {
|
|
381
|
+
const proc = spawn(cmd, args, {
|
|
382
|
+
stdio: ['pipe', 'inherit', 'inherit'],
|
|
383
|
+
})
|
|
384
|
+
proc.stdin?.write(connectionString)
|
|
385
|
+
proc.stdin?.end()
|
|
386
|
+
proc.on('close', (code) => {
|
|
387
|
+
if (code === 0) resolve()
|
|
388
|
+
else
|
|
389
|
+
reject(
|
|
390
|
+
new Error(`Clipboard command exited with code ${code}`),
|
|
391
|
+
)
|
|
392
|
+
})
|
|
393
|
+
proc.on('error', reject)
|
|
394
|
+
})
|
|
395
|
+
|
|
396
|
+
console.log(chalk.gray(' Connection string copied to clipboard'))
|
|
397
|
+
} catch {
|
|
398
|
+
// Ignore clipboard errors
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
|
|
160
402
|
console.log()
|
|
161
403
|
}
|
|
162
404
|
} catch (err) {
|
|
163
405
|
const e = err as Error
|
|
406
|
+
|
|
407
|
+
// Check if this is a missing tool error
|
|
408
|
+
if (
|
|
409
|
+
e.message.includes('pg_restore not found') ||
|
|
410
|
+
e.message.includes('psql not found') ||
|
|
411
|
+
e.message.includes('pg_dump not found')
|
|
412
|
+
) {
|
|
413
|
+
const missingTool = e.message.includes('pg_restore')
|
|
414
|
+
? 'pg_restore'
|
|
415
|
+
: e.message.includes('pg_dump')
|
|
416
|
+
? 'pg_dump'
|
|
417
|
+
: 'psql'
|
|
418
|
+
const installed = await promptInstallDependencies(missingTool)
|
|
419
|
+
if (installed) {
|
|
420
|
+
console.log(
|
|
421
|
+
chalk.yellow(
|
|
422
|
+
' Please re-run your command to continue.',
|
|
423
|
+
),
|
|
424
|
+
)
|
|
425
|
+
}
|
|
426
|
+
process.exit(1)
|
|
427
|
+
}
|
|
428
|
+
|
|
164
429
|
console.error(error(e.message))
|
|
165
430
|
process.exit(1)
|
|
431
|
+
} finally {
|
|
432
|
+
// Clean up temp file if we created one
|
|
433
|
+
if (tempDumpPath) {
|
|
434
|
+
try {
|
|
435
|
+
await rm(tempDumpPath, { force: true })
|
|
436
|
+
} catch {
|
|
437
|
+
// Ignore cleanup errors
|
|
438
|
+
}
|
|
439
|
+
}
|
|
166
440
|
}
|
|
167
441
|
},
|
|
168
442
|
)
|