mysql_genius 0.1.1 → 0.3.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.
- checksums.yaml +4 -4
- data/.github/FUNDING.yml +5 -0
- data/.github/workflows/ci.yml +30 -7
- data/.github/workflows/publish.yml +32 -0
- data/.gitignore +3 -0
- data/.rubocop.yml +24 -0
- data/CHANGELOG.md +41 -0
- data/Gemfile +7 -2
- data/README.md +61 -218
- data/Rakefile +3 -1
- data/app/controllers/concerns/mysql_genius/ai_features.rb +90 -52
- data/app/controllers/concerns/mysql_genius/database_analysis.rb +81 -45
- data/app/controllers/concerns/mysql_genius/query_execution.rb +18 -16
- data/app/controllers/mysql_genius/base_controller.rb +3 -1
- data/app/controllers/mysql_genius/queries_controller.rb +19 -12
- data/app/services/mysql_genius/ai_client.rb +9 -2
- data/app/services/mysql_genius/ai_optimization_service.rb +8 -4
- data/app/services/mysql_genius/ai_suggestion_service.rb +5 -2
- data/app/views/layouts/mysql_genius/application.html.erb +147 -5
- data/app/views/mysql_genius/queries/_tab_dashboard.html.erb +95 -0
- data/app/views/mysql_genius/queries/_tab_duplicate_indexes.html.erb +11 -0
- data/app/views/mysql_genius/queries/_tab_query_explorer.html.erb +110 -0
- data/app/views/mysql_genius/queries/_tab_query_stats.html.erb +2 -2
- data/app/views/mysql_genius/queries/_tab_table_sizes.html.erb +7 -5
- data/app/views/mysql_genius/queries/_tab_unused_indexes.html.erb +11 -0
- data/app/views/mysql_genius/queries/index.html.erb +436 -52
- data/bin/console +1 -0
- data/config/routes.rb +2 -0
- data/docs/screenshots/dashboard.png +0 -0
- data/docs/screenshots/query_explore.png +0 -0
- data/docs/superpowers/plans/2026-04-08-dashboard-first-redesign.md +741 -0
- data/docs/superpowers/specs/2026-04-08-dashboard-first-redesign.md +87 -0
- data/lib/generators/mysql_genius/install/install_generator.rb +19 -0
- data/lib/generators/mysql_genius/install/templates/initializer.rb +56 -0
- data/lib/mysql_genius/configuration.rb +8 -6
- data/lib/mysql_genius/engine.rb +2 -0
- data/lib/mysql_genius/slow_query_monitor.rb +29 -25
- data/lib/mysql_genius/sql_validator.rb +6 -4
- data/lib/mysql_genius/version.rb +3 -1
- data/lib/mysql_genius.rb +2 -0
- data/mysql_genius.gemspec +9 -8
- metadata +23 -15
- data/app/views/mysql_genius/queries/_tab_sql_query.html.erb +0 -40
- data/app/views/mysql_genius/queries/_tab_visual_builder.html.erb +0 -61
- data/docs/screenshots/sql_query.png +0 -0
- data/docs/screenshots/visual_builder.png +0 -0
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e54ecfd5bfe23f9c1e97caee0dd7d76499375488a37c2bbf2f58951b2b37dd85
|
|
4
|
+
data.tar.gz: d0fd0253422d4fab85dbc40497ad60d4c8ed4ed699de435a8210fbed1981734d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: eb7a5cf0dbda1f1c0b5df59b543735d3a83a902219d54e354f06434a85101d858a7c80407ecf4e492f4bdceaf9b26d2e8d854af3396cad6e4cf6ef503781f500
|
|
7
|
+
data.tar.gz: a910f441a3a234e91c95b338ae74427fd19c68184ad8b5a7b4019adc4f6220e9440381f646c0f2daeaacfec7cb73a3204c1aa1efc0e4dc960ce5b7633a8b46b0
|
data/.github/FUNDING.yml
ADDED
data/.github/workflows/ci.yml
CHANGED
|
@@ -13,14 +13,9 @@ jobs:
|
|
|
13
13
|
strategy:
|
|
14
14
|
fail-fast: false
|
|
15
15
|
matrix:
|
|
16
|
-
ruby: ["2.7", "3.0", "3.1", "3.2", "3.3"]
|
|
17
|
-
rails: ["5.2", "6.0", "6.1", "7.0", "7.1", "7.2"]
|
|
16
|
+
ruby: ["2.7", "3.0", "3.1", "3.2", "3.3", "3.4"]
|
|
17
|
+
rails: ["5.2", "6.0", "6.1", "7.0", "7.1", "7.2", "8.0", "8.1"]
|
|
18
18
|
exclude:
|
|
19
|
-
# Rails 7.0+ requires Ruby 2.7+, but 7.2 requires 3.1+
|
|
20
|
-
- ruby: "2.7"
|
|
21
|
-
rails: "7.2"
|
|
22
|
-
- ruby: "3.0"
|
|
23
|
-
rails: "7.2"
|
|
24
19
|
# Rails 5.2 doesn't support Ruby 3.1+
|
|
25
20
|
- ruby: "3.1"
|
|
26
21
|
rails: "5.2"
|
|
@@ -28,11 +23,39 @@ jobs:
|
|
|
28
23
|
rails: "5.2"
|
|
29
24
|
- ruby: "3.3"
|
|
30
25
|
rails: "5.2"
|
|
26
|
+
- ruby: "3.4"
|
|
27
|
+
rails: "5.2"
|
|
31
28
|
# Rails 6.0 doesn't support Ruby 3.2+
|
|
32
29
|
- ruby: "3.2"
|
|
33
30
|
rails: "6.0"
|
|
34
31
|
- ruby: "3.3"
|
|
35
32
|
rails: "6.0"
|
|
33
|
+
- ruby: "3.4"
|
|
34
|
+
rails: "6.0"
|
|
35
|
+
# Rails 6.1 doesn't support Ruby 3.4 (mutex_m removed from stdlib)
|
|
36
|
+
- ruby: "3.4"
|
|
37
|
+
rails: "6.1"
|
|
38
|
+
# Rails 7.0 doesn't support Ruby 3.4 (mutex_m removed from stdlib)
|
|
39
|
+
- ruby: "3.4"
|
|
40
|
+
rails: "7.0"
|
|
41
|
+
# Rails 7.2+ requires Ruby 3.1+
|
|
42
|
+
- ruby: "2.7"
|
|
43
|
+
rails: "7.2"
|
|
44
|
+
- ruby: "3.0"
|
|
45
|
+
rails: "7.2"
|
|
46
|
+
# Rails 8.0+ requires Ruby 3.2+
|
|
47
|
+
- ruby: "2.7"
|
|
48
|
+
rails: "8.0"
|
|
49
|
+
- ruby: "3.0"
|
|
50
|
+
rails: "8.0"
|
|
51
|
+
- ruby: "3.1"
|
|
52
|
+
rails: "8.0"
|
|
53
|
+
- ruby: "2.7"
|
|
54
|
+
rails: "8.1"
|
|
55
|
+
- ruby: "3.0"
|
|
56
|
+
rails: "8.1"
|
|
57
|
+
- ruby: "3.1"
|
|
58
|
+
rails: "8.1"
|
|
36
59
|
|
|
37
60
|
env:
|
|
38
61
|
RAILS_VERSION: ${{ matrix.rails }}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
name: Publish to RubyGems
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
tags: ["v*"]
|
|
6
|
+
workflow_dispatch:
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
test:
|
|
10
|
+
runs-on: ubuntu-latest
|
|
11
|
+
steps:
|
|
12
|
+
- uses: actions/checkout@v4
|
|
13
|
+
- uses: ruby/setup-ruby@v1
|
|
14
|
+
with:
|
|
15
|
+
ruby-version: 3.3
|
|
16
|
+
bundler-cache: true
|
|
17
|
+
- run: bundle exec rspec
|
|
18
|
+
|
|
19
|
+
publish:
|
|
20
|
+
needs: test
|
|
21
|
+
runs-on: ubuntu-latest
|
|
22
|
+
steps:
|
|
23
|
+
- uses: actions/checkout@v4
|
|
24
|
+
- uses: ruby/setup-ruby@v1
|
|
25
|
+
with:
|
|
26
|
+
ruby-version: 3.3
|
|
27
|
+
- name: Build gem
|
|
28
|
+
run: gem build mysql_genius.gemspec
|
|
29
|
+
- name: Publish to RubyGems
|
|
30
|
+
run: gem push mysql_genius-*.gem
|
|
31
|
+
env:
|
|
32
|
+
GEM_HOST_API_KEY: ${{ secrets.RUBYGEMS_API_KEY }}
|
data/.gitignore
CHANGED
data/.rubocop.yml
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
inherit_gem:
|
|
2
|
+
rubocop-shopify: rubocop.yml
|
|
3
|
+
|
|
4
|
+
require:
|
|
5
|
+
- rubocop-rspec
|
|
6
|
+
|
|
7
|
+
AllCops:
|
|
8
|
+
TargetRubyVersion: 2.6
|
|
9
|
+
NewCops: enable
|
|
10
|
+
|
|
11
|
+
Layout/LineLength:
|
|
12
|
+
Max: 250
|
|
13
|
+
|
|
14
|
+
RSpec/MessageSpies:
|
|
15
|
+
Enabled: false
|
|
16
|
+
|
|
17
|
+
RSpec/VerifiedDoubles:
|
|
18
|
+
Enabled: false
|
|
19
|
+
|
|
20
|
+
RSpec/ExampleLength:
|
|
21
|
+
Max: 25
|
|
22
|
+
|
|
23
|
+
RSpec/MultipleExpectations:
|
|
24
|
+
Max: 5
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,46 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.3.1
|
|
4
|
+
|
|
5
|
+
### Added
|
|
6
|
+
- **Sortable columns** -- click any column header to sort ascending/descending on all data tables
|
|
7
|
+
- **Automated RubyGems publishing** -- GitHub Actions workflow publishes gem on tag push
|
|
8
|
+
|
|
9
|
+
### Fixed
|
|
10
|
+
- **Query stats noise** -- MySQLGenius internal queries (information_schema, performance_schema, SHOW, etc.) are now excluded from the Query Stats tab
|
|
11
|
+
|
|
12
|
+
## 0.3.0
|
|
13
|
+
|
|
14
|
+
### Improved
|
|
15
|
+
- **SQL syntax highlighting** -- SQL code blocks in tables now feature a dark-themed syntax highlighter with distinct colors for keywords, functions, strings, numbers, operators, identifiers, and placeholders (Catppuccin Mocha palette)
|
|
16
|
+
- **Table visual hierarchy** -- redesigned table headers (uppercase, thicker bottom border, rounded top corners), improved row hover states (blue tint), cleaner alternating row colors, removed vertical cell borders
|
|
17
|
+
- **Numeric column formatting** -- right-aligned with monospace tabular-nums font for easy scanning; duration values color-coded green/amber/red by severity
|
|
18
|
+
- **Overall dashboard polish** -- more generous cell padding, improved inline `code` tag styling, added `mg-badge-success` variant
|
|
19
|
+
- **Tab persistence** -- active tab is remembered across page reloads via URL hash
|
|
20
|
+
|
|
21
|
+
### Fixed
|
|
22
|
+
- **Unused indexes SQL error on MySQL 8.0+** -- `reads` and `writes` are reserved words and now use backtick quoting
|
|
23
|
+
|
|
24
|
+
### Added
|
|
25
|
+
- **Dark theme** -- auto-detects system preference, manual toggle via sun/moon button, persisted in localStorage
|
|
26
|
+
- **Tables tab** -- renamed from "Table Sizes", now shows engine, collation, auto-increment, last updated, and accurate row counts via `COUNT(*)`
|
|
27
|
+
- **Optimize suggestions** -- tables with >10% fragmentation are flagged with an optimize badge
|
|
28
|
+
|
|
29
|
+
## 0.2.0
|
|
30
|
+
|
|
31
|
+
- **Dashboard-first redesign** -- new default landing page with server health, top slow queries, top expensive queries, and index alert badges
|
|
32
|
+
- **Query Explorer** -- merged Visual Builder and SQL Query into one tab with a mode toggle
|
|
33
|
+
- **Suggested migrations** -- duplicate and unused index tabs generate timestamped Rails migrations with copy-to-clipboard
|
|
34
|
+
- **Install generator** -- `rails generate mysql_genius:install` creates initializer and mounts the engine
|
|
35
|
+
- **RuboCop** -- added rubocop-shopify and rubocop-rspec, enforced across the codebase
|
|
36
|
+
- **CI matrix** -- added Ruby 3.4, Rails 8.0 and 8.1; excluded incompatible Ruby 3.4 + Rails 6.1/7.0 combos
|
|
37
|
+
- **Smarter AI prompts** -- schema review now includes primary keys and Rails-aware context (no foreign key constraint recommendations, recommends indexes on FK columns instead)
|
|
38
|
+
- **SSL fix** -- explicit CA certificate file for AI API requests
|
|
39
|
+
- Tab reorder: Dashboard, Slow Queries, Query Stats, Server, Table Sizes, Unused Indexes, Duplicate Indexes, Query Explorer, AI Tools
|
|
40
|
+
- Dashboard links to Server tab for full details
|
|
41
|
+
- Clipboard fallback for non-HTTPS environments
|
|
42
|
+
- Gemspec description updated to lead with monitoring features
|
|
43
|
+
|
|
3
44
|
## 0.1.0
|
|
4
45
|
|
|
5
46
|
- Initial release
|
data/Gemfile
CHANGED
|
@@ -1,15 +1,20 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
source "https://rubygems.org"
|
|
2
4
|
|
|
3
5
|
gemspec
|
|
4
6
|
|
|
5
7
|
if ENV["RAILS_VERSION"]
|
|
6
8
|
rails_version = ENV["RAILS_VERSION"]
|
|
7
|
-
gem "railties", "~> #{rails_version}.0"
|
|
8
|
-
gem "activerecord", "~> #{rails_version}.0"
|
|
9
9
|
gem "actionpack", "~> #{rails_version}.0"
|
|
10
|
+
gem "activerecord", "~> #{rails_version}.0"
|
|
11
|
+
gem "railties", "~> #{rails_version}.0"
|
|
10
12
|
end
|
|
11
13
|
|
|
12
14
|
group :development, :test do
|
|
13
15
|
gem "rake"
|
|
14
16
|
gem "rspec", "~> 3.0"
|
|
17
|
+
gem "rubocop"
|
|
18
|
+
gem "rubocop-shopify"
|
|
19
|
+
gem "rubocop-rspec"
|
|
15
20
|
end
|
data/README.md
CHANGED
|
@@ -1,291 +1,134 @@
|
|
|
1
1
|
# MySQLGenius
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
An AI-powered MySQL dashboard for Rails to help you optimize your database for maximum performance, inspired by [PgHero](https://github.com/ankane/pghero).
|
|
4
4
|
|
|
5
5
|
## Screenshots
|
|
6
6
|
|
|
7
|
-
###
|
|
8
|
-
Build queries visually -- select tables, pick columns, add type-aware filters, and sort results without writing SQL.
|
|
7
|
+
### Dashboard
|
|
9
8
|
|
|
10
|
-
|
|
9
|
+
At-a-glance server health, top slow queries, most expensive queries, and index alerts.
|
|
11
10
|
|
|
12
|
-
|
|
13
|
-
Write raw SQL or describe what you want in plain English and let the AI generate the query for you.
|
|
14
|
-
|
|
15
|
-

|
|
16
|
-
|
|
17
|
-
### Duplicate Index Detection
|
|
18
|
-
Find redundant indexes whose columns are a left-prefix of another index, with ready-to-run `DROP INDEX` statements.
|
|
19
|
-
|
|
20
|
-

|
|
21
|
-
|
|
22
|
-
### Table Sizes
|
|
23
|
-
View row counts, data size, index size, fragmentation, and a visual size chart for every table.
|
|
24
|
-
|
|
25
|
-

|
|
11
|
+

|
|
26
12
|
|
|
27
13
|
### Query Stats
|
|
28
|
-
|
|
14
|
+
|
|
15
|
+
Top queries from `performance_schema` sorted by total time, with SQL syntax highlighting and color-coded durations.
|
|
29
16
|
|
|
30
17
|

|
|
31
18
|
|
|
32
19
|
### Server Dashboard
|
|
33
|
-
At-a-glance server health: version, connections, InnoDB buffer pool, and query activity with AI-powered diagnostics.
|
|
34
20
|
|
|
35
|
-
|
|
21
|
+
Server health: version, connections, InnoDB buffer pool, and query activity with AI-powered diagnostics.
|
|
36
22
|
|
|
37
|
-
|
|
38
|
-
Schema review that finds anti-patterns -- missing primary keys, nullable foreign keys, inappropriate column types, and more.
|
|
23
|
+

|
|
39
24
|
|
|
40
|
-
|
|
25
|
+
### Tables
|
|
41
26
|
|
|
42
|
-
|
|
27
|
+
Row counts, data size, index size, engine, fragmentation, and optimize suggestions for every table.
|
|
43
28
|
|
|
44
|
-
|
|
45
|
-
- **Safe SQL Execution** -- read-only enforcement, blocked tables, masked sensitive columns, row limits, query timeouts
|
|
46
|
-
- **EXPLAIN Analysis** -- run EXPLAIN on any query and view the execution plan
|
|
47
|
-
- **AI Query Suggestions** -- describe what you want in plain English, get SQL back (optional, any OpenAI-compatible API)
|
|
48
|
-
- **AI Query Optimization** -- get actionable optimization suggestions from EXPLAIN output (optional)
|
|
49
|
-
- **Slow Query Monitoring** -- captures slow SELECT queries via ActiveSupport notifications and Redis
|
|
50
|
-
- **Duplicate Index Detection** -- finds redundant indexes whose columns are a left-prefix of another index
|
|
51
|
-
- **Table Size Dashboard** -- view row counts, data size, index size, and fragmentation for all tables
|
|
52
|
-
- **Audit Logging** -- logs all query executions, rejections, and errors
|
|
53
|
-
- **MariaDB Support** -- automatically detects MariaDB and uses appropriate timeout syntax
|
|
54
|
-
- **Self-contained UI** -- no external CSS/JS dependencies, works with any Rails layout
|
|
55
|
-
- **Zero jQuery** -- pure vanilla JavaScript frontend
|
|
29
|
+

|
|
56
30
|
|
|
57
|
-
|
|
31
|
+
### Duplicate Index Detection
|
|
58
32
|
|
|
59
|
-
-
|
|
60
|
-
- Ruby 2.6+
|
|
61
|
-
- MySQL or MariaDB
|
|
62
|
-
- Redis (optional, for slow query monitoring)
|
|
33
|
+
Find redundant indexes whose columns are a left-prefix of another index, with ready-to-run `DROP INDEX` statements.
|
|
63
34
|
|
|
64
|
-
|
|
35
|
+

|
|
65
36
|
|
|
66
|
-
|
|
37
|
+
### Query Explorer
|
|
67
38
|
|
|
68
|
-
|
|
69
|
-
gem "mysql_genius"
|
|
70
|
-
```
|
|
39
|
+
Build queries visually or write raw SQL. Optional AI assistant generates queries from plain English descriptions.
|
|
71
40
|
|
|
72
|
-
|
|
41
|
+

|
|
73
42
|
|
|
74
|
-
|
|
75
|
-
gem "mysql_genius", github: "antarr/mysql_genius"
|
|
76
|
-
```
|
|
43
|
+
### AI Tools
|
|
77
44
|
|
|
78
|
-
|
|
45
|
+
Schema review, query optimization, index advisor, anomaly detection, root cause analysis, and migration risk assessment.
|
|
79
46
|
|
|
80
|
-
|
|
81
|
-
bundle install
|
|
82
|
-
```
|
|
47
|
+

|
|
83
48
|
|
|
84
|
-
##
|
|
49
|
+
## Features
|
|
85
50
|
|
|
86
|
-
|
|
51
|
+
- **Dashboard** -- server health, slow queries, expensive queries, index alerts at a glance
|
|
52
|
+
- **Query Explorer** -- visual builder + raw SQL editor with AI assistant
|
|
53
|
+
- **SQL Syntax Highlighting** -- dark-themed code blocks with color-coded keywords, functions, strings
|
|
54
|
+
- **Safe SQL Execution** -- read-only enforcement, blocked tables, masked columns, row limits, timeouts
|
|
55
|
+
- **EXPLAIN Analysis** -- run EXPLAIN on any query and view the execution plan
|
|
56
|
+
- **9 AI Tools** -- suggestions, optimization, schema review, query rewrite, index advisor, anomaly detection, root cause analysis, migration risk ([details](https://github.com/antarr/mysql_genius/wiki/AI-Features))
|
|
57
|
+
- **Slow Query Monitoring** -- captures slow queries via ActiveSupport notifications and Redis ([details](https://github.com/antarr/mysql_genius/wiki/Slow-Query-Monitoring))
|
|
58
|
+
- **Index Analysis** -- duplicate index detection, unused index detection with DROP statements
|
|
59
|
+
- **Dark Theme** -- auto-detects system preference with manual toggle ([details](https://github.com/antarr/mysql_genius/wiki/Dark-Theme))
|
|
60
|
+
- **MariaDB Support** -- automatically detects MariaDB and uses appropriate timeout syntax
|
|
61
|
+
- **Self-contained UI** -- no external CSS/JS dependencies, no jQuery, works with any Rails layout
|
|
87
62
|
|
|
88
|
-
|
|
63
|
+
## Quick Start
|
|
89
64
|
|
|
90
65
|
```ruby
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
end
|
|
66
|
+
# Gemfile
|
|
67
|
+
gem "mysql_genius"
|
|
94
68
|
```
|
|
95
69
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
# Using a session constraint
|
|
100
|
-
constraints ->(req) { req.session[:admin] } do
|
|
101
|
-
mount MysqlGenius::Engine, at: "/mysql_genius"
|
|
102
|
-
end
|
|
103
|
-
|
|
104
|
-
# Or using Devise
|
|
105
|
-
authenticate :user, ->(u) { u.admin? } do
|
|
106
|
-
mount MysqlGenius::Engine, at: "/mysql_genius"
|
|
107
|
-
end
|
|
70
|
+
```bash
|
|
71
|
+
bundle install
|
|
72
|
+
rails generate mysql_genius:install
|
|
108
73
|
```
|
|
109
74
|
|
|
110
|
-
|
|
75
|
+
Visit `/mysql_genius` in your browser.
|
|
76
|
+
|
|
77
|
+
For detailed setup, see the [Installation guide](https://github.com/antarr/mysql_genius/wiki/Installation).
|
|
111
78
|
|
|
112
|
-
|
|
79
|
+
## Configuration
|
|
113
80
|
|
|
114
81
|
```ruby
|
|
115
82
|
MysqlGenius.configure do |config|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
# Default: allows everyone. Use route constraints for most cases.
|
|
119
|
-
config.authenticate = ->(controller) { true }
|
|
120
|
-
|
|
121
|
-
# To use current_user or other app helpers, inherit from ApplicationController:
|
|
122
|
-
# config.base_controller = "ApplicationController"
|
|
123
|
-
# config.authenticate = ->(controller) { controller.current_user&.admin? }
|
|
124
|
-
|
|
125
|
-
# --- Tables ---
|
|
126
|
-
# Tables featured at the top of the visual builder dropdown (optional)
|
|
127
|
-
config.featured_tables = %w[users posts comments]
|
|
128
|
-
|
|
129
|
-
# Tables blocked from querying (defaults: sessions, schema_migrations, ar_internal_metadata)
|
|
83
|
+
config.base_controller = "ApplicationController"
|
|
84
|
+
config.authenticate = ->(controller) { controller.current_user&.admin? }
|
|
130
85
|
config.blocked_tables += %w[oauth_tokens api_keys]
|
|
131
|
-
|
|
132
|
-
# Column patterns to redact with [REDACTED] in results (case-insensitive substring match)
|
|
133
|
-
config.masked_column_patterns = %w[password secret digest token ssn]
|
|
134
|
-
|
|
135
|
-
# Default columns checked in the visual builder per table (optional).
|
|
136
|
-
# When empty for a table, all columns are checked by default.
|
|
137
|
-
config.default_columns = {
|
|
138
|
-
"users" => %w[id name email created_at],
|
|
139
|
-
"posts" => %w[id title user_id published_at]
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
# --- Query Safety ---
|
|
143
|
-
config.max_row_limit = 1000 # Hard cap on rows returned
|
|
144
|
-
config.default_row_limit = 25 # Default when no limit specified
|
|
145
|
-
config.query_timeout_ms = 30_000 # 30 second timeout (uses MariaDB or MySQL hints)
|
|
146
|
-
|
|
147
|
-
# --- Slow Query Monitoring ---
|
|
148
|
-
# Requires Redis. Set to nil to disable.
|
|
149
|
-
config.redis_url = ENV["REDIS_URL"].presence || "redis://127.0.0.1:6379/0"
|
|
150
|
-
config.slow_query_threshold_ms = 250
|
|
151
|
-
|
|
152
|
-
# --- Audit Logging ---
|
|
153
|
-
# Set to nil to disable. Logs query executions, rejections, and errors.
|
|
154
|
-
config.audit_logger = Logger.new(Rails.root.join("log", "mysql_genius.log"))
|
|
155
86
|
end
|
|
156
87
|
```
|
|
157
88
|
|
|
158
|
-
|
|
89
|
+
For full configuration options, see the [Configuration guide](https://github.com/antarr/mysql_genius/wiki/Configuration).
|
|
159
90
|
|
|
160
|
-
|
|
91
|
+
## AI Features (optional)
|
|
92
|
+
|
|
93
|
+
Works with OpenAI, Azure OpenAI, Ollama Cloud, local Ollama, or any OpenAI-compatible API.
|
|
161
94
|
|
|
162
95
|
```ruby
|
|
163
96
|
MysqlGenius.configure do |config|
|
|
164
|
-
# --- Option A: OpenAI ---
|
|
165
97
|
config.ai_endpoint = "https://api.openai.com/v1/chat/completions"
|
|
166
98
|
config.ai_api_key = ENV["OPENAI_API_KEY"]
|
|
167
99
|
config.ai_model = "gpt-4o"
|
|
168
100
|
config.ai_auth_style = :bearer
|
|
169
|
-
|
|
170
|
-
# --- Option B: Azure OpenAI ---
|
|
171
|
-
config.ai_endpoint = ENV["AZURE_OPENAI_ENDPOINT"] # Your deployment URL
|
|
172
|
-
config.ai_api_key = ENV["AZURE_OPENAI_API_KEY"]
|
|
173
|
-
config.ai_auth_style = :api_key # Default, uses api-key header
|
|
174
|
-
|
|
175
|
-
# --- Option C: Ollama Cloud ---
|
|
176
|
-
config.ai_endpoint = "https://api.ollama.com/v1/chat/completions"
|
|
177
|
-
config.ai_api_key = ENV["OLLAMA_API_KEY"]
|
|
178
|
-
config.ai_model = "gemma3:27b"
|
|
179
|
-
config.ai_auth_style = :bearer
|
|
180
|
-
|
|
181
|
-
# --- Option D: Local Ollama ---
|
|
182
|
-
config.ai_endpoint = "http://localhost:11434/v1/chat/completions"
|
|
183
|
-
config.ai_api_key = "ollama" # Any non-empty string
|
|
184
|
-
config.ai_model = "llama3"
|
|
185
|
-
config.ai_auth_style = :bearer
|
|
186
|
-
|
|
187
|
-
# --- Option E: Custom client ---
|
|
188
|
-
# Any callable that accepts messages: and temperature: kwargs
|
|
189
|
-
# and returns an OpenAI-compatible response hash.
|
|
190
|
-
config.ai_client = ->(messages:, temperature:) {
|
|
191
|
-
MyAiService.chat(messages, temperature: temperature)
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
# --- Domain Context ---
|
|
195
|
-
# Helps the AI understand your schema and generate better queries.
|
|
196
|
-
config.ai_system_context = <<~CONTEXT
|
|
197
|
-
This is an e-commerce database.
|
|
198
|
-
- `users` stores customer accounts. Primary key is `id`.
|
|
199
|
-
- `orders` tracks purchases. Linked to users via `user_id`.
|
|
200
|
-
- `products` contains the product catalog.
|
|
201
|
-
- Soft-deleted records have `deleted_at IS NOT NULL`.
|
|
202
|
-
CONTEXT
|
|
203
101
|
end
|
|
204
102
|
```
|
|
205
103
|
|
|
206
|
-
|
|
207
|
-
|--------|-----------------|------------|-------|
|
|
208
|
-
| OpenAI | `:bearer` | Required (e.g. `gpt-4o`) | |
|
|
209
|
-
| Azure OpenAI | `:api_key` (default) | Optional (uses deployment default) | |
|
|
210
|
-
| Ollama Cloud | `:bearer` | Required (e.g. `gemma3:27b`) | Follows redirects automatically |
|
|
211
|
-
| Local Ollama | `:bearer` | Required | No API key validation |
|
|
212
|
-
| Custom client | N/A | N/A | You handle everything |
|
|
213
|
-
|
|
214
|
-
When AI is not configured, the AI Assistant panel and optimization buttons are hidden automatically.
|
|
215
|
-
|
|
216
|
-
## Usage
|
|
217
|
-
|
|
218
|
-
Visit `/mysql_genius` in your browser. The dashboard has five tabs:
|
|
219
|
-
|
|
220
|
-
### Visual Builder
|
|
221
|
-
Select a table, pick columns, add type-aware filters (dates get date pickers, booleans get dropdowns), add sort orders, and run queries -- no SQL knowledge required. The generated SQL is shown and synced with the SQL tab.
|
|
222
|
-
|
|
223
|
-
### SQL Query
|
|
224
|
-
Write raw SQL directly. The optional AI Assistant lets you describe what you want in plain English and generates a query. AI-generated queries are synced back to the Visual Builder for further refinement.
|
|
225
|
-
|
|
226
|
-
### Slow Queries
|
|
227
|
-
View slow SELECT queries captured from your application in real time. Each slow query can be:
|
|
228
|
-
- **Explained** -- run EXPLAIN to see the execution plan (bypasses blocked table restrictions)
|
|
229
|
-
- **Used** -- copy to the SQL tab for editing and re-running
|
|
230
|
-
- **Optimized** -- get AI-powered optimization suggestions with specific index and rewrite recommendations
|
|
231
|
-
|
|
232
|
-
### Duplicate Indexes
|
|
233
|
-
Scans all tables for redundant indexes -- indexes whose columns are a left-prefix of another index on the same table. Shows the `ALTER TABLE ... DROP INDEX` statement for each duplicate, ready to copy and run.
|
|
234
|
-
|
|
235
|
-
### Table Sizes
|
|
236
|
-
Displays every table sorted by total size, with columns for estimated row count, data size, index size, total size, and fragmented space. Includes a visual size bar for quick comparison.
|
|
237
|
-
|
|
238
|
-
## Configuration Reference
|
|
239
|
-
|
|
240
|
-
| Option | Type | Default | Description |
|
|
241
|
-
|--------|------|---------|-------------|
|
|
242
|
-
| `authenticate` | Proc | `->(_) { true }` | Authorization check |
|
|
243
|
-
| `base_controller` | String | `"ActionController::Base"` | Parent controller class |
|
|
244
|
-
| `featured_tables` | Array | `[]` | Tables shown in Featured group |
|
|
245
|
-
| `blocked_tables` | Array | `[sessions, ...]` | Tables that cannot be queried |
|
|
246
|
-
| `masked_column_patterns` | Array | `[password, secret, ...]` | Column patterns to redact |
|
|
247
|
-
| `default_columns` | Hash | `{}` | Default checked columns per table |
|
|
248
|
-
| `max_row_limit` | Integer | `1000` | Maximum rows returned |
|
|
249
|
-
| `default_row_limit` | Integer | `25` | Default row limit |
|
|
250
|
-
| `query_timeout_ms` | Integer | `30000` | Query timeout in ms |
|
|
251
|
-
| `redis_url` | String | `nil` | Redis URL for slow query monitoring |
|
|
252
|
-
| `slow_query_threshold_ms` | Integer | `250` | Slow query threshold |
|
|
253
|
-
| `audit_logger` | Logger | `nil` | Logger for query audit trail |
|
|
254
|
-
| `ai_endpoint` | String | `nil` | AI API endpoint URL |
|
|
255
|
-
| `ai_api_key` | String | `nil` | AI API key |
|
|
256
|
-
| `ai_model` | String | `nil` | AI model name |
|
|
257
|
-
| `ai_auth_style` | Symbol | `:api_key` | `:bearer` or `:api_key` |
|
|
258
|
-
| `ai_client` | Proc | `nil` | Custom AI client callable |
|
|
259
|
-
| `ai_system_context` | String | `nil` | Domain context for AI prompts |
|
|
104
|
+
For all provider examples, see the [AI Features guide](https://github.com/antarr/mysql_genius/wiki/AI-Features).
|
|
260
105
|
|
|
261
106
|
## Compatibility
|
|
262
107
|
|
|
263
|
-
Tested against:
|
|
264
|
-
|
|
265
108
|
| Rails | Ruby |
|
|
266
109
|
|-------|------|
|
|
267
|
-
| 5.2 | 2.
|
|
268
|
-
| 6.0 | 2.
|
|
269
|
-
| 6.1 | 2.
|
|
110
|
+
| 5.2 | 2.7, 3.0 |
|
|
111
|
+
| 6.0 | 2.7, 3.0, 3.1 |
|
|
112
|
+
| 6.1 | 2.7, 3.0, 3.1, 3.2, 3.3 |
|
|
270
113
|
| 7.0 | 2.7, 3.0, 3.1, 3.2, 3.3 |
|
|
271
|
-
| 7.1 | 2.7, 3.0, 3.1, 3.2, 3.3 |
|
|
272
|
-
| 7.2 | 3.1, 3.2, 3.3 |
|
|
114
|
+
| 7.1 | 2.7, 3.0, 3.1, 3.2, 3.3, 3.4 |
|
|
115
|
+
| 7.2 | 3.1, 3.2, 3.3, 3.4 |
|
|
116
|
+
| 8.0 | 3.2, 3.3, 3.4 |
|
|
117
|
+
| 8.1 | 3.2, 3.3, 3.4 |
|
|
118
|
+
|
|
119
|
+
## Documentation
|
|
120
|
+
|
|
121
|
+
Full documentation is available on the [Wiki](https://github.com/antarr/mysql_genius/wiki).
|
|
273
122
|
|
|
274
123
|
## Development
|
|
275
124
|
|
|
276
|
-
```
|
|
125
|
+
```bash
|
|
277
126
|
git clone https://github.com/antarr/mysql_genius.git
|
|
278
127
|
cd mysql_genius
|
|
279
128
|
bin/setup
|
|
280
129
|
bundle exec rspec
|
|
281
130
|
```
|
|
282
131
|
|
|
283
|
-
To test against a specific Rails version:
|
|
284
|
-
|
|
285
|
-
```
|
|
286
|
-
RAILS_VERSION=6.1 bundle update && bundle exec rspec
|
|
287
|
-
```
|
|
288
|
-
|
|
289
132
|
## Contributing
|
|
290
133
|
|
|
291
134
|
Bug reports and pull requests are welcome on GitHub at https://github.com/antarr/mysql_genius.
|