Skip to content

Commit

Permalink
Repo.delete! with string (uuid) (issue #57)
Browse files Browse the repository at this point in the history
  • Loading branch information
wookay committed Sep 7, 2023
1 parent 985b88b commit 29321f9
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 17 deletions.
37 changes: 20 additions & 17 deletions src/Repo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ using ..Octo: Raw, SQLKeyword
using ..Schema # Schema.validates
using ..Pretty: show

const PrimaryKeyType = Union{Int, String}
const ExecuteResult = Union{Nothing, NamedTuple}

struct NeedsConnectError <: Exception
msg
end
Expand All @@ -23,7 +26,6 @@ struct Connection
conn
end

const ExecuteResult = Union{Nothing, NamedTuple}

@enum RepoLogLevel::Int32 begin
LogLevelDebugSQL = -1
Expand Down Expand Up @@ -193,39 +195,40 @@ function query(stmt::Structured, vals::Vector; db::Connection=current_connection
Base.invokelatest(db.loader.query, db.conn, prepared, vals)
end


### Repo.query - pk
"""
Repo.query(M::Type, pk::Union{Int, String}; db::Connection=current_connection())
Repo.query(M::Type, pk::PrimaryKeyType; db::Connection=current_connection())
"""
function query(M::Type, pk::Union{Int, String}; db::Connection=current_connection()) # throw Schema.PrimaryKeyError
function query(M::Type, pk::PrimaryKeyType; db::Connection=current_connection()) # throw Schema.PrimaryKeyError
a = db.adapter
table = a.from(M)
key = _field_for_primary_key(db, M)
query([a.SELECT * a.FROM table a.WHERE key == pk]; db=db)
end

"""
Repo.query(from::FromItem, pk::Union{Int, String}; db::Connection=current_connection())
Repo.query(from::FromItem, pk::PrimaryKeyType; db::Connection=current_connection())
"""
function query(from::FromItem, pk::Union{Int, String}; db::Connection=current_connection()) # throw Schema.PrimaryKeyError
function query(from::FromItem, pk::PrimaryKeyType; db::Connection=current_connection()) # throw Schema.PrimaryKeyError
query(from.__octo_model, pk; db=db)
end

### Repo.query - pk_range
"""
Repo.query(M::Type, pk_range::UnitRange{Int64}; db::Connection=current_connection())
Repo.query(M::Type, pk_range::UnitRange{Int}; db::Connection=current_connection())
"""
function query(M::Type, pk_range::UnitRange{Int64}; db::Connection=current_connection()) # throw Schema.PrimaryKeyError
function query(M::Type, pk_range::UnitRange{Int}; db::Connection=current_connection()) # throw Schema.PrimaryKeyError
a = db.adapter
table = a.from(M)
key = _field_for_primary_key(db, M)
query([a.SELECT * a.FROM table a.WHERE key a.BETWEEN pk_range.start a.AND pk_range.stop]; db=db)
end

"""
Repo.query(from::FromItem, pk_range::UnitRange{Int64}; db::Connection=current_connection())
Repo.query(from::FromItem, pk_range::UnitRange{Int}; db::Connection=current_connection())
"""
function query(from::FromItem, pk_range::UnitRange{Int64}; db::Connection=current_connection()) # throw Schema.PrimaryKeyError
function query(from::FromItem, pk_range::UnitRange{Int}; db::Connection=current_connection()) # throw Schema.PrimaryKeyError
query(from.__octo_model, pk_range; db=db)
end

Expand Down Expand Up @@ -283,16 +286,16 @@ end

# Repo.get
"""
Repo.get(M::Type, pk::Union{Int, String}; db::Connection=current_connection())
Repo.get(M::Type, pk::PrimaryKeyType; db::Connection=current_connection())
"""
function get(M::Type, pk::Union{Int, String}; db::Connection=current_connection()) # throw Schema.PrimaryKeyError
function get(M::Type, pk::PrimaryKeyType; db::Connection=current_connection()) # throw Schema.PrimaryKeyError
query(M, pk; db=db)
end

"""
Repo.get(M::Type, pk_range::UnitRange{Int64}; db::Connection=current_connection())
Repo.get(M::Type, pk_range::UnitRange{Int}; db::Connection=current_connection())
"""
function get(M::Type, pk_range::UnitRange{Int64}; db::Connection=current_connection()) # throw Schema.PrimaryKeyError
function get(M::Type, pk_range::UnitRange{Int}; db::Connection=current_connection()) # throw Schema.PrimaryKeyError
query(M, pk_range; db=db)
end

Expand Down Expand Up @@ -469,16 +472,16 @@ function delete!(M::Type, pk_range::UnitRange{Int}; db::Connection=current_conne
end

"""
Repo.delete!(M::Type, pk_tuple::(Tuple{Vararg{Int, N}} where N); db::Connection=current_connection())
Repo.delete!(M::Type, pk_tuple::(Tuple{Vararg{PrimaryKeyType, N}} where N); db::Connection=current_connection())
"""
function delete!(M::Type, pk_tuple::(Tuple{Vararg{Int, N}} where N); db::Connection=current_connection()) # throw Schema.PrimaryKeyError
function delete!(M::Type, pk_tuple::(Tuple{Vararg{PrimaryKeyType, N}} where N); db::Connection=current_connection()) # throw Schema.PrimaryKeyError
delete!(M, collect(pk_tuple); db)
end

"""
Repo.delete!(M::Type, pk_vector::Vector{Int}; db::Connection=current_connection())
Repo.delete!(M::Type, pk_vector::Vector{<:PrimaryKeyType}; db::Connection=current_connection())
"""
function delete!(M::Type, pk_vector::Vector{Int}; db::Connection=current_connection()) # throw Schema.PrimaryKeyError
function delete!(M::Type, pk_vector::Vector{<:PrimaryKeyType}; db::Connection=current_connection()) # throw Schema.PrimaryKeyError
a = db.adapter
table = a.from(M)
key = _field_for_primary_key(db, M)
Expand Down
52 changes: 52 additions & 0 deletions test/adapters/postgresql/repo_uuid_test.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
module adapters_postgresql_repo_uuid_test

using Test # @test
using Octo.Adapters.PostgreSQL # Repo Schema Raw

Repo.debug_sql()

include("options.jl")

Repo.connect(;
adapter = Octo.Adapters.PostgreSQL,
Options.for_postgresql...
)

struct PinkFloyd
end
Schema.model(PinkFloyd, table_name="PINK_FLOYD")

Repo.execute([DROP TABLE IF EXISTS PinkFloyd])

# https://arctype.com/blog/postgres-uuid/
Repo.execute(Raw("""
CREATE TABLE PINK_FLOYD (
id uuid DEFAULT uuid_generate_v4(),
album_name VARCHAR NOT NULL,
PRIMARY KEY (id)
)
"""))

Repo.insert!(PinkFloyd, [
(album_name="The Wall",),
(album_name="The Dark Side of the Moon",),
(album_name="Wish You Were Here",),
(album_name="The Division Bell",),
(album_name="Pulse",),
(album_name="Meddle",),
(album_name="Atom Heart Mother",),
(album_name="The Final Cut",),
])

df = Repo.query(PinkFloyd)
@test size(df) == (8,)

pk_ids = map(x -> x.id, df[1:2])
Repo.delete!(PinkFloyd, pk_ids)

df = Repo.query(PinkFloyd)
@test size(df) == (6,)

Repo.disconnect()

end # module adapters_postgresql_repo_uuid_test

0 comments on commit 29321f9

Please sign in to comment.