Skip to content

Updated dependencies and fixed compactibilty issues with nest js, also fixing Spatial Indexing - #159

Open
relativity-codes wants to merge 2 commits into
cockroachdb:masterfrom
relativity-codes:master
Open

relativity-codes wants to merge 2 commits into
cockroachdb:masterfrom
relativity-codes:master

Conversation

@relativity-codes

Copy link
Copy Markdown

Here is a comprehensive summary of all the fixes and improvements implemented in the sequelize-cockroachdb:

1. Symlink and Require Interception (setTypeParser crash)

  • Problem: When developing/symlinking the package locally, multiple conflicting instances of Sequelize and pg were loaded, resulting in a Cannot read properties of undefined (reading 'setTypeParser') crash.
  • Fix: Added a global require interceptor hook at the top of source/index.js to ensure the same sequelize instance is shared from the parent application context, and added a robust fallback to require 'pg' directly if pg.types is missing.

2. Dependency Upgrades

  • Fix: Upgraded peer dependencies and dev dependencies in package.json to match the host application's versions (sequelize@^6.37.8 and pg@^8.21.0), ensuring complete compatibility with the latest version 6 of Sequelize.

3. Dynamic Database Engine Auto-Detection

  • Problem: Static configuration assumed the target database was always CockroachDB, breaking compatibility with standard PostgreSQL when running local development or test suites.
  • Fix: Implemented connection-time database type detection using SELECT version() AS version to dynamically check if the host contains CockroachDB. Standard PostgreSQL connection properties (like EXCEPTION support and lock modes) are now dynamically restored when not connected to a CockroachDB engine.

4. Enum Creation Patch (pgEnum)

  • Problem: ENUM creation queries crashed on CockroachDB due to unsupported standard Postgres DO ... EXCEPTION WHEN duplicate_object syntax.
  • Fix: Patched the query generator pgEnum method to dynamically output CREATE TYPE IF NOT EXISTS when connected to CockroachDB, while preserving the original DO ... EXCEPTION block when connected to PostgreSQL.

5. Spatial Index Mapping (addIndexQuery)

  • Problem: Defining indexes with type: 'SPATIAL' on spatial (GEOMETRY/GEOGRAPHY) columns generated default B-tree index queries which are rejected by both CockroachDB and PostgreSQL.
  • Fix: Patched QueryGenerator.prototype.addIndexQuery to intercept index definitions using type: 'SPATIAL' (or type: 'spatial') and automatically map them to using: 'gist' (GIST index) while omitting the unsupported type parameter.

6. Test Suite Environment Configuration

  • Problem: Test connection instances in the package's test suite hardcoded localhost credentials, meaning that running npm test completely ignored database environment variables.
  • Fix: Updated all connection helpers across the package's test suite files (tests/support.js, tests/sequelize_query_test.js, tests/dialects_query_test.js, and tests/belongs_to_many_test.js) to dynamically respect and map database environment variables (DB_NAME, DB_USER, DB_PASS, DB_HOST, DB_PORT, DB_SSL, CA_CERT_PATH), making it fully compatible with custom test databases (such as CockroachDB Cloud) while preserving default fallbacks for GitHub CI workflow.

rafiss added a commit that referenced this pull request Sep 29, 2026
Newer versions of Sequelize v6 create ENUM types inside a
`DO ... EXCEPTION WHEN duplicate_object` block, so that creating a type
that already exists is not an error. CockroachDB does not support
CREATE TYPE inside a function body, so any model with an ENUM column
failed to sync with:

    unimplemented: CREATE TYPE usage inside a function definition is
    not supported

This overrides the query generator's pgEnum to use
CREATE TYPE IF NOT EXISTS instead, which has the same effect.

This was originally proposed in
#159.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
rafiss added a commit that referenced this pull request Sep 30, 2026
Newer versions of Sequelize v6 create ENUM types inside a
`DO ... EXCEPTION WHEN duplicate_object` block, so that creating a type
that already exists is not an error. CockroachDB does not support
CREATE TYPE inside a function body, so any model with an ENUM column
failed to sync with:

    unimplemented: CREATE TYPE usage inside a function definition is
    not supported

This overrides the query generator's pgEnum to use
CREATE TYPE IF NOT EXISTS instead, which has the same effect.

This was originally proposed in
#159.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@rafiss

rafiss commented Sep 30, 2026

Copy link
Copy Markdown
Contributor

Thanks for putting this together! We pulled the ENUM fix into #162, which is now merged. With recent Sequelize 6 releases, ENUM types were created inside a DO ... EXCEPTION block, which CockroachDB doesn't support, so the adapter now uses CREATE TYPE IF NOT EXISTS instead. That PR also updated CI to test against CockroachDB v24.3 through v26.2 on Node 22.

I lean against taking the other changes, mostly because they could be hard to maintain or surprising for people using the adapter:

  • Intercepting require changes Module.prototype.require for the whole process, which feels like more than a library should do.
  • Detecting PostgreSQL at connect time adds a query to every new connection, and it changes dialect feature flags that every Sequelize instance in the process shares. Since this package is meant for CockroachDB, I think PostgreSQL users are better served by plain Sequelize.
  • For spatial indexes, Sequelize drops type: 'SPATIAL' for Postgres and creates a regular index. CockroachDB then rejects that with column geom has type geometry, which is not indexable in a non-inverted index and a hint to create an inverted index instead. Since the error points people in the right direction and using: 'gist' already works on CockroachDB, I'd rather not remap type: 'SPATIAL'.
  • Sequelize itself relies on pg.types, so if it's missing, I suspect a custom dialectModule is misconfigured, and falling back to a different pg module might hide that.

If you still see the setTypeParser crash when linking the package locally, it'd be great if you could open an issue with steps to reproduce, and we can take a closer look.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants