Comments (7)
I have found a workaround - if I rewrite the schema.rb
to:
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 schema.rb
after each new migration. Is there a solution to control the schema.rb
generation and make it compatible with ApaageAGE?
from rails.
Can you try with structure.sql instead of schema.rb? https://edgeguides.rubyonrails.org/active_record_migrations.html#schema-dumping-and-you
from rails.
@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:
PG::DuplicateObject: ERROR: constraint "fk_graph_oid" for relation "ag_label" already exists (PG::DuplicateObject)
.
DBMS-level name collision?
You may have ruled this out already, but I wonder if create_graph('age_schema')
is running twice, once successfully in dev migrations, then again in test, but failing since it has the same age_schema
namespace, and that namespace is being shared across the dev and test databases.
Potential fixes
Separate graph namespaces for dev and test
You 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)
- If AGE allows complete isolation where the graph and extension are unique to the dev or test DB, rather than sharing some elements like the graph name at the DBMS level, this might be easiest. In this approach, after migration there's an
age_schema
graph in your dev db and completely independentage_schema
graph in your dev db. IfAGE
supports DB-level isolation of graphs like this instead of DBMS-level, this might be your best option.
Option 2 - Isolate by graph name
- Modify
execute("SELECT create_graph('age_schema');")
to avoid dev/test namespace collision for the nameage_schema
, the end goal would be that when run in test mode the name of age_schema would be changing theage_schema_test
. So you'd haveage_schema
andage_schema_test
after running migrations.
Note this should be avoided if possible, I'd only consider it if AGE does not support DB-level isolation
Single graph namespace
- Goal: modify
execute("SELECT create_graph('age_schema');")
by adding the equivalent of anIF NOT EXISTS
statement.
Option 1 - Add check for existing AGE graph using AGE functions
- Since
create_graph
is a function, you may want to make use of theAGE
graph_exists()
orget_graphnames()
functions, integrating them into the equivalent of anIF NOT EXISTS
check in the graph creation query above.
Option 2 - Add check for existing AGE graph using Postgres DDL queries
- This would require running the
create_graph
function, determining the underlying structures you want to look for that are often created bycreate_graph
then query to determine if they are present.
from rails.
@simi
Can you try with structure.sql instead of schema.rb?
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.
from rails.
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:
PG::DuplicateObject: ERROR: constraint "fk_graph_oid" for relation "ag_label" already exists (PG::DuplicateObject).
@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.
from rails.
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.
from rails.
@mashallah - I have a fix for the text / migration problem bin/rails apache_age:migrate
- see: https://github.com/marpori/rails_age
There are now quite a few generators to make configuration, generation, etc easy - like rails.
Feel free to contribute.
from rails.
Related Issues (20)
- Rails server --pid option does not seem to work as expected. Also no documentation found. HOT 2
- `ActiveRecord::NotNullViolation` for Missing Enum Default in Rails 7 HOT 3
- No connection pool for 'ActiveRecord::Base' found for the 'reading' role. #275 HOT 4
- trix with rails 7.0 giving TypeError HOT 2
- in guides.rubyonrails documentation of threads: missing info about db access HOT 1
- HasMany/HasOne associations: combining `:through` and a scope including a `.merge` leads to missing table HOT 1
- [Question] Why doesn't set_pk_sequence! support the parameter for is_called? HOT 5
- Rails 6.0.6.1 transaction rollback doesn't reload object HOT 1
- Error `undefined method `start_with?' for nil` when logging
- new_framework_defaults initializer for Rails 7.1 not setting ActiveRecord.run_after_transaction_callbacks_in_order_defined
- [Feature Request][CSP] Support for multiple Content Security Policies HOT 2
- ActionCable: Invalid channel causes repeated subscription attempts
- Code generation memory bloat post alias fix HOT 8
- Rails throws an error when sending invalid UTF-8 query param HOT 1
- Lazy default_scope evaluation in joined models loses original context
- Some Rails integration tests break with rack 3.1 HOT 4
- ActionText Editor Previews uploaded pre 7.1.3.4 are broken in 7.1.3.4 after a resave HOT 10
- Inversion of associations with :through don't work until it's saved
- Incorrect `has_many through` load with custom association name and custom keys type
- Demo video on home page doesn't work as demo'd HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rails.