database-model-generator 0.6.0
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.
- checksums.yaml +7 -0
- checksums.yaml.gz.sig +3 -0
- data/CHANGES.md +59 -0
- data/Gemfile +2 -0
- data/LICENSE +177 -0
- data/MANIFEST.md +10 -0
- data/README.md +186 -0
- data/Rakefile +32 -0
- data/bin/dmg +831 -0
- data/certs/djberg96_pub.pem +26 -0
- data/database-model-generator.gemspec +31 -0
- data/docker/README.md +238 -0
- data/docker/oracle/Dockerfile +87 -0
- data/docker/oracle/README.md +140 -0
- data/docker/oracle/docker-compose.yml +41 -0
- data/docker/oracle/test.sh +45 -0
- data/docker/sqlserver/DOCKER.md +152 -0
- data/docker/sqlserver/Dockerfile +29 -0
- data/docker/sqlserver/SUPPORT.md +477 -0
- data/docker/sqlserver/TESTING.md +194 -0
- data/docker/sqlserver/docker-compose.yml +52 -0
- data/docker/sqlserver/init-db.sql +158 -0
- data/docker/sqlserver/run_tests.sh +154 -0
- data/docker/sqlserver/setup-db.sh +9 -0
- data/docker/sqlserver/test-Dockerfile +36 -0
- data/docker/sqlserver/test.sh +56 -0
- data/lib/database_model_generator.rb +652 -0
- data/lib/oracle/model/generator.rb +287 -0
- data/lib/sqlserver/model/generator.rb +281 -0
- data/spec/oracle_model_generator_spec.rb +176 -0
- data/spec/spec_helper.rb +30 -0
- data/spec/support/oracle_connection.rb +126 -0
- data.tar.gz.sig +0 -0
- metadata +162 -0
- metadata.gz.sig +0 -0
@@ -0,0 +1,152 @@
|
|
1
|
+
# Docker Testing Environment for Oracle Model Generator
|
2
|
+
|
3
|
+
This directory contains Docker configuration files to create a complete testing environment for the oracle-model-generator library.
|
4
|
+
|
5
|
+
## Quick Start
|
6
|
+
|
7
|
+
### Using Docker Compose (Recommended)
|
8
|
+
|
9
|
+
The easiest way to test the library is using Docker Compose, which will start both an Oracle database and the testing environment:
|
10
|
+
|
11
|
+
```bash
|
12
|
+
# Build and start the services
|
13
|
+
docker-compose up --build
|
14
|
+
|
15
|
+
# This will:
|
16
|
+
# 1. Download and start Oracle Express Edition
|
17
|
+
# 2. Build the oracle-model-generator testing image
|
18
|
+
# 3. Set up the HR sample schema
|
19
|
+
# 4. Start an interactive shell in the testing container
|
20
|
+
```
|
21
|
+
|
22
|
+
### Manual Docker Build
|
23
|
+
|
24
|
+
If you prefer to build just the testing image:
|
25
|
+
|
26
|
+
```bash
|
27
|
+
# Build the Docker image
|
28
|
+
docker build -t oracle-model-generator-test .
|
29
|
+
|
30
|
+
# Run with a separate Oracle database
|
31
|
+
docker run -it --rm \
|
32
|
+
-e ORACLE_HOST=your-oracle-host \
|
33
|
+
-e ORACLE_PORT=1521 \
|
34
|
+
-e ORACLE_USER=hr \
|
35
|
+
-e ORACLE_PASSWORD=hr \
|
36
|
+
oracle-model-generator-test
|
37
|
+
```
|
38
|
+
|
39
|
+
## Running Tests
|
40
|
+
|
41
|
+
Once inside the container, you can run various commands:
|
42
|
+
|
43
|
+
```bash
|
44
|
+
# Run the Docker environment test to verify everything is working
|
45
|
+
bundle exec ruby test/docker_environment_test.rb
|
46
|
+
|
47
|
+
# Run the test suite
|
48
|
+
bundle exec ruby test/test_oracle_model_generator.rb
|
49
|
+
|
50
|
+
# Test the command-line tool
|
51
|
+
bundle exec ruby -Ilib bin/dmg --help
|
52
|
+
|
53
|
+
# Interactive Ruby with the library loaded
|
54
|
+
irb -I lib -r oracle/model/generator
|
55
|
+
```
|
56
|
+
|
57
|
+
## Environment Variables
|
58
|
+
|
59
|
+
The Docker environment supports these environment variables:
|
60
|
+
|
61
|
+
- `ORACLE_HOST`: Oracle database hostname (default: localhost)
|
62
|
+
- `ORACLE_PORT`: Oracle database port (default: 1521)
|
63
|
+
- `ORACLE_SID`: Oracle database SID (default: XE)
|
64
|
+
- `ORACLE_USER`: Database username (default: hr)
|
65
|
+
- `ORACLE_PASSWORD`: Database password (default: hr)
|
66
|
+
|
67
|
+
## Summary
|
68
|
+
|
69
|
+
The Docker setup provides a robust testing environment for the Oracle Model Generator that:
|
70
|
+
|
71
|
+
- ✅ Works on all platforms (Linux, macOS, Windows)
|
72
|
+
- ✅ Includes Oracle Instant Client and ruby-oci8
|
73
|
+
- ✅ Has comprehensive environment verification
|
74
|
+
- ✅ Supports both standalone and full-stack testing
|
75
|
+
- ✅ Uses optimized gem installation for containers
|
76
|
+
- ⚠️ Oracle XE database has some macOS compatibility limitations
|
77
|
+
|
78
|
+
This gives you a reliable way to test Oracle connectivity without installing Oracle client libraries on your host system.
|
79
|
+
|
80
|
+
## Troubleshooting
|
81
|
+
|
82
|
+
### macOS Docker Issues
|
83
|
+
|
84
|
+
On macOS, you might encounter shared memory issues with the Oracle XE container:
|
85
|
+
|
86
|
+
```
|
87
|
+
ORA-27104: system-defined limits for shared memory was misconfigured
|
88
|
+
```
|
89
|
+
|
90
|
+
**Workaround**: Test the application container directly:
|
91
|
+
|
92
|
+
```bash
|
93
|
+
# Build and test just the application
|
94
|
+
docker build -t oracle-model-generator-test .
|
95
|
+
|
96
|
+
# Run the environment verification test
|
97
|
+
docker run --rm oracle-model-generator-test bundle exec ruby test/docker_environment_test.rb
|
98
|
+
|
99
|
+
# Interactive testing without database
|
100
|
+
docker run -it --rm oracle-model-generator-test bash
|
101
|
+
```
|
102
|
+
|
103
|
+
### Connection Issues
|
104
|
+
|
105
|
+
If you get Oracle connection errors:
|
106
|
+
|
107
|
+
1. Check that the Oracle database is running: `docker-compose ps`
|
108
|
+
2. Wait for the database to be fully initialized (can take 2-3 minutes)
|
109
|
+
3. Verify connection parameters match your setup
|
110
|
+
|
111
|
+
### Build Issues
|
112
|
+
|
113
|
+
If the Docker build fails:
|
114
|
+
|
115
|
+
1. Make sure you have internet access (downloads Oracle Instant Client)
|
116
|
+
2. Try rebuilding: `docker-compose build --no-cache`
|
117
|
+
|
118
|
+
### Test Failures
|
119
|
+
|
120
|
+
If tests fail but connection works:
|
121
|
+
|
122
|
+
1. Verify the HR schema is properly installed
|
123
|
+
2. Check that sample data is present
|
124
|
+
3. Ensure the user has necessary permissions
|
125
|
+
|
126
|
+
## Development
|
127
|
+
|
128
|
+
To develop/debug the library using Docker:
|
129
|
+
|
130
|
+
```bash
|
131
|
+
# Start the container with a bash shell
|
132
|
+
docker-compose run oracle-model-generator bash
|
133
|
+
|
134
|
+
# Or mount the local directory for live editing
|
135
|
+
docker run -it --rm -v $(pwd):/app oracle-model-generator-test bash
|
136
|
+
```
|
137
|
+
|
138
|
+
## Cleaning Up
|
139
|
+
|
140
|
+
To remove all Docker resources:
|
141
|
+
|
142
|
+
```bash
|
143
|
+
# Stop and remove containers
|
144
|
+
docker-compose down
|
145
|
+
|
146
|
+
# Also remove volumes (this will delete the Oracle database data)
|
147
|
+
docker-compose down -v
|
148
|
+
|
149
|
+
# Remove the built images
|
150
|
+
docker rmi oracle-model-generator-test
|
151
|
+
docker rmi gvenzl/oracle-xe:21-slim
|
152
|
+
```
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# SQL Server Docker setup for testing Database Model Generator
|
2
|
+
FROM mcr.microsoft.com/mssql/server:2022-latest
|
3
|
+
|
4
|
+
# Set environment variables
|
5
|
+
ENV ACCEPT_EULA=Y
|
6
|
+
ENV SA_PASSWORD=YourStrong!Passw0rd
|
7
|
+
ENV MSSQL_PID=Express
|
8
|
+
|
9
|
+
# Expose SQL Server port
|
10
|
+
EXPOSE 1433
|
11
|
+
|
12
|
+
# Switch to root to create directories and copy files
|
13
|
+
USER root
|
14
|
+
|
15
|
+
# Create directory for initialization scripts
|
16
|
+
RUN mkdir -p /opt/mssql-tools/scripts
|
17
|
+
|
18
|
+
# Copy initialization scripts
|
19
|
+
COPY docker/sqlserver/init-db.sql /opt/mssql-tools/scripts/
|
20
|
+
COPY docker/sqlserver/setup-db.sh /opt/mssql-tools/scripts/
|
21
|
+
|
22
|
+
# Make setup script executable
|
23
|
+
RUN chmod +x /opt/mssql-tools/scripts/setup-db.sh
|
24
|
+
|
25
|
+
# Switch back to mssql user
|
26
|
+
USER mssql
|
27
|
+
|
28
|
+
# Run SQL Server and setup script
|
29
|
+
CMD /opt/mssql-tools/scripts/setup-db.sh & /opt/mssql/bin/sqlservr
|
@@ -0,0 +1,477 @@
|
|
1
|
+
# SQL Server Support
|
2
|
+
|
3
|
+
The Database Model Generator (formerly Oracle Model Generator) now supports both Oracle and Microsoft SQL Server databases, providing comprehensive ActiveRecord model generation with intelligent schema introspection and performance optimization recommendations.
|
4
|
+
|
5
|
+
## Overview
|
6
|
+
|
7
|
+
This tool automatically generates Rails ActiveRecord models from database schema with the following enhancements:
|
8
|
+
|
9
|
+
- **Dual Database Support**: Oracle and SQL Server
|
10
|
+
- **Schema Introspection**: Automatic table analysis
|
11
|
+
- **Performance Optimization**: Index recommendations
|
12
|
+
- **Test Generation**: RSpec, TestUnit, and Minitest support
|
13
|
+
- **Advanced Features**: Relationships, validations, scopes, enums, and callbacks
|
14
|
+
|
15
|
+
## Installation
|
16
|
+
|
17
|
+
### Prerequisites
|
18
|
+
|
19
|
+
```bash
|
20
|
+
# For Oracle support
|
21
|
+
gem install oci8
|
22
|
+
|
23
|
+
# For SQL Server support
|
24
|
+
gem install tiny_tds
|
25
|
+
|
26
|
+
# Core dependencies
|
27
|
+
gem install getopt
|
28
|
+
```
|
29
|
+
|
30
|
+
### SQL Server Requirements
|
31
|
+
|
32
|
+
- **Supported Versions**: SQL Server 2012, 2014, 2016, 2017, 2019, 2022
|
33
|
+
- **Connection**: Uses TinyTDS for native connectivity
|
34
|
+
- **Schema Access**: Requires read access to INFORMATION_SCHEMA views
|
35
|
+
- **Authentication**: Supports SQL Server Authentication and Windows Authentication
|
36
|
+
|
37
|
+
## Usage
|
38
|
+
|
39
|
+
### Basic Command Structure
|
40
|
+
|
41
|
+
```bash
|
42
|
+
# Explicit database type specification
|
43
|
+
./bin/dmg -T <type> [connection_options] -t <table> [other_options]
|
44
|
+
|
45
|
+
# Auto-detection based on connection parameters
|
46
|
+
./bin/dmg [connection_options] -t <table> [other_options]
|
47
|
+
```
|
48
|
+
|
49
|
+
### SQL Server Examples
|
50
|
+
|
51
|
+
#### Basic Model Generation
|
52
|
+
```bash
|
53
|
+
# Generate Employee model from SQL Server
|
54
|
+
./bin/dmg -T sqlserver -s localhost -P 1433 -d northwind -u sa -p password -t Employees
|
55
|
+
|
56
|
+
# Auto-detect SQL Server (when server specified)
|
57
|
+
./bin/dmg -s myserver -d adventureworks -u user -p pass -t Customers
|
58
|
+
```
|
59
|
+
|
60
|
+
#### Index Recommendations Only
|
61
|
+
```bash
|
62
|
+
# Show only performance recommendations
|
63
|
+
./bin/dmg -T sqlserver -s localhost -d mydb -u user -p pass -t Orders --indexes
|
64
|
+
```
|
65
|
+
|
66
|
+
#### With Custom Options
|
67
|
+
```bash
|
68
|
+
# Generate with specific Rails version and test framework
|
69
|
+
./bin/dmg -T sqlserver -s localhost -d mydb -u user -p pass -t Products -r 7 -x rspec -o product.rb
|
70
|
+
```
|
71
|
+
|
72
|
+
### Oracle Examples (Backward Compatible)
|
73
|
+
|
74
|
+
```bash
|
75
|
+
# Traditional Oracle connection
|
76
|
+
./bin/dmg -T oracle -d localhost:1521/XE -u hr -p hr -t employees
|
77
|
+
|
78
|
+
# Auto-detect Oracle (no server specified)
|
79
|
+
./bin/dmg -d localhost:1521/XE -u scott -p tiger -t users
|
80
|
+
```
|
81
|
+
|
82
|
+
## Connection Parameters
|
83
|
+
|
84
|
+
### SQL Server Connection Options
|
85
|
+
|
86
|
+
| Option | Short | Required | Description | Default |
|
87
|
+
|--------|--------|----------|-------------|---------|
|
88
|
+
| `--type` | `-T` | No | Database type (`sqlserver`) | Auto-detect |
|
89
|
+
| `--server` | `-s` | Yes* | SQL Server hostname | `localhost` |
|
90
|
+
| `--port` | `-P` | No | SQL Server port | `1433` |
|
91
|
+
| `--database` | `-d` | Yes | Database name | - |
|
92
|
+
| `--user` | `-u` | Yes | Username | - |
|
93
|
+
| `--password` | `-p` | Yes | Password | - |
|
94
|
+
|
95
|
+
*Required for SQL Server, triggers auto-detection
|
96
|
+
|
97
|
+
### Oracle Connection Options
|
98
|
+
|
99
|
+
| Option | Short | Required | Description | Default |
|
100
|
+
|--------|--------|----------|-------------|---------|
|
101
|
+
| `--type` | `-T` | No | Database type (`oracle`) | Auto-detect |
|
102
|
+
| `--database` | `-d` | Yes | TNS name or connection string | - |
|
103
|
+
| `--user` | `-u` | Yes | Username | - |
|
104
|
+
| `--password` | `-p` | Yes | Password | - |
|
105
|
+
|
106
|
+
## Generated Models
|
107
|
+
|
108
|
+
### SQL Server Model Example
|
109
|
+
|
110
|
+
```ruby
|
111
|
+
# Generated by Database Model Generator v0.6.0
|
112
|
+
# Database: SQL Server
|
113
|
+
# Table: Employees
|
114
|
+
# Generated on: 2024-12-19 15:30:00
|
115
|
+
|
116
|
+
class Employee < ActiveRecord::Base
|
117
|
+
set_table_name "Employees"
|
118
|
+
set_primary_key :EmployeeID
|
119
|
+
|
120
|
+
# Recommended Indexes (add these to your database migration):
|
121
|
+
# add_index :employees, :DepartmentID # Foreign key index
|
122
|
+
# add_index :employees, :ManagerID # Foreign key index
|
123
|
+
# add_index :employees, :Email, unique: true # Unique constraint
|
124
|
+
# add_index :employees, :HireDate # Date queries
|
125
|
+
# add_index :employees, [:DepartmentID, :HireDate] # Composite index
|
126
|
+
|
127
|
+
# Relationships
|
128
|
+
belongs_to :department, foreign_key: :DepartmentID
|
129
|
+
belongs_to :manager, class_name: 'Employee', foreign_key: :ManagerID, optional: true
|
130
|
+
has_many :subordinates, class_name: 'Employee', foreign_key: :ManagerID
|
131
|
+
|
132
|
+
# Scopes
|
133
|
+
scope :active, -> { where(Status: 'Active') }
|
134
|
+
scope :recent_hires, -> { where('HireDate > ?', 30.days.ago) }
|
135
|
+
scope :by_department, ->(dept) { where(DepartmentID: dept) }
|
136
|
+
|
137
|
+
# Validations
|
138
|
+
validates :FirstName, presence: true, length: { maximum: 50 }
|
139
|
+
validates :LastName, presence: true, length: { maximum: 50 }
|
140
|
+
validates :Email, presence: true, uniqueness: true, length: { maximum: 100 }
|
141
|
+
validates :HireDate, presence: true
|
142
|
+
validates :Salary, numericality: { greater_than: 0 }, allow_nil: true
|
143
|
+
|
144
|
+
# Enums
|
145
|
+
enum Status: { active: 0, inactive: 1, terminated: 2 }
|
146
|
+
|
147
|
+
# Class Methods
|
148
|
+
def self.search(query)
|
149
|
+
return all if query.blank?
|
150
|
+
where("FirstName LIKE ? OR LastName LIKE ? OR Email LIKE ?", "%#{query}%", "%#{query}%", "%#{query}%")
|
151
|
+
end
|
152
|
+
|
153
|
+
def self.by_name
|
154
|
+
order(:FirstName, :LastName)
|
155
|
+
end
|
156
|
+
|
157
|
+
# Instance Methods
|
158
|
+
def full_name
|
159
|
+
"#{FirstName} #{LastName}".strip
|
160
|
+
end
|
161
|
+
|
162
|
+
def display_name
|
163
|
+
full_name.presence || "Employee #{EmployeeID}"
|
164
|
+
end
|
165
|
+
end
|
166
|
+
```
|
167
|
+
|
168
|
+
## Database-Specific Features
|
169
|
+
|
170
|
+
### SQL Server Features
|
171
|
+
|
172
|
+
#### Schema Introspection
|
173
|
+
- Uses `INFORMATION_SCHEMA` views for portability
|
174
|
+
- Supports case-sensitive table and column names
|
175
|
+
- Detects SQL Server data types (`varchar`, `nvarchar`, `datetime2`, etc.)
|
176
|
+
|
177
|
+
#### Data Type Mapping
|
178
|
+
| SQL Server Type | ActiveRecord Validation | Ruby Type |
|
179
|
+
|----------------|------------------------|-----------|
|
180
|
+
| `varchar(n)` | `length: { maximum: n }` | String |
|
181
|
+
| `nvarchar(n)` | `length: { maximum: n }` | String |
|
182
|
+
| `int` | `numericality: { only_integer: true }` | Integer |
|
183
|
+
| `decimal(p,s)` | `numericality: { ... }` | BigDecimal |
|
184
|
+
| `datetime2` | `validates_date` | DateTime |
|
185
|
+
| `bit` | Boolean validation | Boolean |
|
186
|
+
|
187
|
+
#### Full-Text Search
|
188
|
+
```sql
|
189
|
+
-- SQL Server Full-Text Index
|
190
|
+
CREATE FULLTEXT INDEX ON Employees (FirstName, LastName)
|
191
|
+
KEY INDEX PK_Employees
|
192
|
+
```
|
193
|
+
|
194
|
+
### Oracle Features
|
195
|
+
|
196
|
+
#### Schema Introspection
|
197
|
+
- Uses `USER_*` dictionary views
|
198
|
+
- Traditional Oracle naming conventions
|
199
|
+
- Supports Oracle-specific data types (`VARCHAR2`, `NUMBER`, `DATE`)
|
200
|
+
|
201
|
+
#### Data Type Mapping
|
202
|
+
| Oracle Type | ActiveRecord Validation | Ruby Type |
|
203
|
+
|------------|------------------------|-----------|
|
204
|
+
| `VARCHAR2(n)` | `length: { maximum: n }` | String |
|
205
|
+
| `NUMBER(p,s)` | `numericality: { ... }` | Numeric |
|
206
|
+
| `DATE` | `validates_date` | Date |
|
207
|
+
| `CLOB` | Text validation | String |
|
208
|
+
|
209
|
+
#### Full-Text Search
|
210
|
+
```sql
|
211
|
+
-- Oracle Text Index
|
212
|
+
CREATE INDEX idx_employees_name_text ON EMPLOYEES (FIRST_NAME)
|
213
|
+
INDEXTYPE IS CTXSYS.CONTEXT
|
214
|
+
```
|
215
|
+
|
216
|
+
## Performance Optimization
|
217
|
+
|
218
|
+
### Index Recommendations
|
219
|
+
|
220
|
+
The generator analyzes your schema and suggests performance-optimizing indexes:
|
221
|
+
|
222
|
+
#### 1. Foreign Key Indexes
|
223
|
+
```ruby
|
224
|
+
# Detected relationships automatically get index recommendations
|
225
|
+
add_index :orders, :customer_id # Speeds up JOINs
|
226
|
+
add_index :order_items, :product_id # Foreign key performance
|
227
|
+
```
|
228
|
+
|
229
|
+
#### 2. Unique Constraint Indexes
|
230
|
+
```ruby
|
231
|
+
# Email, username, and code fields get unique indexes
|
232
|
+
add_index :users, :email, unique: true
|
233
|
+
add_index :products, :sku, unique: true
|
234
|
+
```
|
235
|
+
|
236
|
+
#### 3. Date Query Indexes
|
237
|
+
```ruby
|
238
|
+
# Date/timestamp columns get indexes for range queries
|
239
|
+
add_index :orders, :created_at
|
240
|
+
add_index :events, :start_date
|
241
|
+
```
|
242
|
+
|
243
|
+
#### 4. Composite Indexes
|
244
|
+
```ruby
|
245
|
+
# Multi-column indexes for common query patterns
|
246
|
+
add_index :orders, [:customer_id, :created_at]
|
247
|
+
add_index :tasks, [:status, :due_date]
|
248
|
+
```
|
249
|
+
|
250
|
+
### Performance Impact
|
251
|
+
|
252
|
+
| Index Type | Performance Gain | Use Case |
|
253
|
+
|------------|------------------|----------|
|
254
|
+
| Foreign Key | 10-100x | JOIN operations |
|
255
|
+
| Unique | Instant validation | Uniqueness checks |
|
256
|
+
| Date | 5-50x | Date range queries |
|
257
|
+
| Composite | 2-100x | Multi-column WHERE |
|
258
|
+
| Full-Text | Search enabled | Text search |
|
259
|
+
|
260
|
+
## Migration Generation
|
261
|
+
|
262
|
+
### Rails Migration Example
|
263
|
+
|
264
|
+
The `--indexes` option generates ready-to-use Rails migrations:
|
265
|
+
|
266
|
+
```ruby
|
267
|
+
class AddIndexesToEmployees < ActiveRecord::Migration[7.0]
|
268
|
+
def change
|
269
|
+
# Foreign key indexes
|
270
|
+
add_index :employees, :DepartmentID
|
271
|
+
add_index :employees, :ManagerID
|
272
|
+
|
273
|
+
# Unique constraints
|
274
|
+
add_index :employees, :Email, unique: true
|
275
|
+
|
276
|
+
# Date optimization
|
277
|
+
add_index :employees, :HireDate
|
278
|
+
|
279
|
+
# Composite indexes for common queries
|
280
|
+
add_index :employees, [:DepartmentID, :HireDate]
|
281
|
+
|
282
|
+
# Full-text search (database-specific)
|
283
|
+
case connection.adapter_name
|
284
|
+
when 'SQLServer'
|
285
|
+
execute "CREATE FULLTEXT INDEX ON Employees (FirstName, LastName) KEY INDEX PK_Employees"
|
286
|
+
when 'Oracle', 'OracleEnhanced'
|
287
|
+
execute "CREATE INDEX idx_employees_name_text ON EMPLOYEES (FIRST_NAME) INDEXTYPE IS CTXSYS.CONTEXT"
|
288
|
+
end
|
289
|
+
end
|
290
|
+
end
|
291
|
+
```
|
292
|
+
|
293
|
+
## Advanced Usage
|
294
|
+
|
295
|
+
### Environment-Specific Configuration
|
296
|
+
|
297
|
+
```ruby
|
298
|
+
# config/database.yml
|
299
|
+
development:
|
300
|
+
adapter: sqlserver # or oracle_enhanced
|
301
|
+
host: localhost
|
302
|
+
port: 1433
|
303
|
+
database: myapp_dev
|
304
|
+
username: dev_user
|
305
|
+
password: dev_pass
|
306
|
+
|
307
|
+
production:
|
308
|
+
adapter: sqlserver
|
309
|
+
host: <%= ENV['DB_HOST'] %>
|
310
|
+
port: <%= ENV['DB_PORT'] || 1433 %>
|
311
|
+
database: <%= ENV['DB_NAME'] %>
|
312
|
+
username: <%= ENV['DB_USER'] %>
|
313
|
+
password: <%= ENV['DB_PASS'] %>
|
314
|
+
```
|
315
|
+
|
316
|
+
### Batch Model Generation
|
317
|
+
|
318
|
+
```bash
|
319
|
+
# Generate models for multiple tables
|
320
|
+
for table in Customers Orders Products; do
|
321
|
+
./bin/dmg -T sqlserver -s myserver -d mydb -u user -p pass -t $table
|
322
|
+
done
|
323
|
+
|
324
|
+
# Generate only index recommendations for all tables
|
325
|
+
for table in $(sqlcmd -Q "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES" -h-1); do
|
326
|
+
./bin/dmg -T sqlserver -s myserver -d mydb -u user -p pass -t $table --indexes
|
327
|
+
done
|
328
|
+
```
|
329
|
+
|
330
|
+
### Integration with Rails
|
331
|
+
|
332
|
+
```ruby
|
333
|
+
# lib/tasks/db_models.rake
|
334
|
+
namespace :db do
|
335
|
+
desc "Generate models from database schema"
|
336
|
+
task :generate_models => :environment do
|
337
|
+
config = Rails.application.config.database_configuration[Rails.env]
|
338
|
+
|
339
|
+
case config['adapter']
|
340
|
+
when 'sqlserver'
|
341
|
+
system("./bin/dmg -T sqlserver -s #{config['host']} -d #{config['database']} -u #{config['username']} -p #{config['password']} -t #{ENV['TABLE']}")
|
342
|
+
when 'oracle_enhanced'
|
343
|
+
system("./bin/dmg -T oracle -d #{config['database']} -u #{config['username']} -p #{config['password']} -t #{ENV['TABLE']}")
|
344
|
+
end
|
345
|
+
end
|
346
|
+
end
|
347
|
+
|
348
|
+
# Usage: rails db:generate_models TABLE=employees
|
349
|
+
```
|
350
|
+
|
351
|
+
## Troubleshooting
|
352
|
+
|
353
|
+
### Common Issues
|
354
|
+
|
355
|
+
#### SQL Server Connection
|
356
|
+
```bash
|
357
|
+
# Connection timeout
|
358
|
+
./bin/dmg -T sqlserver -s myserver -d mydb -u user -p pass -t mytable
|
359
|
+
# Error: TLS/SSL connection failed
|
360
|
+
|
361
|
+
# Solution: Disable encryption for development
|
362
|
+
TinyTds::Client.new(
|
363
|
+
host: 'myserver',
|
364
|
+
username: 'user',
|
365
|
+
password: 'pass',
|
366
|
+
database: 'mydb',
|
367
|
+
encrypt: false
|
368
|
+
)
|
369
|
+
```
|
370
|
+
|
371
|
+
#### Missing Dependencies
|
372
|
+
```bash
|
373
|
+
# Install SQL Server support
|
374
|
+
gem install tiny_tds
|
375
|
+
|
376
|
+
# On Ubuntu/Debian
|
377
|
+
sudo apt-get install freetds-dev
|
378
|
+
|
379
|
+
# On macOS
|
380
|
+
brew install freetds
|
381
|
+
```
|
382
|
+
|
383
|
+
#### Permission Issues
|
384
|
+
```sql
|
385
|
+
-- Grant necessary permissions
|
386
|
+
GRANT SELECT ON INFORMATION_SCHEMA.TABLES TO [username];
|
387
|
+
GRANT SELECT ON INFORMATION_SCHEMA.COLUMNS TO [username];
|
388
|
+
GRANT SELECT ON INFORMATION_SCHEMA.TABLE_CONSTRAINTS TO [username];
|
389
|
+
```
|
390
|
+
|
391
|
+
### Debugging
|
392
|
+
|
393
|
+
Enable verbose output:
|
394
|
+
```bash
|
395
|
+
# Set environment variable for debugging
|
396
|
+
DEBUG=1 ./bin/dmg -T sqlserver -s myserver -d mydb -u user -p pass -t mytable
|
397
|
+
```
|
398
|
+
|
399
|
+
## Migration from Oracle-Only Version
|
400
|
+
|
401
|
+
### Backward Compatibility
|
402
|
+
|
403
|
+
The tool maintains full backward compatibility:
|
404
|
+
|
405
|
+
```ruby
|
406
|
+
# Old code still works
|
407
|
+
require 'oracle/model/generator'
|
408
|
+
connection = OCI8.new(user, pass, database)
|
409
|
+
omg = Oracle::Model::Generator.new(connection)
|
410
|
+
|
411
|
+
# New code provides more features
|
412
|
+
require 'database_model_generator'
|
413
|
+
omg = DatabaseModel::Generator.new(connection, type: :oracle)
|
414
|
+
```
|
415
|
+
|
416
|
+
### Upgrading Existing Projects
|
417
|
+
|
418
|
+
1. **Install new dependencies**:
|
419
|
+
```bash
|
420
|
+
gem install tiny_tds # For SQL Server support
|
421
|
+
```
|
422
|
+
|
423
|
+
2. **Update require statements**:
|
424
|
+
```ruby
|
425
|
+
# From
|
426
|
+
require 'oracle/model/generator'
|
427
|
+
|
428
|
+
# To
|
429
|
+
require 'database_model_generator'
|
430
|
+
```
|
431
|
+
|
432
|
+
3. **Use new CLI options**:
|
433
|
+
```bash
|
434
|
+
# Old Oracle-specific
|
435
|
+
./bin/dmg -d oracle_db -u user -p pass -t table
|
436
|
+
|
437
|
+
# New database-agnostic
|
438
|
+
./bin/dmg -T oracle -d oracle_db -u user -p pass -t table
|
439
|
+
./bin/dmg -T sqlserver -s sql_server -d sql_db -u user -p pass -t table
|
440
|
+
```
|
441
|
+
|
442
|
+
## Contributing
|
443
|
+
|
444
|
+
### Adding Database Support
|
445
|
+
|
446
|
+
To add support for additional databases (PostgreSQL, MySQL, etc.):
|
447
|
+
|
448
|
+
1. **Create database-specific generator**:
|
449
|
+
```ruby
|
450
|
+
# lib/postgresql_generator.rb
|
451
|
+
class PostgresqlGenerator < DatabaseModel::Generator::Base
|
452
|
+
# Implement abstract methods
|
453
|
+
end
|
454
|
+
```
|
455
|
+
|
456
|
+
2. **Add connection detection**:
|
457
|
+
```ruby
|
458
|
+
# lib/database_model_generator.rb
|
459
|
+
def self.detect_database_type(connection, options = {})
|
460
|
+
case connection.class.name
|
461
|
+
when 'PG::Connection'
|
462
|
+
:postgresql
|
463
|
+
# ... existing cases
|
464
|
+
end
|
465
|
+
end
|
466
|
+
```
|
467
|
+
|
468
|
+
3. **Update CLI support**:
|
469
|
+
```ruby
|
470
|
+
# bin/dmg - add new connection logic
|
471
|
+
```
|
472
|
+
|
473
|
+
The modular architecture makes adding new database support straightforward while maintaining existing functionality.
|
474
|
+
|
475
|
+
## License
|
476
|
+
|
477
|
+
This tool maintains the same license as the original Oracle Model Generator. SQL Server support is provided under the same terms.
|