Skip to content

Instantly share code, notes, and snippets.

@ryu1kn
Last active May 15, 2019 08:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryu1kn/a76694c42432cfd62e9fdf16cb2f5c88 to your computer and use it in GitHub Desktop.
Save ryu1kn/a76694c42432cfd62e9fdf16cb2f5c88 to your computer and use it in GitHub Desktop.
SQL Server Application Intent Read Only with TinyTDS

Read-Only Intent with TinyTDS

Prerequisite

  • Docker

Also set following environment variables: DB_HOST, DB_NAME, DB_USERNAME, DB_PASSWORD

Install dependencies

$ docker build -t tiny-tds-image .

Run with application intent

Check if the right server is executing the query by printing the server name.

$ ./run.sh get-server-name.rb
FROM ruby:2.5
RUN apt-get update -y \
&& apt-get install -y wget build-essential libtool-bin libc6-dev libssl1.0-dev \
&& mkdir -p /opt \
&& cd /opt \
&& wget http://www.freetds.org/files/stable/freetds-1.1.6.tar.gz \
&& tar -xzf freetds-1.1.6.tar.gz \
&& cd freetds-1.1.6 \
&& ./configure --prefix=/usr/local --with-tdsver=7.3 \
&& make \
&& make install
WORKDIR /app
COPY . .
RUN bundle install
CMD ["bundle", "exec", "ruby", "-v"]
source 'https://rubygems.org'
gem 'tiny_tds', git: 'git://github.com/aharpervc/tiny_tds.git', ref: '819810b7cde3c723f6ae9b00c35ac6e422d71ab8'
GIT
remote: git://github.com/aharpervc/tiny_tds.git
revision: 819810b7cde3c723f6ae9b00c35ac6e422d71ab8
ref: 819810b7cde3c723f6ae9b00c35ac6e422d71ab8
specs:
tiny_tds (2.1.1)
GEM
remote: https://rubygems.org/
specs:
PLATFORMS
ruby
DEPENDENCIES
tiny_tds!
BUNDLED WITH
1.17.3
require 'tiny_tds'
client = TinyTds::Client.new(
host: ENV['DB_HOST'],
username: ENV['DB_USERNAME'],
password: ENV['DB_PASSWORD'],
database: ENV['DB_NAME'],
read_only_intent: true,
contained: true,
message_handler: Proc.new { |m| puts m.message }
)
client.execute('print @@servername').do
#!/bin/bash
set -euo pipefail
work_dir=/app
image_name=tiny-tds-image
script_name=$1
docker run -it --rm --name tiny-tds \
-e DB_HOST -e DB_USERNAME -e DB_PASSWORD -e DB_NAME \
-v `pwd`:$work_dir \
-w $work_dir \
$image_name \
bundle exec ruby $script_name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment