-
Notifications
You must be signed in to change notification settings - Fork 21.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PostgreSQL Apache AGE Rails (GraphDB driver questions / problems) #51843
Comments
I have found a workaround - if I rewrite the ActiveRecord::Schema[7.1].define(version: 2024_05_05_183043) do
execute('CREATE EXTENSION IF NOT EXISTS age;')
execute <<-SQL
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1
FROM pg_namespace
WHERE nspname = 'ag_catalog'
) THEN
CREATE SCHEMA ag_catalog;
END IF;
END $$;
SQL
# These are extensions that must be enabled in order to support this database
enable_extension 'age'
enable_extension 'plpgsql'
# Load the age code
execute("LOAD 'age';")
# Load the ag_catalog into the search path
execute('SET search_path = ag_catalog, "$user", public;')
execute <<-SQL
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1
FROM pg_constraint
WHERE conname = 'fk_graph_oid'
) THEN
ALTER TABLE ag_label ADD CONSTRAINT fk_graph_oid FOREIGN KEY (graph) REFERENCES ag_graph (graphid);
END IF;
END $$;
SQL
# create_schema 'age_schema'
execute <<-SQL
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1
FROM ag_catalog.ag_graph
WHERE name = 'age_schema'
) THEN
PERFORM create_graph('age_schema');
END IF;
END $$;
SQL
end instead of - which indeed different than the migration (& incorrect): ActiveRecord::Schema[7.1].define(version: 2024_05_05_183043) 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"
add_foreign_key "ag_label", "ag_graph", column: "graph", primary_key: "graphid", name: "fk_graph_oid"
end unfortunately, I need to reset the |
Can you try with structure.sql instead of schema.rb? https://edgeguides.rubyonrails.org/active_record_migrations.html#schema-dumping-and-you |
@btihen thanks for working on this, I'd also be interested in exploring AGE for graph support. You probably already saw this, but some ideas if name collision hasn't already been ruled out: DBMS-level name collision?You may have ruled this out already, but I wonder if Potential fixesSeparate graph namespaces for dev and testYou might be able to find examples among gems that have implemented support for PG extensions like activerecord-postgis-adapter and see how the situations where a common extension catalog and schema can handle this. Option 1 - Isolate by DB, nothing shared at DBMS (preferred)
Option 2 - Isolate by graph name
Note this should be avoided if possible, I'd only consider it if AGE does not support DB-level isolation Single graph namespace
Option 1 - Add check for existing AGE graph using AGE functions
Option 2 - Add check for existing AGE graph using Postgres DDL queries
|
@simi
I will need to experiment a bit more. I am using a dockerized version of the DB with AGE (Postgres v16) but my psql version is postgresql v14). At the moment i can't change this setup n this computer - i'll see if i can get another setup going. ActiveRecord::Schema[7.1].define(version: 20_240_505_183_043) do
# 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;')
# Create age_schema graph if it doesn't exist
execute("SELECT create_graph('age_schema');")
end in the meantime I found I can just copy the migration into the schema.rb and just reset it with careful commits. |
@mashallah - Hi I'd be glad to collaborate. I did see the name collision, but I've been ignoring them since I discoved the problem was that the schema is incorrectly generated - when i copy the migration into the schema - \i can run tests and there is no name collision - the schema just needs to be correct. I have some basic working rails code at: https://github.com/btihen-dev/rails_graphdb_age_app (with tests running) PS - code is still evolving, only handles ons schema, and doesn't work with all data types supported by AGE & I havn;t yet tested unique restrictions. |
Please use https://discuss.rubyonrails.org/ for questions/help, where a wider community will be able to help you. We reserve the issues tracker for issues only. |
Steps to reproduce
I am writing 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:
I have a setup migration with:
all goes well when I do:
and I get the schema:
when I look in the database AGE Extension (only it creates the following schemas and tables - in case that helps):
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.
mini example - create a simple Node with:
Usage:
All works well in development mode.
Expected behavior
When I run tests the migrations that have already run be detected in the test ENV and the tests should run. Without the Apache AGE extension - everything works as expected. I can run migrations, and tests and it detects the migrations ran.
Actual behavior
When I want to run tests migrations I detects they have already run (I can also run in development mode as often as I want and the migrations are detected and all works well), but when I run tests - even using
RAILS_ENV=test
, it no longer detects the migrations and I get migration errors: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:
Can someone help me move forward / explain what I am overlooking or point me to where I can get help or appropriate documentation.
I read with each DB connection (like when I use psql I need first run)
I don't know how to, but perhaps I need to do something like that first with the PG connection and then load the tests. Might that help?
System configuration
Rails version: Rails 7.1.3.2
Ruby version: 3.3.1
The text was updated successfully, but these errors were encountered: