-
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build: geometrylog table geom type to jsonb, int id to uuid (#2063)
* feat: update geom type to jsonb, int id to uuid in geometrylog table * refactor: use gist index over gin for simple geom table --------- Co-authored-by: spwoodcock <[email protected]>
- Loading branch information
1 parent
19a7eed
commit 35939f1
Showing
5 changed files
with
70 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
-- ## Migration to update geom column to jsonb type and int id to uuid. | ||
|
||
-- Start a transaction | ||
|
||
BEGIN; | ||
-- drop existing indexes | ||
DROP INDEX IF EXISTS idx_geometrylog; | ||
|
||
-- Change the 'geom' column to jsonb type | ||
ALTER TABLE geometrylog | ||
ALTER COLUMN geom TYPE jsonb USING geom::jsonb; | ||
|
||
-- Alter the 'id' column to UUID | ||
-- set the default value using gen_random_uuid() | ||
-- First, drop the default if it's currently not a UUID | ||
ALTER TABLE geometrylog | ||
ALTER COLUMN id DROP DEFAULT; | ||
|
||
-- Change the column type to UUID and set the default value | ||
ALTER TABLE geometrylog | ||
ALTER COLUMN id TYPE UUID USING gen_random_uuid(), | ||
ALTER COLUMN id SET DEFAULT gen_random_uuid(); | ||
|
||
CREATE INDEX IF NOT EXISTS idx_geom_gin ON geometrylog USING gist (geom); | ||
|
||
-- Commit the transaction | ||
COMMIT; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/backend/migrations/revert/003-jsonb-geom-geometry-log.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
-- Start a transaction | ||
BEGIN; | ||
|
||
-- Drop the newly created index if needed | ||
DROP INDEX IF EXISTS idx_geom_gin; | ||
|
||
-- Change the 'geom' column back to its original type (geometry) | ||
-- Using ST_GeomFromGeoJSON() to convert jsonb (GeoJSON) back to geometry | ||
ALTER TABLE geometrylog | ||
ALTER COLUMN geom TYPE geometry USING ST_GEOMFROMGEOJSON(geom::text); | ||
|
||
-- This step creates a new temporary 'id_int' column to store the integer IDs | ||
ALTER TABLE geometrylog ADD COLUMN id_int INTEGER; | ||
|
||
WITH numbered_rows AS ( | ||
SELECT | ||
id, | ||
ROW_NUMBER() OVER () AS new_id | ||
FROM geometrylog | ||
) | ||
|
||
UPDATE geometrylog | ||
SET id_int = numbered_rows.new_id | ||
FROM numbered_rows | ||
WHERE geometrylog.id = numbered_rows.id; | ||
|
||
-- Now, drop the old UUID 'id' column | ||
ALTER TABLE geometrylog DROP COLUMN id; | ||
|
||
-- Rename the new 'id_int' column back to 'id' | ||
ALTER TABLE geometrylog RENAME COLUMN id_int TO id; | ||
ALTER TABLE geometrylog ADD PRIMARY KEY (id); | ||
|
||
CREATE INDEX IF NOT EXISTS idx_geometrylog ON geometrylog USING gist (geom); | ||
|
||
-- Commit the transaction | ||
COMMIT; |