Usage with ApacheAGE for Postgresql
btihen opened this issue · 2 comments
I am trying to write a Rails PG Apache AGE adapter (https://age.apache.org/) a PG GraphDB extension. I have the basics working in development mode, however, I am not sure how to get it to work with both development and test.
I am using the database Config:
default: &default
adapter: postgresql
encoding: unicode
username: postgresUser
password: postgresPW
host: localhost
port: 5455
# For details on connection pooling, see Rails configuration guide
# https://guides.rubyonrails.org/configuring.html#database-pooling
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
schema_search_path: "ag_catalog,age_schema,public"
development:
<<: *default
database: graphdb_mini_development
test:
<<: *default
database: graphdb_mini_test
production:
<<: *default
database: graphdb_mini_production
username: graphdb_mini
password: <%= ENV["GRAPHDB_MINI_DATABASE_PASSWORD"] %>
I have a setup migration with:
class AddAgeBaseMigration < ActiveRecord::Migration[7.1]
def change
# allow age extension
execute("CREATE EXTENSION IF NOT EXISTS age;")
# load the age code
execute("LOAD 'age';")
# load the ag_catalog into the search path
execute('SET search_path = ag_catalog, "$user", public;')
# creates our AGE schema
# USE: `execute("SELECT create_graph('age_schema');")`, as we need to use: `create_graph` function
# NOT: `ActiveRecord::Base.connection.create_schema('age_schema')`
execute("SELECT create_graph('age_schema');")
end
end
This creates the schema:
ActiveRecord::Schema[7.1].define(version: 2024_05_14_224709) do
create_schema "ag_catalog"
create_schema "age_schema"
# These are extensions that must be enabled in order to support this database
enable_extension "age"
enable_extension "plpgsql"
# Could not dump table "ag_graph" because of following StandardError
# Unknown type 'regnamespace' for column 'namespace'
# Could not dump table "ag_label" because of following StandardError
# Unknown type 'regclass' for column 'relation'
add_foreign_key "ag_label", "ag_graph", column: "graph", primary_key: "graphid", name: "fk_graph_oid"
end
all goes well when I do:
~/devel/learn/mentor/age/graphdb_mini main ?25 Ruby 3.3.1 Node 20.12.0 Ruby ruby-3.3.1 19:56:59
wti0405@GREM-VPJQF2D52M ❯ bin/rails db:create
Created database 'graphdb_mini_development'
Created database 'graphdb_mini_test'
~/devel/learn/mentor/age/graphdb_mini main ?25 Ruby 3.3.1 Node 20.12.0 Ruby ruby-3.3.1 19:57:07
wti0405@GREM-VPJQF2D52M ❯ bin/rails db:migrate
== 20240514224709 AddAgeBaseMigration: migrating ==============================
-- execute("CREATE EXTENSION IF NOT EXISTS age;")
-> 0.0391s
-- execute("LOAD 'age';")
-> 0.0044s
-- execute("SET search_path = ag_catalog, \"$user\", public;")
-> 0.0025s
-- execute("SELECT create_graph('age_schema');")
-> 0.0117s
== 20240514224709 AddAgeBaseMigration: migrated (0.0586s) =====================
unknown OID 4089: failed to recognize type of 'namespace'. It will be treated as String.
unknown OID 2205: failed to recognize type of 'relation'. It will be treated as String.
now I can successfully do (I assume the warnings aren't a problem):
bin/rails c
and test lots of interesting stuff including my code.
however, when I want to run tests I get:
bin/rails test
yarn install v1.22.19
[1/4] 🔍 Resolving packages...
success Already up-to-date.
✨ Done in 0.06s.
yarn run v1.22.19
$ yarn build:css:compile && yarn build:css:prefix
$ sass ./app/assets/stylesheets/application.bootstrap.scss:./app/assets/builds/application.css --no-source-map --load-path=node_modules
$ postcss ./app/assets/builds/application.css --use=autoprefixer --output=./app/assets/builds/application.css
✨ Done in 2.75s.
bin/rails aborted!
ActiveRecord::StatementInvalid: PG::DuplicateObject: ERROR: constraint "fk_graph_oid" for relation "ag_label" already exists (ActiveRecord::StatementInvalid)
/Users/wti0405/devel/learn/mentor/age/graphdb_mini/db/schema.rb:33:in `block in <main>'
/Users/wti0405/devel/learn/mentor/age/graphdb_mini/db/schema.rb:13:in `<main>'
Caused by:
PG::DuplicateObject: ERROR: constraint "fk_graph_oid" for relation "ag_label" already exists (PG::DuplicateObject)
/Users/wti0405/devel/learn/mentor/age/graphdb_mini/db/schema.rb:33:in `block in <main>'
/Users/wti0405/devel/learn/mentor/age/graphdb_mini/db/schema.rb:13:in `<main>'
Tasks: TOP => db:test:load_schema
(See full trace by running task with --trace)
Migrations are pending. To resolve this issue, run:
bin/rails db:migrate
You have 1 pending migration:
db/migrate/20240514224709_add_age_base_migration.rb
I am not sure why this last line of the schema exists and how it gets there, I think that is the reason for the crash, but maybe it really doesn't know that the migration already ran. This error happens even if I ensure the test db migration ran with:
bin/rails db:migrate RAILS_ENV=test
Can someone help me move forward / explain what I am overlooking or point me to where I can get help or appropriate documentation.
Thanks
Bill
PS - I assume I will need to write this as an Engine / Gem to make it easily work with Rails in a reusable way - but still just working on the structure within a test environment, but If a rails repo would help I can push it to github.
Could you please post your issue at the rails issue tracker? Here is the development of the PostgreSQL C-extension only and your issue is about rails.
OK - will do.