-
Notifications
You must be signed in to change notification settings - Fork 91
feat(CE): add mysql source and destination connectors #1032
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dataengineergaurav
wants to merge
5
commits into
Multiwoven:main
Choose a base branch
from
dataengineergaurav:feature/mysql-connector
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e9bdeb3
feat(CE): add mysql source and destination connectors
gauravgurjar-de bc3b85f
fix(CE): fix group_by_table bug and improve test coverage
gauravgurjar-de dc56d0f
fix(mysql): address connection leaks, SQL injection, and PII logging
dataengineergaurav 7c27fa5
chore: bump version to 0.38.0
dataengineergaurav c4fbfe0
feat: add MySQL connector support, auto-create tables, and fix gemspec
dataengineergaurav File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
integrations/lib/multiwoven/integrations/destination/mysql/client.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Multiwoven::Integrations::Destination | ||
| module Mysql | ||
| include Multiwoven::Integrations::Core | ||
| class Client < DestinationConnector | ||
| def check_connection(connection_config) | ||
| connection_config = connection_config.with_indifferent_access | ||
| create_connection(connection_config) | ||
| ConnectionStatus.new(status: ConnectionStatusType["succeeded"]).to_multiwoven_message | ||
| rescue StandardError => e | ||
| ConnectionStatus.new(status: ConnectionStatusType["failed"], message: e.message).to_multiwoven_message | ||
| end | ||
|
|
||
| def discover(connection_config) | ||
| connection_config = connection_config.with_indifferent_access | ||
| query = "SELECT table_name, column_name, data_type, is_nullable | ||
| FROM information_schema.columns | ||
| WHERE table_schema = '#{connection_config[:database]}' | ||
| ORDER BY table_name, ordinal_position;" | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| db = create_connection(connection_config) | ||
| records = db.fetch(query) do |result| | ||
| result.map do |row| | ||
| row | ||
| end | ||
| end | ||
| catalog = Catalog.new(streams: create_streams(records)) | ||
| catalog.to_multiwoven_message | ||
| rescue StandardError => e | ||
| handle_exception(e, { | ||
| context: "MYSQL:DISCOVER:EXCEPTION", | ||
| type: "error" | ||
| }) | ||
| end | ||
|
|
||
| def write(sync_config, records, action = "destination_insert") | ||
| connection_config = sync_config.destination.connection_specification.with_indifferent_access | ||
| table_name = sync_config.stream.name | ||
| primary_key = sync_config.model.primary_key | ||
| db = create_connection(connection_config) | ||
|
|
||
| log_message_array = [] | ||
| write_success = 0 | ||
| write_failure = 0 | ||
|
|
||
| records.each do |record| | ||
| query = Multiwoven::Integrations::Core::QueryBuilder.perform(action, table_name, record, primary_key) | ||
| logger.debug("MYSQL:WRITE:QUERY query = #{query} sync_id = #{sync_config.sync_id} sync_run_id = #{sync_config.sync_run_id}") | ||
| begin | ||
| db.run(query) | ||
| write_success += 1 | ||
| log_message_array << log_request_response("info", query, "Successful") | ||
| rescue StandardError => e | ||
| handle_exception(e, { | ||
| context: "MYSQL:RECORD:WRITE:EXCEPTION", | ||
| type: "error", | ||
| sync_id: sync_config.sync_id, | ||
| sync_run_id: sync_config.sync_run_id | ||
| }) | ||
| write_failure += 1 | ||
| log_message_array << log_request_response("error", query, e.message) | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| end | ||
| end | ||
| tracking_message(write_success, write_failure, log_message_array) | ||
| rescue StandardError => e | ||
| handle_exception(e, { | ||
| context: "MYSQL:WRITE:EXCEPTION", | ||
| type: "error", | ||
| sync_id: sync_config.sync_id, | ||
| sync_run_id: sync_config.sync_run_id | ||
| }) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def create_connection(connection_config) | ||
| Sequel.connect( | ||
| adapter: "mysql2", | ||
| host: connection_config[:host], | ||
| port: connection_config[:port], | ||
| user: connection_config[:username], | ||
| password: connection_config[:password], | ||
| database: connection_config[:database] | ||
| ) | ||
| end | ||
|
|
||
| def create_streams(records) | ||
| group_by_table(records).map do |_, r| | ||
| Multiwoven::Integrations::Protocol::Stream.new(name: r[:tablename], action: StreamAction["fetch"], json_schema: convert_to_json_schema(r[:columns])) | ||
| end | ||
| end | ||
|
|
||
| def group_by_table(records) | ||
| result = {} | ||
| records.each_with_index do |entry, index| | ||
| table_name = entry[:table_name] | ||
| column_data = { | ||
| column_name: entry[:column_name], | ||
| data_type: entry[:data_type], | ||
| is_nullable: entry[:is_nullable] == "YES" | ||
| } | ||
| result[index] ||= {} | ||
| result[index][:tablename] = table_name | ||
| result[index][:columns] = [column_data] | ||
| end | ||
| result.values.group_by { |entry| entry[:tablename] }.transform_values do |entries| | ||
| { tablename: entries.first[:tablename], columns: entries.flat_map { |entry| entry[:columns] } } | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
15 changes: 15 additions & 0 deletions
15
integrations/lib/multiwoven/integrations/destination/mysql/config/meta.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| { | ||
| "data": { | ||
| "name": "Mysql", | ||
| "title": "MySQL", | ||
| "connector_type": "destination", | ||
| "category": "Database", | ||
| "documentation_url": "https://docs.multiwoven.com/guides/destinations/mysql", | ||
| "github_issue_label": "destination-mysql", | ||
| "icon": "icon.svg", | ||
| "license": "MIT", | ||
| "release_stage": "alpha", | ||
| "support_level": "community", | ||
| "tags": ["language:ruby", "multiwoven"] | ||
| } | ||
| } |
48 changes: 48 additions & 0 deletions
48
integrations/lib/multiwoven/integrations/destination/mysql/config/spec.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| { | ||
| "documentation_url": "https://docs.multiwoven.com/guides/destinations/mysql", | ||
| "stream_type": "dynamic", | ||
| "connector_query_type": "raw_sql", | ||
| "connection_specification": { | ||
| "$schema": "http://json-schema.org/draft-07/schema#", | ||
| "title": "MySQL", | ||
| "type": "object", | ||
| "required": ["host", "port", "username", "password", "database"], | ||
| "properties": { | ||
| "host": { | ||
| "description": "The hostname or IP address of the server where the MySQL database is hosted.", | ||
| "examples": ["localhost"], | ||
| "type": "string", | ||
| "title": "Host", | ||
| "order": 0 | ||
| }, | ||
| "port": { | ||
| "description": "The port number on which the MySQL server is listening for connections.", | ||
| "examples": ["3306"], | ||
| "type": "string", | ||
| "title": "Port", | ||
| "order": 1 | ||
| }, | ||
| "username": { | ||
| "description": "The username used to authenticate and connect to the MySQL database.", | ||
| "examples": ["root"], | ||
| "type": "string", | ||
| "title": "Username", | ||
| "order": 2 | ||
| }, | ||
| "password": { | ||
| "description": "The password corresponding to the username used for authentication.", | ||
| "type": "string", | ||
| "multiwoven_secret": true, | ||
| "title": "Password", | ||
| "order": 3 | ||
| }, | ||
| "database": { | ||
| "description": "The name of the specific database within the MySQL server to connect to.", | ||
| "examples": ["mydatabase"], | ||
| "type": "string", | ||
| "title": "Database", | ||
| "order": 4 | ||
| } | ||
| } | ||
| } | ||
| } |
6 changes: 6 additions & 0 deletions
6
integrations/lib/multiwoven/integrations/destination/mysql/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ | |
|
|
||
| module Multiwoven | ||
| module Integrations | ||
| VERSION = "0.35.0" | ||
| VERSION = "0.35.1" | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dataengineergaurav Raise to 0.38.0. We have some other connector PRs that need to be merged before this one
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. covered. |
||
|
|
||
| ENABLED_SOURCES = %w[ | ||
| Snowflake | ||
|
|
@@ -15,6 +15,7 @@ module Integrations | |
| Clickhouse | ||
| AmazonS3 | ||
| MariaDB | ||
| Mysql | ||
| Oracle | ||
| DatabricksModel | ||
| AwsSagemakerModel | ||
|
|
@@ -52,6 +53,7 @@ module Integrations | |
| Http | ||
| Iterable | ||
| MariaDB | ||
| Mysql | ||
| DatabricksLakehouse | ||
| Oracle | ||
| MicrosoftExcel | ||
|
|
||
92 changes: 92 additions & 0 deletions
92
integrations/lib/multiwoven/integrations/source/mysql/client.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Multiwoven::Integrations::Source | ||
| module Mysql | ||
| include Multiwoven::Integrations::Core | ||
| class Client < SourceConnector | ||
| def check_connection(connection_config) | ||
| connection_config = connection_config.with_indifferent_access | ||
| create_connection(connection_config) | ||
| ConnectionStatus.new(status: ConnectionStatusType["succeeded"]).to_multiwoven_message | ||
| rescue StandardError => e | ||
| ConnectionStatus.new(status: ConnectionStatusType["failed"], message: e.message).to_multiwoven_message | ||
| end | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| def discover(connection_config) | ||
| connection_config = connection_config.with_indifferent_access | ||
| query = "SELECT table_name, column_name, data_type, is_nullable FROM information_schema.columns WHERE table_schema = '#{connection_config[:database]}' ORDER BY table_name, ordinal_position;" | ||
| db = create_connection(connection_config) | ||
| results = query_execution(db, query) | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| catalog = Catalog.new(streams: create_streams(results)) | ||
| catalog.to_multiwoven_message | ||
| rescue StandardError => e | ||
| handle_exception(e, { | ||
| context: "MYSQL:DISCOVER:EXCEPTION", | ||
| type: "error" | ||
| }) | ||
| end | ||
|
|
||
| def read(sync_config) | ||
| connection_config = sync_config.source.connection_specification.with_indifferent_access | ||
| query = sync_config.model.query | ||
| query = batched_query(query, sync_config.limit, sync_config.offset) unless sync_config.limit.nil? && sync_config.offset.nil? | ||
| db = create_connection(connection_config) | ||
| query(db, query) | ||
| rescue StandardError => e | ||
| handle_exception(e, { | ||
| context: "MYSQL:READ:EXCEPTION", | ||
| type: "error", | ||
| sync_id: sync_config.sync_id, | ||
| sync_run_id: sync_config.sync_run_id | ||
| }) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def create_connection(connection_config) | ||
| Sequel.connect( | ||
| adapter: "mysql2", | ||
| host: connection_config[:host], | ||
| port: connection_config[:port], | ||
| user: connection_config[:username], | ||
| password: connection_config[:password], | ||
| database: connection_config[:database] | ||
| ) | ||
| end | ||
|
|
||
| def query_execution(db, query) | ||
| db.fetch(query).all | ||
| end | ||
|
|
||
| def create_streams(records) | ||
| group_by_table(records).map do |_, r| | ||
| Multiwoven::Integrations::Protocol::Stream.new(name: r[:tablename], action: StreamAction["fetch"], json_schema: convert_to_json_schema(r[:columns])) | ||
| end | ||
| end | ||
|
|
||
| def query(db, query) | ||
| records = [] | ||
| query_execution(db, query).map do |row| | ||
| records << RecordMessage.new(data: row, emitted_at: Time.now.to_i).to_multiwoven_message | ||
| end | ||
| records | ||
| end | ||
|
|
||
| def group_by_table(records) | ||
| result = {} | ||
| records.each_with_index do |entry, index| | ||
| table_name = entry[:table_name] | ||
| column_data = { | ||
| column_name: entry[:column_name], | ||
| data_type: entry[:data_type], | ||
| is_nullable: entry[:is_nullable] == "YES" | ||
| } | ||
| result[index] ||= {} | ||
| result[index][:tablename] = table_name | ||
| result[index][:columns] = [column_data] | ||
| end | ||
| result | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| end | ||
| end | ||
| end | ||
| end | ||
16 changes: 16 additions & 0 deletions
16
integrations/lib/multiwoven/integrations/source/mysql/config/meta.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| { | ||
| "data": { | ||
| "name": "Mysql", | ||
| "title": "MySQL", | ||
| "connector_type": "source", | ||
| "category": "Data Warehouse", | ||
| "sub_category": "Relational Database", | ||
| "documentation_url": "https://docs.multiwoven.com/guides/sources/mysql", | ||
| "github_issue_label": "source-mysql", | ||
| "icon": "icon.svg", | ||
| "license": "MIT", | ||
| "release_stage": "alpha", | ||
| "support_level": "community", | ||
| "tags": ["language:ruby", "multiwoven"] | ||
| } | ||
| } |
48 changes: 48 additions & 0 deletions
48
integrations/lib/multiwoven/integrations/source/mysql/config/spec.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| { | ||
| "documentation_url": "https://docs.multiwoven.com/guides/sources/mysql", | ||
| "stream_type": "dynamic", | ||
| "connector_query_type": "raw_sql", | ||
| "connection_specification": { | ||
| "$schema": "http://json-schema.org/draft-07/schema#", | ||
| "title": "MySQL", | ||
| "type": "object", | ||
| "required": ["host", "port", "username", "password", "database"], | ||
| "properties": { | ||
| "host": { | ||
| "description": "The hostname or IP address of the server where the MySQL database is hosted.", | ||
| "examples": ["localhost"], | ||
| "type": "string", | ||
| "title": "Host", | ||
| "order": 0 | ||
| }, | ||
| "port": { | ||
| "description": "The port number on which the MySQL server is listening for connections.", | ||
| "examples": ["3306"], | ||
| "type": "string", | ||
| "title": "Port", | ||
| "order": 1 | ||
| }, | ||
| "username": { | ||
| "description": "The username used to authenticate and connect to the MySQL database.", | ||
| "examples": ["root"], | ||
| "type": "string", | ||
| "title": "Username", | ||
| "order": 2 | ||
| }, | ||
| "password": { | ||
| "description": "The password corresponding to the username used for authentication.", | ||
| "type": "string", | ||
| "multiwoven_secret": true, | ||
| "title": "Password", | ||
| "order": 3 | ||
| }, | ||
| "database": { | ||
| "description": "The name of the specific database within the MySQL server to connect to.", | ||
| "examples": ["mydatabase"], | ||
| "type": "string", | ||
| "title": "Database", | ||
| "order": 4 | ||
| } | ||
| } | ||
| } | ||
| } |
6 changes: 6 additions & 0 deletions
6
integrations/lib/multiwoven/integrations/source/mysql/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.