This commit is contained in:
@@ -1,2 +1 @@
|
||||
DROP TABLE IF EXISTS entity_types;
|
||||
DROP TABLE IF EXISTS entities;
|
||||
@@ -1,48 +1,20 @@
|
||||
CREATE TABLE IF NOT EXISTS entity_types (
|
||||
id SMALLSERIAL PRIMARY KEY,
|
||||
name TEXT UNIQUE NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS entities (
|
||||
id UUID PRIMARY KEY DEFAULT uuidv7(),
|
||||
type_id SMALLINT REFERENCES entity_types(id),
|
||||
name TEXT NOT NULL,
|
||||
slug TEXT UNIQUE,
|
||||
description TEXT,
|
||||
thumbnail_url TEXT,
|
||||
status SMALLINT DEFAULT 1, -- 1 draft, 2 published
|
||||
is_deleted BOOLEAN NOT NULL DEFAULT false,
|
||||
reviewed_by UUID REFERENCES users(id),
|
||||
reviewed_at TIMESTAMPTZ,
|
||||
created_at TIMESTAMPTZ DEFAULT now(),
|
||||
updated_at TIMESTAMPTZ DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX uniq_entities_slug_active
|
||||
ON entities(slug)
|
||||
WHERE is_deleted = false;
|
||||
|
||||
CREATE INDEX idx_entities_type
|
||||
ON entities(type_id)
|
||||
WHERE is_deleted = false;
|
||||
|
||||
|
||||
CREATE INDEX idx_entities_status_created
|
||||
ON entities(status, created_at DESC)
|
||||
WHERE is_deleted = false;
|
||||
|
||||
|
||||
CREATE INDEX idx_entities_type_status
|
||||
ON entities(type_id, status)
|
||||
WHERE is_deleted = false;
|
||||
|
||||
|
||||
CREATE INDEX idx_entities_reviewed_by
|
||||
ON entities(reviewed_by)
|
||||
WHERE is_deleted = false;
|
||||
|
||||
CREATE INDEX idx_entities_name_search
|
||||
ON entities USING gin (name gin_trgm_ops);
|
||||
ON entities USING GIN (name gin_trgm_ops);
|
||||
|
||||
CREATE INDEX idx_entities_created_active
|
||||
ON entities(created_at DESC)
|
||||
WHERE is_deleted = false;
|
||||
|
||||
CREATE TRIGGER trigger_entities_updated_at
|
||||
BEFORE UPDATE ON entities
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
DROP TABLE IF EXISTS wiki_pages;
|
||||
DROP TABLE IF EXISTS wiki_versions;
|
||||
DROP TABLE IF EXISTS wikis;
|
||||
DROP TABLE IF EXISTS entity_wikis;
|
||||
@@ -1,17 +1,30 @@
|
||||
CREATE EXTENSION IF NOT EXISTS pg_trgm;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS wikis (
|
||||
id UUID PRIMARY KEY DEFAULT uuidv7(),
|
||||
entity_id UUID REFERENCES entities(id) ON DELETE CASCADE,
|
||||
user_id UUID REFERENCES users(id),
|
||||
title TEXT,
|
||||
is_deleted BOOLEAN NOT NULL DEFAULT false,
|
||||
note TEXT,
|
||||
content TEXT,
|
||||
is_deleted BOOLEAN NOT NULL DEFAULT false,
|
||||
created_at TIMESTAMPTZ DEFAULT now(),
|
||||
updated_at TIMESTAMPTZ DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE INDEX idx_wiki_entity
|
||||
ON wikis(entity_id)
|
||||
|
||||
CREATE TABLE IF NOT EXISTS entity_wikis (
|
||||
entity_id UUID REFERENCES entities(id) ON DELETE CASCADE,
|
||||
wiki_id UUID REFERENCES wikis(id) ON DELETE CASCADE,
|
||||
PRIMARY KEY (entity_id, wiki_id)
|
||||
);
|
||||
|
||||
CREATE INDEX idx_entity_wikis_wiki_id
|
||||
ON entity_wikis(wiki_id);
|
||||
|
||||
CREATE INDEX idx_wikis_created_active
|
||||
ON wikis(created_at DESC)
|
||||
WHERE is_deleted = false;
|
||||
|
||||
CREATE INDEX idx_wikis_title_search
|
||||
ON wikis USING GIN (title gin_trgm_ops)
|
||||
WHERE is_deleted = false;
|
||||
|
||||
CREATE TRIGGER trigger_wikis_updated_at
|
||||
|
||||
@@ -1,3 +1,2 @@
|
||||
DROP TABLE IF EXISTS geometries;
|
||||
DROP TABLE IF EXISTS geo_versions;
|
||||
DROP TABLE IF EXISTS entity_geometries;
|
||||
@@ -1,27 +1,22 @@
|
||||
CREATE EXTENSION IF NOT EXISTS btree_gist;
|
||||
CREATE EXTENSION IF NOT EXISTS postgis;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS geometries (
|
||||
id UUID PRIMARY KEY DEFAULT uuidv7(),
|
||||
geom GEOMETRY, -- point / polygon / line
|
||||
geo_type VARCHAR(50) NOT NULL DEFAULT 'id'
|
||||
draw_geometry JSONB NOT NULL,
|
||||
binding JSONB,
|
||||
time_start INT,
|
||||
time_end INT,
|
||||
bbox GEOMETRY,
|
||||
is_deleted BOOLEAN NOT NULL DEFAULT false,
|
||||
bbox GEOMETRY, -- optional
|
||||
created_at TIMESTAMPTZ DEFAULT now(),
|
||||
updated_at TIMESTAMPTZ DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS geo_versions (
|
||||
id UUID PRIMARY KEY DEFAULT uuidv7(),
|
||||
geo_id UUID REFERENCES geometries(id) ON DELETE CASCADE,
|
||||
created_user UUID REFERENCES users(id),
|
||||
geom GEOMETRY,
|
||||
is_deleted BOOLEAN NOT NULL DEFAULT false,
|
||||
note TEXT,
|
||||
reviewed_by UUID REFERENCES users(id),
|
||||
reviewed_at TIMESTAMPTZ,
|
||||
created_at TIMESTAMPTZ DEFAULT now()
|
||||
);
|
||||
ALTER TABLE geometries DROP CONSTRAINT IF EXISTS check_geo_type;
|
||||
ALTER TABLE geometries ADD CONSTRAINT check_geo_type
|
||||
CHECK (geo_type IN ('id', 'name', 'icon', 'variant', 'description'));
|
||||
|
||||
CREATE TABLE IF NOT EXISTS entity_geometries (
|
||||
entity_id UUID REFERENCES entities(id) ON DELETE CASCADE,
|
||||
@@ -29,34 +24,27 @@ CREATE TABLE IF NOT EXISTS entity_geometries (
|
||||
PRIMARY KEY (entity_id, geometry_id)
|
||||
);
|
||||
|
||||
CREATE INDEX idx_geom_spatial_active
|
||||
ON geometries USING GIST (geom)
|
||||
WHERE is_deleted = false;
|
||||
CREATE INDEX idx_geom_draw_geometry
|
||||
ON geometries USING GIN (draw_geometry);
|
||||
|
||||
CREATE INDEX idx_geom_bbox
|
||||
ON geometries USING GIST (bbox)
|
||||
WHERE is_deleted = false;
|
||||
|
||||
CREATE INDEX idx_geom_time_range
|
||||
ON geometries
|
||||
USING GIST (int4range(time_start, time_end))
|
||||
WHERE is_deleted = false;
|
||||
|
||||
CREATE INDEX idx_geo_versions_geo_id
|
||||
ON geo_versions(geo_id)
|
||||
WHERE is_deleted = false;
|
||||
|
||||
CREATE INDEX idx_geo_versions_reviewed_by
|
||||
ON geo_versions(reviewed_by)
|
||||
WHERE is_deleted = false;
|
||||
|
||||
CREATE INDEX idx_geo_versions_created_at
|
||||
ON geo_versions(created_at DESC)
|
||||
ON geometries USING GIST (int4range(time_start, time_end))
|
||||
WHERE is_deleted = false;
|
||||
|
||||
CREATE INDEX idx_entity_geometries_geometry
|
||||
ON entity_geometries(geometry_id);
|
||||
|
||||
CREATE INDEX idx_geom_binding
|
||||
ON geometries USING GIN (binding);
|
||||
|
||||
CREATE INDEX idx_geom_updated_at
|
||||
ON geometries (updated_at DESC)
|
||||
WHERE is_deleted = false;
|
||||
|
||||
CREATE TRIGGER trigger_geometries_updated_at
|
||||
BEFORE UPDATE ON geometries
|
||||
FOR EACH ROW
|
||||
|
||||
40
db/query/entities.sql
Normal file
40
db/query/entities.sql
Normal file
@@ -0,0 +1,40 @@
|
||||
-- name: CreateEntity :one
|
||||
INSERT INTO entities (
|
||||
name, description, thumbnail_url
|
||||
) VALUES (
|
||||
$1, $2, $3
|
||||
)
|
||||
RETURNING *;
|
||||
|
||||
|
||||
-- name: GetEntityById :one
|
||||
SELECT *
|
||||
FROM entities
|
||||
WHERE id = $1 AND is_deleted = false;
|
||||
|
||||
|
||||
-- name: UpdateEntity :one
|
||||
UPDATE entities
|
||||
SET
|
||||
name = COALESCE(sqlc.narg('name'), name),
|
||||
description = COALESCE(sqlc.narg('description'), description),
|
||||
thumbnail_url = COALESCE(sqlc.narg('thumbnail_url'), thumbnail_url)
|
||||
WHERE id = sqlc.arg('id') AND is_deleted = false
|
||||
RETURNING *;
|
||||
|
||||
|
||||
-- name: DeleteEntity :exec
|
||||
UPDATE entities
|
||||
SET
|
||||
is_deleted = true
|
||||
WHERE id = $1;
|
||||
|
||||
|
||||
-- name: SearchEntities :many
|
||||
SELECT *
|
||||
FROM entities
|
||||
WHERE is_deleted = false
|
||||
AND name ILIKE '%' || sqlc.arg('name')::text || '%'
|
||||
AND (sqlc.narg('cursor_id')::uuid IS NULL OR id < sqlc.narg('cursor_id')::uuid)
|
||||
ORDER BY id DESC
|
||||
LIMIT sqlc.arg('limit_count');
|
||||
90
db/query/geometries.sql
Normal file
90
db/query/geometries.sql
Normal file
@@ -0,0 +1,90 @@
|
||||
-- name: CreateGeometry :one
|
||||
INSERT INTO geometries (
|
||||
geo_type, draw_geometry, binding, time_start, time_end, bbox
|
||||
) VALUES (
|
||||
$1, $2, $3, $4, $5, ST_MakeEnvelope(sqlc.arg('min_lng')::float8, sqlc.arg('min_lat')::float8, sqlc.arg('max_lng')::float8, sqlc.arg('max_lat')::float8, 4326)
|
||||
)
|
||||
RETURNING id, geo_type, draw_geometry, binding, time_start, time_end,
|
||||
ST_XMin(bbox)::float8 as min_lng, ST_YMin(bbox)::float8 as min_lat, ST_XMax(bbox)::float8 as max_lng, ST_YMax(bbox)::float8 as max_lat,
|
||||
is_deleted, created_at, updated_at;
|
||||
|
||||
-- name: GetGeometryById :one
|
||||
SELECT id, geo_type, draw_geometry, binding, time_start, time_end,
|
||||
ST_XMin(bbox)::float8 as min_lng, ST_YMin(bbox)::float8 as min_lat, ST_XMax(bbox)::float8 as max_lng, ST_YMax(bbox)::float8 as max_lat,
|
||||
is_deleted, created_at, updated_at
|
||||
FROM geometries
|
||||
WHERE id = $1 AND is_deleted = false;
|
||||
|
||||
-- name: UpdateGeometry :one
|
||||
UPDATE geometries
|
||||
SET
|
||||
geo_type = COALESCE(sqlc.narg('geo_type'), geo_type),
|
||||
draw_geometry = COALESCE(sqlc.narg('draw_geometry'), draw_geometry),
|
||||
binding = COALESCE(sqlc.narg('binding'), binding),
|
||||
time_start = COALESCE(sqlc.narg('time_start'), time_start),
|
||||
time_end = COALESCE(sqlc.narg('time_end'), time_end),
|
||||
bbox = CASE
|
||||
WHEN sqlc.narg('update_bbox')::boolean = true THEN
|
||||
ST_MakeEnvelope(sqlc.narg('min_lng')::float8, sqlc.narg('min_lat')::float8, sqlc.narg('max_lng')::float8, sqlc.narg('max_lat')::float8, 4326)
|
||||
ELSE bbox
|
||||
END,
|
||||
updated_at = now()
|
||||
WHERE id = sqlc.arg('id') AND is_deleted = false
|
||||
RETURNING id, geo_type, draw_geometry, binding, time_start, time_end,
|
||||
ST_XMin(bbox)::float8 as min_lng, ST_YMin(bbox)::float8 as min_lat, ST_XMax(bbox)::float8 as max_lng, ST_YMax(bbox)::float8 as max_lat,
|
||||
is_deleted, created_at, updated_at;
|
||||
|
||||
-- name: DeleteGeometry :exec
|
||||
UPDATE geometries
|
||||
SET
|
||||
is_deleted = true
|
||||
WHERE id = $1;
|
||||
|
||||
-- name: SearchGeometries :many
|
||||
SELECT
|
||||
g.id, g.geo_type, g.draw_geometry, g.binding, g.time_start, g.time_end,
|
||||
ST_XMin(g.bbox)::float8 as min_lng,
|
||||
ST_YMin(g.bbox)::float8 as min_lat,
|
||||
ST_XMax(g.bbox)::float8 as max_lng,
|
||||
ST_YMax(g.bbox)::float8 as max_lat,
|
||||
g.is_deleted, g.created_at, g.updated_at
|
||||
FROM geometries g
|
||||
WHERE g.is_deleted = false
|
||||
AND (
|
||||
sqlc.narg('search_min_lng')::float8 IS NULL OR
|
||||
sqlc.narg('search_min_lat')::float8 IS NULL OR
|
||||
sqlc.narg('search_max_lng')::float8 IS NULL OR
|
||||
sqlc.narg('search_max_lat')::float8 IS NULL OR
|
||||
g.bbox && ST_MakeEnvelope(
|
||||
sqlc.narg('search_min_lng')::float8,
|
||||
sqlc.narg('search_min_lat')::float8,
|
||||
sqlc.narg('search_max_lng')::float8,
|
||||
sqlc.narg('search_max_lat')::float8,
|
||||
4326
|
||||
)
|
||||
)
|
||||
AND (
|
||||
sqlc.narg('time_point')::int IS NULL OR
|
||||
(g.time_start <= sqlc.narg('time_point')::int AND g.time_end >= sqlc.narg('time_point')::int)
|
||||
)
|
||||
AND (
|
||||
sqlc.narg('entity_id')::uuid IS NULL OR
|
||||
EXISTS (
|
||||
SELECT 1
|
||||
FROM entity_geometries eg
|
||||
WHERE eg.geometry_id = g.id
|
||||
AND eg.entity_id = sqlc.narg('entity_id')::uuid
|
||||
)
|
||||
)
|
||||
ORDER BY g.id DESC;
|
||||
|
||||
-- name: BulkDeleteEntityGeometriesByEntityId :many
|
||||
DELETE FROM entity_geometries
|
||||
WHERE entity_id = $1
|
||||
RETURNING geometry_id;
|
||||
|
||||
-- name: CreateEntityGeometries :exec
|
||||
INSERT INTO entity_geometries (
|
||||
entity_id, geometry_id
|
||||
)
|
||||
SELECT $1, unnest(@geometry_ids::uuid[]);
|
||||
56
db/query/wiki.sql
Normal file
56
db/query/wiki.sql
Normal file
@@ -0,0 +1,56 @@
|
||||
-- name: CreateWiki :one
|
||||
INSERT INTO wikis (
|
||||
title, content
|
||||
) VALUES (
|
||||
$1, $2
|
||||
)
|
||||
RETURNING *;
|
||||
|
||||
-- name: GetWikiById :one
|
||||
SELECT *
|
||||
FROM wikis
|
||||
WHERE id = $1 AND is_deleted = false;
|
||||
|
||||
-- name: UpdateWiki :one
|
||||
UPDATE wikis
|
||||
SET
|
||||
title = COALESCE(sqlc.narg('title'), title),
|
||||
content = COALESCE(sqlc.narg('content'), content)
|
||||
WHERE id = sqlc.arg('id') AND is_deleted = false
|
||||
RETURNING *;
|
||||
|
||||
-- name: DeleteWiki :exec
|
||||
UPDATE wikis
|
||||
SET
|
||||
is_deleted = true
|
||||
WHERE id = $1;
|
||||
|
||||
-- name: SearchWikis :many
|
||||
SELECT w.*
|
||||
FROM wikis w
|
||||
WHERE w.is_deleted = false
|
||||
AND w.title ILIKE '%' || sqlc.arg('title')::text || '%'
|
||||
AND (
|
||||
sqlc.narg('entity_id')::uuid IS NULL OR
|
||||
EXISTS (
|
||||
SELECT 1
|
||||
FROM entity_wikis ew
|
||||
WHERE ew.wiki_id = w.id
|
||||
AND ew.entity_id = sqlc.narg('entity_id')::uuid
|
||||
)
|
||||
)
|
||||
AND (sqlc.narg('cursor_id')::uuid IS NULL OR w.id < sqlc.narg('cursor_id')::uuid)
|
||||
|
||||
ORDER BY w.id DESC
|
||||
LIMIT sqlc.arg('limit_count');
|
||||
|
||||
-- name: BulkDeleteEntityWikisByEntityId :many
|
||||
DELETE FROM entity_wikis
|
||||
WHERE entity_id = $1
|
||||
RETURNING wiki_id;
|
||||
|
||||
-- name: CreateEntityWikis :exec
|
||||
INSERT INTO entity_wikis (
|
||||
entity_id, wiki_id
|
||||
)
|
||||
SELECT $1, unnest(@wiki_ids::uuid[]);
|
||||
@@ -68,4 +68,49 @@ CREATE TABLE IF NOT EXISTS verification_medias (
|
||||
verification_id UUID REFERENCES user_verifications(id) ON DELETE CASCADE,
|
||||
media_id UUID REFERENCES medias(id) ON DELETE CASCADE,
|
||||
PRIMARY KEY (verification_id, media_id)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS entities (
|
||||
id UUID PRIMARY KEY DEFAULT uuidv7(),
|
||||
name TEXT NOT NULL,
|
||||
description TEXT,
|
||||
thumbnail_url TEXT,
|
||||
is_deleted BOOLEAN NOT NULL DEFAULT false,
|
||||
created_at TIMESTAMPTZ DEFAULT now(),
|
||||
updated_at TIMESTAMPTZ DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS wikis (
|
||||
id UUID PRIMARY KEY DEFAULT uuidv7(),
|
||||
title TEXT,
|
||||
content TEXT,
|
||||
is_deleted BOOLEAN NOT NULL DEFAULT false,
|
||||
created_at TIMESTAMPTZ DEFAULT now(),
|
||||
updated_at TIMESTAMPTZ DEFAULT now()
|
||||
);
|
||||
|
||||
|
||||
CREATE TABLE IF NOT EXISTS entity_wikis (
|
||||
entity_id UUID REFERENCES entities(id) ON DELETE CASCADE,
|
||||
wiki_id UUID REFERENCES wikis(id) ON DELETE CASCADE,
|
||||
PRIMARY KEY (entity_id, wiki_id)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS geometries (
|
||||
id UUID PRIMARY KEY DEFAULT uuidv7(),
|
||||
geo_type VARCHAR(50) NOT NULL DEFAULT 'id',
|
||||
draw_geometry JSONB NOT NULL,
|
||||
binding JSONB,
|
||||
time_start INT,
|
||||
time_end INT,
|
||||
bbox GEOMETRY,
|
||||
is_deleted BOOLEAN NOT NULL DEFAULT false,
|
||||
created_at TIMESTAMPTZ DEFAULT now(),
|
||||
updated_at TIMESTAMPTZ DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS entity_geometries (
|
||||
entity_id UUID REFERENCES entities(id) ON DELETE CASCADE,
|
||||
geometry_id UUID REFERENCES geometries(id) ON DELETE CASCADE,
|
||||
PRIMARY KEY (entity_id, geometry_id)
|
||||
);
|
||||
Reference in New Issue
Block a user