Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ erl_crash.dump
*.beam
/config/*.secret.exs
.elixir_ls/
.idea
8 changes: 0 additions & 8 deletions .idea/.gitignore

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/copilot.data.migration.agent.xml

This file was deleted.

9 changes: 0 additions & 9 deletions .idea/guided.iml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/inspectionProfiles/Project_Default.xml

This file was deleted.

8 changes: 0 additions & 8 deletions .idea/modules.xml

This file was deleted.

7 changes: 0 additions & 7 deletions .idea/vcs.xml

This file was deleted.

33 changes: 25 additions & 8 deletions guided/config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,32 @@ config :bcrypt_elixir, :log_rounds, 1

# Configure your database
#
# The MIX_TEST_PARTITION environment variable can be used
# to provide built-in test partitioning in CI environment.
# Run `mix help test` for more information.
# The Dockerized Postgres instance already has the AGE extension installed.
# Reuse that database by default so graph queries keep working in tests.
db_username =
System.get_env("TEST_DATABASE_USERNAME") || System.get_env("DATABASE_USERNAME") || "postgres"

db_password =
System.get_env("TEST_DATABASE_PASSWORD") || System.get_env("DATABASE_PASSWORD") || "guided"

db_hostname =
System.get_env("TEST_DATABASE_HOSTNAME") || System.get_env("DATABASE_HOSTNAME") || "localhost"

db_port = System.get_env("TEST_DATABASE_PORT") || System.get_env("DATABASE_PORT") || "5455"
db_name = System.get_env("TEST_DATABASE_NAME") || System.get_env("DATABASE_NAME") || "guided"

db_name =
case System.get_env("MIX_TEST_PARTITION") do
nil -> db_name
partition -> "#{db_name}#{partition}"
end

config :guided, Guided.Repo,
username: "postgres",
password: "guided",
hostname: "localhost",
port: 5455,
database: "guided_test#{System.get_env("MIX_TEST_PARTITION")}",
username: db_username,
password: db_password,
hostname: db_hostname,
port: String.to_integer(db_port),
database: db_name,
pool: Ecto.Adapters.SQL.Sandbox,
pool_size: System.schedulers_online() * 2

Expand Down
19 changes: 12 additions & 7 deletions guided/lib/guided/graph.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,15 @@ defmodule Guided.Graph do
{:ok, %Postgrex.Result{rows: [...]}}
"""
def cypher(cypher_query, params \\ []) do
# Load the AGE extension (must be separate query)
with {:ok, _} <- query(Repo, "LOAD 'age'", [], []),
{:ok, _} <- query(Repo, "SET search_path = ag_catalog, \"$user\", public", [], []) do
# Run all commands in a transaction to ensure they use the same connection
Repo.transaction(fn ->
# Load the AGE extension and set search path
{:ok, _} = query(Repo, "LOAD 'age'", [], [])
{:ok, _} = query(Repo, "SET search_path = ag_catalog, \"$user\", public", [], [])

# Build the Cypher query using AGE's cypher function
# Convert agtype to text which Postgrex can handle
if params == [] do
result = if params == [] do
sql_query = """
SELECT ag_catalog.agtype_to_text(result) as result
FROM ag_catalog.cypher('#{@graph_name}', $$#{cypher_query}$$) as (result ag_catalog.agtype)
Expand All @@ -55,9 +58,11 @@ defmodule Guided.Graph do
query(Repo, sql_query, [@graph_name, cypher_query, params_json], [])
end

else
{:error, error} -> {:error, error}
end
case result do
{:ok, data} -> data
{:error, error} -> Repo.rollback(error)
end
end)
end

@doc """
Expand Down
Loading
Loading