Skip to content

Commit

Permalink
END-013 - Expose is_nullable on Endo.Column.t()
Browse files Browse the repository at this point in the history
  • Loading branch information
vereis committed Nov 8, 2024
1 parent 47d74f0 commit 2c8fd2f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
1 change: 1 addition & 0 deletions lib/endo/adapters/postgres.ex
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ defmodule Endo.Adapters.Postgres do
position: column.ordinal_position,
default_value: column.column_default,
type: column.udt_name,
is_nullable: column.is_nullable == :yes,
type_metadata: Type.Metadata.derive!(column)
}
end
Expand Down
1 change: 1 addition & 0 deletions lib/endo/column.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ defmodule Endo.Column do
:name,
:type,
:position,
:is_nullable,
:type_metadata,
:default_value,
:table_name,
Expand Down
6 changes: 3 additions & 3 deletions priv/repo/migrations/20221221123643_bootstrap_testing_env.exs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ defmodule Test.Postgres.Repo.Migrations.BootstrapTestingEnv do

defp create_accounts do
create table(:accounts) do
add(:username, :string, null: false)
add(:email, :string, null: false)
add(:username, :string, null: true)
add(:email, :string, null: true)

timestamps()
end
Expand Down Expand Up @@ -63,7 +63,7 @@ defmodule Test.Postgres.Repo.Migrations.BootstrapTestingEnv do

create table(:accounts_orgs, primary_key: false) do
add(:account_id, references(:accounts), null: false)
add(:org_id, references(:orgs), null: false)
add(:org_id, references(:orgs), null: true)

timestamps()
end
Expand Down
12 changes: 12 additions & 0 deletions test/endo_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,18 @@ defmodule EndoTest do
assert %Endo.Column{indexes: []} = Enum.find(repos.columns, &(&1.name == "some_interval"))
end

test "given a single table, returns whether columns are nullable" do
accounts_orgs = Endo.get_table(Test.Postgres.Repo, "accounts_orgs")

assert length(accounts_orgs.columns) == 2

org_id = Enum.find(accounts_orgs.columns, &(&1.name == "org_id"))
account_id = Enum.find(accounts_orgs.columns, &(&1.name == "account_id"))

assert org_id.is_nullable
refute account_id.is_nullable
end

test "given a single table, fetches all indexes for each column" do
repos = Endo.get_table(Test.Postgres.Repo, "repos")

Expand Down

0 comments on commit 2c8fd2f

Please sign in to comment.