extract-mysql-schema 1.0.0 → 1.0.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 +221 -1
- package/{run.js → bin.js} +2 -2
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1 +1,221 @@
|
|
|
1
|
-
# extract-mysql-schema
|
|
1
|
+
# extract-mysql-schema
|
|
2
|
+
#### MySQL Schema Extractor
|
|
3
|
+
|
|
4
|
+
A command-line tool to extract MySQL database schemas and generate SQL files. This tool connects to a MySQL database, extracts table structures, stored procedures, and generates comprehensive SQL files that can be used for documentation, backup, or database replication.
|
|
5
|
+
|
|
6
|
+
## Installation
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
npm install extract-mysql-schema -g
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## Requirements
|
|
13
|
+
|
|
14
|
+
- Node.js
|
|
15
|
+
- MySQL database access
|
|
16
|
+
- A configuration file with database connection details
|
|
17
|
+
|
|
18
|
+
## Configuration
|
|
19
|
+
|
|
20
|
+
Create a configuration file (e.g., `config.js`) with your MySQL connection details:
|
|
21
|
+
|
|
22
|
+
```javascript
|
|
23
|
+
module.exports = {
|
|
24
|
+
connection: {
|
|
25
|
+
host: 'localhost',
|
|
26
|
+
user: 'your_username',
|
|
27
|
+
password: 'your_password',
|
|
28
|
+
database: 'your_database_name', // required
|
|
29
|
+
port: 3306,
|
|
30
|
+
location: "../connections.json" // optional - connections can be in external file
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### External Connections File
|
|
36
|
+
|
|
37
|
+
password: 'your_password',
|
|
38
|
+
password: 'your_password',
|
|
39
|
+
Alternatively, you can use an external JSON file to store multiple database connections. When using the `location` property in your config, the tool will look up the database by "database" name in the external file:
|
|
40
|
+
|
|
41
|
+
**connections.json:**
|
|
42
|
+
```json
|
|
43
|
+
{
|
|
44
|
+
"your_database_name": {
|
|
45
|
+
"engine": "mysql",
|
|
46
|
+
"host": "localhost",
|
|
47
|
+
"user": "your_username",
|
|
48
|
+
"password": "your_password",
|
|
49
|
+
"database": "your_database_name",
|
|
50
|
+
"charset": "utf8"
|
|
51
|
+
},
|
|
52
|
+
"production_db": {
|
|
53
|
+
"engine": "mysql",
|
|
54
|
+
"host": "prod-server.example.com",
|
|
55
|
+
"user": "prod_user",
|
|
56
|
+
"password": "prod_password",
|
|
57
|
+
"database": "production_db",
|
|
58
|
+
"charset": "utf8"
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
When using an external connections file, the tool will use the `database` property from your config to find the matching connection in the external file.
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
## Command Line Usage
|
|
68
|
+
|
|
69
|
+
### Basic Syntax
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
extract-mysql-schema --configFile <path-to-config> [options]
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### Required Options
|
|
76
|
+
|
|
77
|
+
- `--configFile <path>` - Path to your configuration file (required)
|
|
78
|
+
|
|
79
|
+
### Optional Options
|
|
80
|
+
|
|
81
|
+
- `--outputFile <path>` - Write JSON schema output to a file instead of console
|
|
82
|
+
- `--debug` - Enable debug mode for detailed logging
|
|
83
|
+
- `--procedureISV` - Include Information Schema Values for stored procedures
|
|
84
|
+
- `--writeSql` - Generate SQL files organized by type
|
|
85
|
+
|
|
86
|
+
## Examples
|
|
87
|
+
|
|
88
|
+
### Extract schema and output JSON to console
|
|
89
|
+
|
|
90
|
+
```bash
|
|
91
|
+
extract-mysql-schema --configFile config.js
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### Extract schema and save JSON to file
|
|
95
|
+
|
|
96
|
+
```bash
|
|
97
|
+
extract-mysql-schema --configFile config.js --outputFile schema.json
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### Generate SQL files
|
|
101
|
+
|
|
102
|
+
```bash
|
|
103
|
+
extract-mysql-schema --configFile config.js --writeSql
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### Extract with all options enabled
|
|
107
|
+
|
|
108
|
+
```bash
|
|
109
|
+
extract-mysql-schema --configFile config.js --outputFile schema.json --writeSql --debug --procedureISV
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### Using a relative path for config
|
|
113
|
+
|
|
114
|
+
```bash
|
|
115
|
+
extract-mysql-schema --configFile ./config/database.js --outputFile ./output/schema.json
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## Output Files
|
|
119
|
+
|
|
120
|
+
When using the `--writeSql` option, the tool generates several SQL files:
|
|
121
|
+
|
|
122
|
+
### Individual Files
|
|
123
|
+
|
|
124
|
+
- **`tables/`** - Directory containing individual table definition files
|
|
125
|
+
- Each table gets its own `.sql` file (e.g., `users.sql`, `orders.sql`)
|
|
126
|
+
|
|
127
|
+
- **`procedures/`** - Directory containing individual stored procedure files
|
|
128
|
+
- Each procedure gets its own `.sql` file
|
|
129
|
+
|
|
130
|
+
- **`seed/`** - Directory for seed data files (if they exist)
|
|
131
|
+
- Reads existing seed files and includes them in output
|
|
132
|
+
|
|
133
|
+
- **`patch/`** - Directory for database patches (if they exist)
|
|
134
|
+
- Reads existing patch files and includes them in output
|
|
135
|
+
|
|
136
|
+
### Consolidated Files
|
|
137
|
+
|
|
138
|
+
- **`0.init.sql`** - Database initialization (CREATE DATABASE and USE statements)
|
|
139
|
+
- **`1.table.sql`** - All table definitions in proper dependency order
|
|
140
|
+
- **`2.seed.sql`** - All seed data
|
|
141
|
+
- **`3.procedures.sql`** - All stored procedures
|
|
142
|
+
- **`4.patch.sql`** - All database patches
|
|
143
|
+
- **`init.sql`** - Complete database script (combines all of the above)
|
|
144
|
+
|
|
145
|
+
## JSON Output Structure
|
|
146
|
+
|
|
147
|
+
The JSON output contains:
|
|
148
|
+
|
|
149
|
+
```json
|
|
150
|
+
{
|
|
151
|
+
"database_name": {
|
|
152
|
+
"name": "database_name",
|
|
153
|
+
"tables": [
|
|
154
|
+
{
|
|
155
|
+
"name": "table_name",
|
|
156
|
+
"schemaName": "database_name",
|
|
157
|
+
"kind": "table",
|
|
158
|
+
"columns": [...],
|
|
159
|
+
"definition": "CREATE TABLE SQL..."
|
|
160
|
+
}
|
|
161
|
+
],
|
|
162
|
+
"tableOrder": ["table1", "table2", ...],
|
|
163
|
+
"views": [...],
|
|
164
|
+
"procedures": [...]
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
### Column Properties
|
|
170
|
+
|
|
171
|
+
Each column includes:
|
|
172
|
+
- `name` - Column name
|
|
173
|
+
- `ordinalPosition` - Position in table
|
|
174
|
+
- `sqltype` - Full SQL type definition
|
|
175
|
+
- `type` - Base data type
|
|
176
|
+
- `maxLength` - Maximum length (for strings)
|
|
177
|
+
- `isPrimaryKey` - Boolean
|
|
178
|
+
- `isCompoundKey` - Boolean (part of compound key)
|
|
179
|
+
- `isNullable` - Boolean
|
|
180
|
+
- `isAutoNumber` - Boolean (auto_increment)
|
|
181
|
+
- `generated` - Generation type: "STORED", "ALWAYS", "BY DEFAULT", or "NEVER"
|
|
182
|
+
- `expression` - Generation expression (if applicable)
|
|
183
|
+
- `isUpdatable` - Boolean
|
|
184
|
+
- `defaultValue` - Default value
|
|
185
|
+
- `references` - Array of foreign key relationships
|
|
186
|
+
|
|
187
|
+
### Procedure Properties
|
|
188
|
+
|
|
189
|
+
Each procedure includes:
|
|
190
|
+
- `name` - Procedure name
|
|
191
|
+
- `schemaName` - Database name
|
|
192
|
+
- `kind` - "procedure"
|
|
193
|
+
- `definition` - Complete CREATE PROCEDURE statement
|
|
194
|
+
- `params` - Array of parameter definitions
|
|
195
|
+
|
|
196
|
+
## Table Dependency Ordering
|
|
197
|
+
|
|
198
|
+
The tool automatically determines the correct order for creating tables based on foreign key dependencies. Tables without dependencies are created first, followed by dependent tables in the correct order. This ensures SQL files can be executed without foreign key constraint errors.
|
|
199
|
+
|
|
200
|
+
## Error Handling
|
|
201
|
+
|
|
202
|
+
If the command is run without required arguments:
|
|
203
|
+
```
|
|
204
|
+
Expected at least one argument!
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
If an unknown option is provided:
|
|
208
|
+
```
|
|
209
|
+
Expected a known option
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
## Notes
|
|
213
|
+
|
|
214
|
+
- Tables are organized in dependency order to respect foreign key constraints
|
|
215
|
+
- Generated SQL files use `IF NOT EXISTS` and `IF EXISTS` for safe re-execution
|
|
216
|
+
- All paths are relative to the current working directory
|
|
217
|
+
- The tool handles compound keys, auto-increment fields, and generated columns
|
|
218
|
+
|
|
219
|
+
## License
|
|
220
|
+
|
|
221
|
+
See [LICENSE](LICENSE) file for details.
|
package/{run.js → bin.js}
RENAMED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
1
3
|
const fs = require("fs");
|
|
2
4
|
const path = require("path");
|
|
3
5
|
const { extractSchemas } = require('./index.js');
|
|
@@ -117,7 +119,6 @@ let options = {
|
|
|
117
119
|
outputFile:"",
|
|
118
120
|
debug:false,
|
|
119
121
|
columnISV:true,
|
|
120
|
-
tableISV:false,
|
|
121
122
|
procedureISV:false,
|
|
122
123
|
writeSql:false
|
|
123
124
|
}
|
|
@@ -131,7 +132,6 @@ let argv = process.argv;
|
|
|
131
132
|
for(let i=2;i<argv.length;i++) {
|
|
132
133
|
if(argv[i]==="--columnISV") options.columnISV=true;
|
|
133
134
|
else if(argv[i]==="--debug") options.debug=true;
|
|
134
|
-
else if(argv[i]==="--tableISV") options.tableISV=true;
|
|
135
135
|
else if(argv[i]==="--procedureISV") options.procedureISV=true;
|
|
136
136
|
else if(argv[i]==="--writeSql") options.writeSql=true;
|
|
137
137
|
else
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "extract-mysql-schema",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "index.js",
|
|
6
|
-
"bin": "./
|
|
6
|
+
"bin": "./bin.js",
|
|
7
7
|
"repository": {
|
|
8
8
|
"type": "git",
|
|
9
9
|
"url": "https://github.com/cdotyone/extract-mysql-schema.git"
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"registry": "https://registry.npmjs.org"
|
|
13
13
|
},
|
|
14
14
|
"scripts": {
|
|
15
|
-
"test": "node ./
|
|
15
|
+
"test": "node ./bin.js"
|
|
16
16
|
},
|
|
17
17
|
"keywords": [],
|
|
18
18
|
"author": "Chris Doty",
|