|
| 1 | +## TinyTDS Note |
| 2 | + |
| 3 | +Current versions of the adapter use TinyTDS rendering much of this documentation obsolete. You can still follow this guide but should be able to skip the Unix ODBC sections. Then look at [[Using TinyTDS]] for more info. |
| 4 | + |
| 5 | +## Configure SQL Server 2008 |
| 6 | + |
| 7 | +By default the server does not listen on a TCP port. Enable it following |
| 8 | +these "instructions.":http://msdn.microsoft.com/en-us/library/bb909712.aspx |
| 9 | + |
| 10 | +## FreeTDS |
| 11 | + |
| 12 | +This package implements the TDS protocol between SQL Server and the Ubuntu box. |
| 13 | +The Ubuntu packages for this are: |
| 14 | + |
| 15 | +@sudo apt-get install freetds-dev freetds-bin tdsodbc@ |
| 16 | + |
| 17 | +## UnixODBC |
| 18 | + |
| 19 | +This package implements and ODBC layer over FreeTDS The Ubuntu packages for |
| 20 | +this are: |
| 21 | + |
| 22 | +@sudo apt-get install unixodbc unixodbc-dev@ |
| 23 | + |
| 24 | +## Configure FreeTDS |
| 25 | + |
| 26 | +FreeTDS needs a configuration file named /etc/freetds/freetds.conf. |
| 27 | + |
| 28 | +The example below has two entries. The first entry, [developer], tells FreeTDS how to |
| 29 | +connect to a named SQL Server 2008 instance named DEVELOPER. |
| 30 | + |
| 31 | +The second entry, [production], tells FreeTDS how to connect to the default SQL Server |
| 32 | +instance. In this case no 'instance' parameter is required. |
| 33 | + |
| 34 | +<pre> |
| 35 | +[developer] |
| 36 | +host = endor |
| 37 | +port = 1433 |
| 38 | +instance = DEVELOPER # connect to a named instance |
| 39 | +tds version = 8.0 |
| 40 | +client charset = UTF-8 |
| 41 | + |
| 42 | +[production] |
| 43 | +host = endor |
| 44 | +port = 1433 |
| 45 | +tds version = 8.0 |
| 46 | +client charset = UTF-8 |
| 47 | +</pre> |
| 48 | + |
| 49 | +## Test FreeTDS |
| 50 | + |
| 51 | +Use the command line client sqsh to test your FreeTDS configuration. |
| 52 | + |
| 53 | +@sudo apt-get install sqsh@ |
| 54 | + |
| 55 | +For example, to connect to the developer database and perform a count on the people |
| 56 | +table do this: |
| 57 | + |
| 58 | +@sqsh -S developer -U database_username -P database_password@ |
| 59 | + |
| 60 | +A sqsh prompt should open up. |
| 61 | +<pre> |
| 62 | +> use project_development |
| 63 | +> go |
| 64 | +> select count(*) from people |
| 65 | +> go |
| 66 | +</pre> |
| 67 | + |
| 68 | +You should see the result of the count. |
| 69 | + |
| 70 | +## Configure UnixODBC |
| 71 | + |
| 72 | +Tell UnixODBC where the FreeTDS driver is. In /etc/odbcinst.ini put the |
| 73 | +following: |
| 74 | + |
| 75 | +<pre> |
| 76 | +[FreeTDS] |
| 77 | +Description = TDS driver (Sybase/MS SQL) |
| 78 | +Driver = /usr/lib/odbc/libtdsodbc.so |
| 79 | +Setup = /usr/lib/odbc/libtdsS.so |
| 80 | +CPTimeout = |
| 81 | +CPReuse = |
| 82 | +FileUsage = 1 |
| 83 | +</pre> |
| 84 | + |
| 85 | +or |
| 86 | +Ubuntu after to 12.04 has a different odbc path. |
| 87 | +<pre> |
| 88 | +[FreeTDS] |
| 89 | +Description = TDS driver (Sybase/MS SQL) |
| 90 | +Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so |
| 91 | +Setup = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so |
| 92 | +CPTimeout = |
| 93 | +CPReuse = |
| 94 | +FileUsage = 1 |
| 95 | +</pre> |
| 96 | +## Create the ODBC entries for you databases |
| 97 | + |
| 98 | +ODBC DSN entries are defined in /etc/odbc.ini. |
| 99 | + |
| 100 | +Note that the names you give these entries are the names you'll use in your |
| 101 | +rails database.yml file. |
| 102 | + |
| 103 | +The template for an odbc.ini entry is: |
| 104 | + |
| 105 | +<pre> |
| 106 | +[dsn] #this is the name you use for the 'dsn' field in your rails database.yml |
| 107 | +Driver = FreeTDS |
| 108 | +Description = ODBC connection via FreeTDS |
| 109 | +Trace = No |
| 110 | +Servername = myserver # This is the name of an entry in your /etc/freetds/freetds.conf file |
| 111 | +Database = actual_database_name # This is the name of a database in your SQL Server instance. |
| 112 | +</pre> |
| 113 | + |
| 114 | +My /etc/odbc.ini looks like this: |
| 115 | + |
| 116 | +<pre> |
| 117 | +[project_development] |
| 118 | +Driver = FreeTDS |
| 119 | +Description = ODBC connection via FreeTDS |
| 120 | +Trace = No |
| 121 | +Servername = developer |
| 122 | +Database = project_development |
| 123 | + |
| 124 | +[project_test] |
| 125 | +Driver = FreeTDS |
| 126 | +Description = ODBC connection via FreeTDS |
| 127 | +Trace = No |
| 128 | +Servername = developer |
| 129 | +Database = test |
| 130 | + |
| 131 | +[project_production] |
| 132 | +Driver = FreeTDS |
| 133 | +Description = ODBC connection via FreeTDS |
| 134 | +Trace = No |
| 135 | +Servername = production |
| 136 | +Database = project_production |
| 137 | +</pre> |
| 138 | + |
| 139 | +## Test UnixODBC |
| 140 | + |
| 141 | +You can test your ODBC configuration with the isql command. For example: |
| 142 | + |
| 143 | +<pre> |
| 144 | +isql -v project_development database_username password |
| 145 | +SQL> select count(*) from people; |
| 146 | +</pre> |
| 147 | + |
| 148 | +returns the count of records in the people table. |
| 149 | + |
| 150 | +## Install ruby-odbc |
| 151 | + |
| 152 | +ruby-odbc is the ruby binding to the UnixODBC library. |
| 153 | + |
| 154 | +h3. Install ruby-odbc from apt |
| 155 | + |
| 156 | +On Ubuntu install the package libodbc-ruby1.8 |
| 157 | + |
| 158 | +@sudo apt-get install libodbc-ruby1.8@ |
| 159 | + |
| 160 | +h3. Install ruby-odbc from source (when the installation from apt doesn't work) |
| 161 | + |
| 162 | +The ruby-odbc library is available in the apt repository and the version in apt has worked in the past, but if the ruby-odbc layer returns an error like |
| 163 | +@ODBC::Error: INTERN (0) [RubyODBC]Cannot allocate SQLHENV@ |
| 164 | +then try installing from source. |
| 165 | + |
| 166 | +The version in apt is compiled to dynamically load the odbc library at run time. This allows ruby-odbc to pick between the iODBC library and the UnixODBC library. There are cases where ruby-odbc is unable to load the UnixODBC library at run time. This behavior can be changed at compile time. |
| 167 | + |
| 168 | +First remove the apt package if you installed it. |
| 169 | +@sudo apt-get remove librubyodbc-ruby1.8@ |
| 170 | + |
| 171 | +Get the latest source package from "http://www.ch-werner.de/rubyodbc/":http://www.ch-werner.de/rubyodbc/. |
| 172 | + |
| 173 | +Follow the instructions in the README, using the following command to disable dynamically loading the odbc library at runtime (the --disable-dlopen is the key bit): |
| 174 | + |
| 175 | +@ruby -Cext extconf.rb --disable-dlopen@ |
| 176 | + |
| 177 | +h3. Test ruby-odbc using irb |
| 178 | +<pre> |
| 179 | +irb > require 'odbc' |
| 180 | +irb > ODBC.connect("dsn", "username", "password") |
| 181 | +</pre> |
| 182 | + |
| 183 | +If you don't get an error ruby-odbc is good to go. |
| 184 | + |
| 185 | +## Install The Rails Adapter |
| 186 | + |
| 187 | +Follow the Installation instructions in the adapter's README, located here: |
| 188 | +"README":http://.com/rails-sqlserver/2000-2005-adapter/tree/master |
| 189 | + |
| 190 | +Scroll down to the "Installation" section. |
| 191 | + |
| 192 | +## Setup your database.yml |
| 193 | + |
| 194 | +The database.yml setup is pretty simple. There is a special case for the 'test' |
| 195 | +entry. If your test database name doesn't match the DSN name for that database |
| 196 | +you must explicitly set the database name by assigning to the database field. |
| 197 | + |
| 198 | +<pre> |
| 199 | +production: |
| 200 | +adapter: sqlserver |
| 201 | +mode: odbc |
| 202 | +dsn: project_production |
| 203 | +username: dbuser |
| 204 | +password: password |
| 205 | +encoding: utf8 |
| 206 | + |
| 207 | +development: |
| 208 | +adapter: sqlserver |
| 209 | +mode: odbc |
| 210 | +dsn: project_development |
| 211 | +username: dbuser |
| 212 | +password: password |
| 213 | +encoding: utf8 |
| 214 | + |
| 215 | +test: |
| 216 | +adapter: sqlserver |
| 217 | +mode: odbc |
| 218 | +dsn: project_test |
| 219 | +database: test #This must be the real name of the database on the server, not the ODBC DSN! Only required for test. |
| 220 | +username: dbuser |
| 221 | +password: password |
| 222 | +encoding: utf8 |
| 223 | +</pre> |
| 224 | + |
| 225 | +## Test that the application can talk to the database |
| 226 | + |
| 227 | +Enter the console and query a table using active_record: |
| 228 | + |
| 229 | +<pre> |
| 230 | +script/console |
| 231 | +> Person.count |
| 232 | +</pre> |
| 233 | + |
| 234 | +## One last bit of Rails hackery. |
| 235 | + |
| 236 | +There are several places where Rails' lib/tasks/database.rake assumes it is |
| 237 | +installed on windows and calls oslq. The test:purge task does this so you can't |
| 238 | +run your tests from Ubuntu. My solution is to simply edit that task in place like this: |
| 239 | + |
| 240 | +<pre> |
| 241 | +when "sqlserver" |
| 242 | +- dropfkscript = "#{abcs["test"]["host"]}.#{abcs["test"]["database"]}.DP1".gsub(/\\/,'-') |
| 243 | +- `osql -E -S #{abcs["test"]["host"]} -d #{abcs["test"]["database"]} -i db\\#{dropfkscript}` |
| 244 | +- `osql -E -S #{abcs["test"]["host"]} -d #{abcs["test"]["database"]} -i db\\#{RAILS_ENV}_structure.sql` |
| 245 | ++ ActiveRecord::Base.clear_active_connections! |
| 246 | ++ ActiveRecord::Base.connection.recreate_database(abcs["test"]["database"]) |
| 247 | +when "oci", "oracle" |
| 248 | +ActiveRecord::Base.establish_connection(:test) |
| 249 | +</pre> |
0 commit comments