Domanda

See "UPDATE" below for what I now know is the crux of the problem.

I have a legacy Derby database that I want to make a Rails application to interface with.
I am using RVM so here are the steps I took:

1. rvm install jruby (installed 1.6.7.2)

2. rvm use jruby

3. gem install rails 

4. rails new myapp

5. add "gem 'activerecord-jdbcderby-adapter'" to Gemfile of new rails app

6. bundle

7. copied my derby db folder (named 'perm') under db in the rails app

8. changed database.yml as follows:

  development:
    adapter: jdbcderby
    database: db/perm

Then I made a model file and used set_table_name to set it to one of the table names and when I run rails c I get an exception that the table does not exist.

Also in the console when I do

ActiveRecord::Base.connection.tables

the only table that comes back is "schema_migrations".

I know there is nothing wrong with the database as I can connect to this exact copy and see all the tables using SquirrelSQL. Also, the rails app is connecting in some way since when I open the console I cannot, while the console is running, connect to the same instance using SquirrelSQL, and vice versa.

Does anyone have any suggestion as to why active record doesn't see the tables?

UPDATE:

The problem has something to do with how derby "organizes" tables into multiple "schemas" in the same file. When I create a new table from rails (i.e. with a migration) the table ends up in the "SA" schema. All of my legacy tables are in the "APP" schema (maybe I could move them but I don't want to do that if I can avoid it...other apps would break). So when I access the db from rails this way it's like only the "SA" schema exists. How do I tell Rails to 'use' the "App" schema (early on I tried prefacing the table name but that didn't work)?

I retitled the question accordingly.

UPDATE #2:

Apparently the jdbcderby gem supports the "schema" setting. On a guess I tried changing my database.yml to the following:

development:
  adapter: jdbcderby
  database: db/perm
  schema: app (also tried APP)

When I have the app (or APP) schema setting when I do ActiveRecord::Base.connection.tables in the console I get the list of tables from the app schema (the list shows up with table names all lower case with no schema name).

But I am still having trouble accessing the tables. When I make a model file on an existing table and try to access it I get a JDBCError: "Schema 'SA' does not exist". I have tried various set_table_name calls with no success.

As far as I know there is nothing unusual about my database. But there is no information anywhere on how to do this. Am I the only person on earth who has ever tried to use a legacy Derby database with Rails?

È stato utile?

Soluzione

The database name 'db/perm' is being interpreted relative to your current working directory, which is probably not the directory that you think it is. Try either (1) searching your hard disk for the file 'derby.log', which is probably being created in the actual current working directory of your rails app, or (2) specifying the absolute file path to your derby database, not a relative file path.

If the problem is the schema, Derby supports the SET SCHEMA statement: http://db.apache.org/derby/docs/10.8/ref/rrefsqlj32268.html

If you don't explicitly set the schema, it defaults to the username that you connect with, so you can also indirectly set the schema by logging in as the desired username.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top