drizzle-team / drizzle-orm

Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too 😅
https://orm.drizzle.team
Apache License 2.0
24.05k stars 609 forks source link

[BUG] PostGIS polygon geometry is changed to Point geometry when migration #3040

Open JinIgarashi opened 3 weeks ago

JinIgarashi commented 3 weeks ago

What version of drizzle-orm are you using?

0.33.0

What version of drizzle-kit are you using?

0.24.2

Describe the Bug

I have a table in the database like the below

CREATE TABLE IF NOT EXISTS geohub.dataset
(
    id character varying COLLATE pg_catalog."default" NOT NULL,
    url character varying COLLATE pg_catalog."default" NOT NULL,
    is_raster boolean NOT NULL,
    license character varying COLLATE pg_catalog."default",
    bounds geometry(Polygon,4326) NOT NULL,
    createdat timestamp with time zone NOT NULL,
    updatedat timestamp with time zone,
    name character varying COLLATE pg_catalog."default",
    description character varying COLLATE pg_catalog."default",
    created_user character varying(100) COLLATE pg_catalog."default" NOT NULL,
    updated_user character varying(100) COLLATE pg_catalog."default",
    access_level integer NOT NULL DEFAULT 3,
    CONSTRAINT dataset_pkey PRIMARY KEY (id)
)

When I used introspect, introspect correctly generated initial SQL like below

CREATE TABLE IF NOT EXISTS "geohub"."dataset" (
    "id" varchar PRIMARY KEY NOT NULL,
    "url" varchar NOT NULL,
    "is_raster" boolean NOT NULL,
    "license" varchar,
    "bounds" geometry(Polygon,4326) NOT NULL, # introspect correctly generated current schema
    "createdat" timestamp with time zone NOT NULL,
    "updatedat" timestamp with time zone,
    "name" varchar,
    "description" varchar,
    "created_user" varchar(100) NOT NULL,
    "updated_user" varchar(100),
    "access_level" integer DEFAULT 3 NOT NULL
);

and, the below is schema.ts generated by introspect. This has no issue.

export const datasetInGeohub = geohub.table(
    'dataset',
    {
        id: varchar('id').primaryKey().notNull(),
        url: varchar('url').notNull(),
        isRaster: boolean('is_raster').notNull(),
        license: varchar('license'),
        bounds: geometry('bounds', { type: 'polygon', srid: 4326 }).notNull(),
        createdat: timestamp('createdat', { withTimezone: true, mode: 'string' }).notNull(),
        updatedat: timestamp('updatedat', { withTimezone: true, mode: 'string' }),
        name: varchar('name'),
        description: varchar('description'),
        createdUser: varchar('created_user', { length: 100 }).notNull(),
        updatedUser: varchar('updated_user', { length: 100 }),
        accessLevel: integer('access_level').default(3).notNull()
    },
    (table) => {
        return {
            boundsGeomIdx: index('dataset_bounds_geom_idx').using('gist', table.bounds.asc().nullsLast())
        };
    }
);

However, when I used pnpm drizzle-kit:generate, it created the wrong SQL like below. Then when I push migrations to my local database, drizzle create the table with point geometry without specified srid 4326 even schema.ts is configured correctly.

CREATE TABLE IF NOT EXISTS "geohub"."dataset" (
    "id" varchar PRIMARY KEY NOT NULL,
    "url" varchar NOT NULL,
    "is_raster" boolean NOT NULL,
    "license" varchar,
    "bounds" geometry(point) NOT NULL, # changed polygon to point, and dropped srid
    "createdat" timestamp with time zone NOT NULL,
    "updatedat" timestamp with time zone,
    "name" varchar,
    "description" varchar,
    "created_user" varchar(100) NOT NULL,
    "updated_user" varchar(100),
    "access_level" integer DEFAULT 3 NOT NULL
);

Expected behavior

migration should correctly create Polygon geometry SQL with srid as introspect does.

Environment & setup

I am using drizzle in svelte-kit

drizzle.config.ts is as follow

import { defineConfig } from 'drizzle-kit';

export default defineConfig({
    schema: './src/lib/server/schema.ts',
    out: './drizzle_tmp',
    dialect: 'postgresql',
    dbCredentials: {
        url: process.env.DATABASE_CONNECTION!
    },
    schemaFilter: ['geohub'],
    extensionsFilters: ['postgis']
});

my working branch is at https://github.com/UNDP-Data/geohub/pull/4142

jbccollins commented 3 days ago

This happens to multipolygons too.