Skip to content

Commit

Permalink
add sql server support
Browse files Browse the repository at this point in the history
  • Loading branch information
omohokcoj committed Nov 15, 2021
1 parent c19bc34 commit f282e1d
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ ENV RAILS_ENV=production

WORKDIR /opt

RUN apt update && apt install -y wget libsqlite3-dev libmysqlclient-dev libpq-dev && rm -rf /var/lib/apt/lists/*
RUN apt update && apt install -y wget libsqlite3-dev libmysqlclient-dev libpq-dev freetds-dev && rm -rf /var/lib/apt/lists/*

RUN wget -O motor-admin https://github.com/motor-admin/motor-admin/releases/download/latest/motor-admin-Linux-$(uname -m) && chmod +x ./motor-admin

Expand Down
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ ruby '2.7.1'

gem 'rails', '~> 7.0.0.alpha'

gem 'activerecord-sqlserver-adapter', git: 'https://github.com/omohokcoj/activerecord-sqlserver-adapter', require: false
gem 'ar_lazy_preload'
gem 'audited'
gem 'devise', git: 'https://github.com/heartcombo/devise'
Expand All @@ -15,6 +16,7 @@ gem 'image_processing'
gem 'jwt'
gem 'lograge'
gem 'motor-admin'
gem 'tiny_tds', '2.1.5', require: false
gem 'mysql2', '0.5.3', require: false
gem 'oj'
gem 'pg', '1.2.3', require: false
Expand Down
13 changes: 12 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ GIT
responders
warden (~> 1.2.3)

GIT
remote: https://github.com/omohokcoj/activerecord-sqlserver-adapter
revision: e1c5dff05828f4a8e30ae726187bdd930d995ad1
specs:
activerecord-sqlserver-adapter (6.1.2.1)
activerecord
tiny_tds

GEM
remote: https://rubygems.org/
specs:
Expand Down Expand Up @@ -131,7 +139,7 @@ GEM
mini_mime (1.1.2)
mini_portile2 (2.6.1)
minitest (5.14.4)
motor-admin (0.2.24)
motor-admin (0.2.25)
activerecord-filter (>= 5.2)
ar_lazy_preload (~> 0.6)
audited (~> 5.0)
Expand Down Expand Up @@ -239,6 +247,7 @@ GEM
sprockets (>= 3.0.0)
sqlite3 (1.4.2)
thor (1.1.0)
tiny_tds (2.1.5)
tzinfo (2.0.4)
concurrent-ruby (~> 1.0)
tzinfo-data (1.2021.5)
Expand Down Expand Up @@ -268,6 +277,7 @@ PLATFORMS
x86_64-darwin-20

DEPENDENCIES
activerecord-sqlserver-adapter!
ar_lazy_preload
audited
brakeman
Expand All @@ -293,6 +303,7 @@ DEPENDENCIES
rubocop-rspec
spring
sqlite3
tiny_tds (= 2.1.5)
tzinfo-data
web-console
webpacker
Expand Down
26 changes: 15 additions & 11 deletions app/controllers/api/verify_db_connection_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,25 @@ def create
def verify_db_connection!(url)
if url.starts_with?('postgres')
PG.connect(url).close
elsif url.starts_with?('sqlserver')
TinyTds::Client.new(parse_options(url)).close
elsif url.starts_with?('mysql')
uri = URI(url)

options = {
username: uri.user,
host: uri.host,
password: uri.password,
port: uri.port,
database: uri.path.delete_prefix('/')
}

Mysql2::Client.new(options).close
Mysql2::Client.new(parse_options(url)).close
else
raise InvalidUrl, 'Database URL is invalid'
end
end

def parse_options(url)
uri = URI(url)

{
username: uri.user,
host: uri.host,
password: uri.password,
port: uri.port,
database: uri.path.delete_prefix('/')
}
end
end
end
5 changes: 3 additions & 2 deletions app/packs/application/components/database_form.vue
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
:label="option.value"
border
size="large"
:style="index === 0 ? 'margin-right: 25px !important' : ''"
:style="index !== 0 ? 'margin-left: 15px !important' : ''"
class="my-1 me-0 w-100"
>
{{ option.label }}
Expand Down Expand Up @@ -149,7 +149,8 @@ export default {
dbTypeOptions () {
return [
{ label: 'PostgreSQL', value: 'postgres' },
{ label: 'MySQL', value: 'mysql2' }
{ label: 'MySQL', value: 'mysql2' },
{ label: 'SQL Server', value: 'sqlserver' }
]
}
},
Expand Down
7 changes: 7 additions & 0 deletions config/boot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,10 @@ def fill_absolute_paths_cache
rescue LoadError
false
end

begin
require 'tiny_tds'
require 'activerecord-sqlserver-adapter'
rescue LoadError
false
end

0 comments on commit f282e1d

Please sign in to comment.