Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Cockroachdb support without transactions #16394

Open
wants to merge 124 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
124 commits
Select commit Hold shift + click to select a range
458bf3a
initial commit
divye11 Dec 6, 2022
2fbdddd
updated docker compose to build a single cluster with proper exposed …
divye11 Dec 7, 2022
a6cee3a
Merge branch 'sequelize:main' into main
divye11 Dec 7, 2022
ad7f647
Merge branch 'sequelize:main' into main
divye11 Jan 4, 2023
28c7c1a
updated data types, connection manager, and query generator to inheri…
divye11 Jan 11, 2023
7589ecd
Merge branch 'main' of github.com:divye11/sequelize-cockroachdb
divye11 Jan 11, 2023
5719cb3
Merge branch 'sequelize:main' into main
divye11 Jan 11, 2023
9686cdb
updated query, query generator, types, query interface and added supp…
divye11 Jan 17, 2023
203e3ca
Merge branch 'main' of https://github.com/sequelize/sequelize
divye11 Jan 17, 2023
e05df1f
Merge branch 'sequelize:main' into main
divye11 Jan 18, 2023
3187642
changed file structure
divye11 Jan 18, 2023
8eee4a4
Merge branch 'sequelize:main' into main
divye11 Jan 19, 2023
b0ca8d5
removed outdated files. removed .d.ts files, migrated to TS for each …
divye11 Jan 19, 2023
5432da2
Merge branch 'main' of github.com:divye11/sequelize-cockroachdb
divye11 Jan 19, 2023
a0a02e5
linter fix
divye11 Jan 19, 2023
d32dda7
Merge branch 'sequelize:main' into main
divye11 Jan 20, 2023
5caa5f4
linter fix
divye11 Jan 20, 2023
335ddfe
added config variables
divye11 Jan 20, 2023
aa92184
Merge branch 'sequelize:main' into main
divye11 Jan 23, 2023
848beb7
merged main from upstream
divye11 Jan 23, 2023
fb69929
Merge branch 'sequelize:main' into main
divye11 Jan 24, 2023
4131a92
improved overrides, imports, exports. added a linter rule in inner es…
divye11 Jan 24, 2023
5f1c3ce
removed linting fixes
divye11 Feb 1, 2023
0969c83
merged main into divye11:main
divye11 Feb 1, 2023
bb1c975
updated few test configs to add crdb ports, reverted a import/order c…
divye11 Feb 14, 2023
63a72f0
Merge branch 'main' of github.com:sequelize/sequelize
divye11 Feb 14, 2023
379d476
Merge branch 'main' of github.com:sequelize/sequelize
divye11 Feb 17, 2023
415c5ea
reverted a few changes, removed array definition for crdb data type
divye11 Feb 17, 2023
eaac9d6
linter fix
divye11 Feb 17, 2023
4a24e83
build fix
divye11 Feb 17, 2023
d2f6923
Merge branch 'main' of github.com:sequelize/sequelize
divye11 Feb 19, 2023
b54c111
updated some tests to include cockroachdb as a dialect
divye11 Feb 21, 2023
38019d2
Merge branch 'main' of github.com:sequelize/sequelize
divye11 Feb 21, 2023
77dcedf
fix failing tests and updated expectations for unit tests
divye11 Mar 3, 2023
383f60b
Merge branch 'main' of github.com:sequelize/sequelize
divye11 Mar 3, 2023
df562f1
fixed a few unit tests, added query and connection manager definition…
divye11 Mar 8, 2023
4cc3361
fix failing tests
divye11 Mar 8, 2023
e20918e
added CI tests for cockraochdb, modified a few parameters, organized …
divye11 Mar 13, 2023
edbc389
fixed tests
divye11 Apr 13, 2023
9e2f03e
fixed linting errors
divye11 Apr 19, 2023
62658e9
Merge branch 'sequelize:main' into main
divye11 Apr 19, 2023
23fd4c1
fixed test failing for postgres due to previous change
divye11 Apr 19, 2023
25cce0d
fix failing tests
divye11 Apr 19, 2023
9f8c776
fix failing tests
divye11 Apr 19, 2023
973a465
removed container modifications
divye11 Apr 19, 2023
ec9016a
modified docker container
divye11 Apr 19, 2023
7dd6975
added integration tests to CI for CRDB
divye11 Apr 19, 2023
c561916
Merge branch 'main' of https://github.com/sequelize/sequelize
divye11 Apr 20, 2023
3ae9812
removing linter issues from source files
divye11 Apr 21, 2023
9bafaad
altered data types
divye11 Apr 24, 2023
fb5fd15
addressed a few review comments by reusing code
divye11 Apr 25, 2023
6adc4c7
removed connection manager
divye11 Apr 25, 2023
873c01d
addressed a few review comments and added the latest as well as oldes…
divye11 Apr 26, 2023
6e60f12
removed a file
divye11 Apr 27, 2023
daaffb1
changed a few tests, modified a few files, added query definition for…
divye11 Apr 28, 2023
820291c
merged main from upstream and modified mixin tests
divye11 May 2, 2023
486912c
added a few comments, uncommented and modified a few tests to work wi…
divye11 May 5, 2023
6ae87a9
Merge branch 'main' of https://github.com/sequelize/sequelize
divye11 May 5, 2023
9c813bb
commented a test since oly top orderby are considered
divye11 May 5, 2023
bc5a0ed
modified failing test
divye11 May 5, 2023
44444df
Revert "modified failing test"
divye11 May 5, 2023
05751c8
modified failing test
divye11 May 5, 2023
7e22c83
fixed a few linting issues
divye11 Jun 1, 2023
5320c56
fixed a failing test
divye11 Jun 1, 2023
0af35e6
meta: updated with main, and changed index queries to accomodate new …
divye11 Jul 6, 2023
18cb576
Merge branch 'main' of github.com:sequelize/sequelize
divye11 Jul 7, 2023
7adf597
removed test file
divye11 Jul 10, 2023
2da7559
meta: merged upstream main
divye11 Jul 10, 2023
60434cd
fix: imported individual methods from lodash
divye11 Jul 10, 2023
cbec4c2
Merge branch 'main' of github.com:sequelize/sequelize
divye11 Jul 12, 2023
38ae69a
fix: failing tests for cockroachdb:latest
divye11 Jul 14, 2023
cec92a7
fix: failing timestamp test for cockroachdb
divye11 Jul 14, 2023
1db9cc9
Merge branch 'main' of github.com:sequelize/sequelize
divye11 Jul 14, 2023
d1a5446
Merge branch 'main' into main
divye11 Jul 14, 2023
dbb5b09
Merge branch 'main' of github.com:divye11/sequelize-cockroachdb
divye11 Jul 14, 2023
8c94ddc
fix: reverted yarn lock changes
divye11 Jul 14, 2023
28a8578
Merge branch 'main' of github.com:sequelize/sequelize
divye11 Jul 18, 2023
5180503
fix: updated query to parse fields properly, skipped a test which doe…
divye11 Jul 21, 2023
d5e7895
Merge branch 'main' of github.com:sequelize/sequelize
divye11 Jul 25, 2023
bc04f94
fix: added a fix for cockroachdb expectations
divye11 Jul 28, 2023
c4221fd
meta: merged main into fork
divye11 Jul 28, 2023
805ad93
fix: updated latest and oldest versions for Cockroachdb tests
divye11 Jul 31, 2023
dfddbde
Merge branch 'main' into main
divye11 Aug 1, 2023
bdf9d55
Merge branch 'main' into main
divye11 Aug 1, 2023
aa8c069
fix: lint fixes
divye11 Aug 2, 2023
fe01a8f
Merge branch 'main' of github.com:sequelize/sequelize
divye11 Aug 2, 2023
e3f13e1
Merge branch 'main' of github.com:sequelize/sequelize
divye11 Aug 3, 2023
a11833e
fix: mysql failing test
divye11 Aug 3, 2023
9ed6c83
fix: failing tests for postgres
divye11 Aug 4, 2023
7ab1100
fix: failing postgres tests
divye11 Aug 4, 2023
0ff0ad9
fix: failing cockroachdb tests
divye11 Aug 4, 2023
d165fa2
Merge branch 'main' into main
divye11 Aug 4, 2023
d19a8f6
fix: failing cockroachdb tests
divye11 Aug 4, 2023
a7fe539
fix: versioning in CRDB test
divye11 Aug 4, 2023
6d71fb6
Merge branch 'main' into main
divye11 Aug 7, 2023
06599a5
Merge branch 'main' into main
divye11 Aug 8, 2023
7d0d22e
meta: updated the contributing docs
divye11 Aug 8, 2023
4349c9e
fix: empty commit to reset nx cache
divye11 Aug 8, 2023
7e7b109
Merge branch 'main' into main
divye11 Aug 9, 2023
53d776f
meta: fixed docs and rese4t nx cache
divye11 Aug 9, 2023
b3179a0
Merge branch 'main' of github.com:divye11/sequelize-cockroachdb
divye11 Aug 9, 2023
678f110
Merge branch 'main' into main
divye11 Aug 11, 2023
3bdbaf7
Merge branch 'main' of github.com:sequelize/sequelize into cockroachd…
divye11 Aug 17, 2023
fd3692e
fix: turned off transactions for Cockroachdb to make a smaller PR
divye11 Aug 17, 2023
e663bc0
meta: changed comment
divye11 Aug 17, 2023
1ec03e4
meta: changed comment
divye11 Aug 17, 2023
e2beef3
meta: updated nx cache
divye11 Aug 18, 2023
fcd8b9b
added a comment for the unique parsing for int8 in cockroachdb
divye11 Aug 18, 2023
abf412b
fix: resolve wsay fixes for review comments
divye11 Sep 17, 2023
7db6d8b
meta: merged upstream mai
divye11 Oct 17, 2023
c26e6ab
fix: fixed conflicts
divye11 Oct 19, 2023
39dd7a7
Merge branch 'main' into cockroachdb-support-without-transactions
divye11 Oct 19, 2023
adee21f
Merge branch 'main' of github.com:sequelize/sequelize into cockroachd…
divye11 Nov 5, 2023
23d33e8
changed enum storage procedure
divye11 Nov 27, 2023
1f78847
Merge branch 'cockroachdb-support-without-transactions' of github.com…
divye11 Nov 27, 2023
f669df1
Merge branch 'main' into cockroachdb-support-without-transactions
divye11 Nov 27, 2023
e022e64
fix: merged upstream main and resolved conflicts
divye11 Jan 30, 2024
2b1003f
Merge branch 'cockroachdb-support-without-transactions' of github.com…
divye11 Jan 30, 2024
f7a8507
fix: merged upstream main and resolved conflicts
divye11 Jan 30, 2024
5226fe8
Merge branch 'main' into cockroachdb-support-without-transactions
divye11 Jan 31, 2024
6eb1b18
fix: UT fix for uuid
divye11 Feb 1, 2024
5255738
merged upstream main into origin main
divye11 Feb 2, 2024
66dea65
Merge branch 'main' into cockroachdb-support-without-transactions
divye11 Feb 2, 2024
ef9a12b
Merge branch 'main' into cockroachdb-support-without-transactions
divye11 Feb 2, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Expand Up @@ -95,6 +95,8 @@ jobs:
run: yarn lerna run test-unit-ibmi --scope=@sequelize/core
- name: Unit tests (snowflake)
run: yarn lerna run test-unit-snowflake --scope=@sequelize/core
- name: Unit tests (cockroachdb)
run: yarn lerna run test-unit-cockroachdb --scope=@sequelize/core
test-win:
strategy:
fail-fast: false
Expand Down Expand Up @@ -216,7 +218,7 @@ jobs:
matrix:
node-version: [18,20]
database-version: [oldest, latest]
dialect: [mysql, mariadb, mssql, db2]
dialect: [mysql, mariadb, mssql, db2, cockroachdb]
name: ${{ matrix.dialect }} ${{ matrix.database-version }} (Node ${{ matrix.node-version }})
runs-on: ubuntu-latest
needs: [ unit-test, test-typings ]
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/pr-title.yml
Expand Up @@ -36,5 +36,6 @@ jobs:
snowflake
sqlite
types
cockroachdb
ignoreLabels: |
ignore-semantic-pull-request
6 changes: 5 additions & 1 deletion CONTRIBUTING.md
Expand Up @@ -119,7 +119,7 @@ Once installed, run the `yarn build` command to build the project.

#### 2.1 Adding and updating dependencies

[Yarn v3](https://yarnpkg.com//) is used in the CI/CD pipeline so adding and updating dependencies must be done with Yarn.
[Yarn v3](https://yarnpkg.com/) is used in the CI/CD pipeline so adding and updating dependencies must be done with Yarn.

#### 2.2 Running commands

Expand Down Expand Up @@ -161,6 +161,7 @@ If you have Docker installed, use any of the following commands to start fresh l
- `yarn start-postgres-oldest` (for Postgres 11) or `yarn start-postgres-latest` (for Postgres 15)
- `yarn start-mssql-oldest` (for MSSQL 2017) or `yarn start-mssql-latest` (for MSSQL 2022)
- `yarn start-db2-oldest` (for Db2 11.5.5.1) or `yarn start-db2-latest` (for Db2 11.5.8.0)
- `yarn start-cockroachdb-oldest` (for Cockroachdb 22.2.0) or `yarn start-cockroachdb-latest` (for Cockroachdb 23.1.6)

_Note:_ if you're using Windows, make sure you run these from Git Bash (or another MinGW environment), since these commands will execute bash scripts. Recall that [it's very easy to include Git Bash as your default integrated terminal on Visual Studio Code](https://code.visualstudio.com/docs/editor/integrated-terminal).

Expand Down Expand Up @@ -197,6 +198,7 @@ Then, if you want to run tests for another dialect, assuming you've set it up as
- `yarn lerna run test-postgres`
- `yarn lerna run test-mssql`
- `yarn lerna run test-db2`
- `yarn lerna run test-cockroachdb`

There are also the `test-unit-*` and `test-integration-*` sets of scripts (for example, `test-integration-postgres`).

Expand Down Expand Up @@ -226,6 +228,7 @@ Run it for the dialect of your choice using one of the following commands:
- `npm run sscce-sqlite` / `yarn sscce-sqlite`
- `npm run sscce-mssql` / `yarn sscce-mssql`
- `npm run sscce-db2` / `yarn sscce-db2`
- `npm run sscce-cockroachdb` / `yarn sscce-cockroachdb`

_Note:_ First, you need to set up (once) the database instance for corresponding dialect, as explained on [Section 3a](https://github.com/sequelize/sequelize/blob/main/CONTRIBUTING.md#3a-with-docker-recommended).

Expand All @@ -252,6 +255,7 @@ We use a simple conventional commits convention:
- `db2`
- `ibmi`
- `snowflake`
- `cockroachdb`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be added to the scopes in .github/workflows/pr-title.yml as well

- `types`
- If your changes impact more than one scope, simply omit the scope.

Expand Down
23 changes: 23 additions & 0 deletions dev/cockroachdb/latest/docker-compose.yml
@@ -0,0 +1,23 @@
services:
cockroachdb-latest:
container_name: sequelize-cockroachdb-latest
image: cockroachdb/cockroach:v23.1.14
ports:
- 26257:26257
- 8080:8080
command: start-single-node --cluster-name=cockroach-sequelize-example --advertise-addr='localhost' --insecure
environment:
COCKROACH_DATABASE: public
volumes:
- cockroach-single:/cockroach/cockroach-data
- ~/init-scripts:/docker-entrypoint-initdb.d
healthcheck:
test: curl --fail http://localhost:8080 || exit 1
interval: 3s
timeout: 1s
retries: 10
networks:
default:
name: sequelize-cockroachdb-latest-network
volumes:
cockroach-single:
25 changes: 25 additions & 0 deletions dev/cockroachdb/latest/start.sh
@@ -0,0 +1,25 @@
#!/usr/bin/env bash
set -Eeuxo pipefail # https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/
cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")" # https://stackoverflow.com/a/17744637

docker compose -p sequelize-cockroachdb-latest down --remove-orphans
docker compose -p sequelize-cockroachdb-latest up -d

./../../wait-until-healthy.sh sequelize-cockroachdb-latest

# Optimising the single node cluster
# Read more here https://cockroachlabs.com/docs/stable/local-testing.html#use-a-local-single-node-cluster-with-in-memory-storage
docker exec sequelize-cockroachdb-latest ./cockroach sql --insecure --execute="
SET CLUSTER SETTING kv.raft_log.disable_synchronization_unsafe = true;
SET CLUSTER SETTING kv.range_merge.queue_interval = '50ms';
SET CLUSTER SETTING jobs.registry.interval.gc = '30s';
SET CLUSTER SETTING jobs.registry.interval.cancel = '180s';
SET CLUSTER SETTING jobs.retention_time = '15s';
SET CLUSTER SETTING sql.stats.automatic_collection.enabled = false;
SET CLUSTER SETTING kv.range_split.by_load_merge_delay = '5s';
ALTER RANGE default CONFIGURE ZONE USING "gc.ttlseconds" = 600;
ALTER DATABASE system CONFIGURE ZONE USING "gc.ttlseconds" = 600;
Comment on lines +20 to +21
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ALTER RANGE default CONFIGURE ZONE USING "gc.ttlseconds" = 600;
ALTER DATABASE system CONFIGURE ZONE USING "gc.ttlseconds" = 600;
ALTER RANGE default CONFIGURE ZONE USING \"gc.ttlseconds\" = 600;
ALTER DATABASE system CONFIGURE ZONE USING \"gc.ttlseconds\" = 600;

for https://github.com/koalaman/shellcheck/wiki/SC2140

DROP DATABASE IF EXISTS sequelize_test;
CREATE DATABASE IF NOT EXISTS sequelize_test;
CREATE USER IF NOT EXISTS sequelize_test;
GRANT admin TO sequelize_test;"
7 changes: 7 additions & 0 deletions dev/cockroachdb/latest/stop.sh
@@ -0,0 +1,7 @@
#!/usr/bin/env bash
set -Eeuxo pipefail # https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/
cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")" # https://stackoverflow.com/a/17744637

docker compose -p sequelize-cockroachdb-latest down --remove-orphans

echo "Cockroach db cluster stopped"
23 changes: 23 additions & 0 deletions dev/cockroachdb/oldest/docker-compose.yml
@@ -0,0 +1,23 @@
services:
cockroachdb-oldest:
container_name: sequelize-cockroachdb-oldest
image: cockroachdb/cockroach:v22.2.0
ports:
- 26257:26257
- 8080:8080
command: start-single-node --cluster-name=cockroach-sequelize-example --advertise-addr='localhost' --insecure
environment:
COCKROACH_DATABASE: public
volumes:
- cockroach-single:/cockroach/cockroach-data-oldest
- ~/init-scripts:/docker-entrypoint-initdb.d
healthcheck:
test: curl --fail http://localhost:8080 || exit 1
interval: 3s
timeout: 1s
retries: 10
networks:
default:
name: sequelize-cockroachdb-oldest-network
volumes:
cockroach-single:
25 changes: 25 additions & 0 deletions dev/cockroachdb/oldest/start.sh
@@ -0,0 +1,25 @@
#!/usr/bin/env bash
set -Eeuxo pipefail # https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/
cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")" # https://stackoverflow.com/a/17744637

docker compose -p sequelize-cockroachdb-oldest down --remove-orphans
docker compose -p sequelize-cockroachdb-oldest up -d

./../../wait-until-healthy.sh sequelize-cockroachdb-oldest

# Optimising the single node cluster
# Read more here https://cockroachlabs.com/docs/stable/local-testing.html#use-a-local-single-node-cluster-with-in-memory-storage
docker exec sequelize-cockroachdb-oldest ./cockroach sql --insecure --execute="
SET CLUSTER SETTING kv.raft_log.disable_synchronization_unsafe = true;
SET CLUSTER SETTING kv.range_merge.queue_interval = '50ms';
SET CLUSTER SETTING jobs.registry.interval.gc = '30s';
SET CLUSTER SETTING jobs.registry.interval.cancel = '180s';
SET CLUSTER SETTING jobs.retention_time = '15s';
SET CLUSTER SETTING sql.stats.automatic_collection.enabled = false;
SET CLUSTER SETTING kv.range_split.by_load_merge_delay = '5s';
ALTER RANGE default CONFIGURE ZONE USING "gc.ttlseconds" = 600;
ALTER DATABASE system CONFIGURE ZONE USING "gc.ttlseconds" = 600;
Comment on lines +20 to +21
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ALTER RANGE default CONFIGURE ZONE USING "gc.ttlseconds" = 600;
ALTER DATABASE system CONFIGURE ZONE USING "gc.ttlseconds" = 600;
ALTER RANGE default CONFIGURE ZONE USING \"gc.ttlseconds\" = 600;
ALTER DATABASE system CONFIGURE ZONE USING \"gc.ttlseconds\" = 600;

DROP DATABASE IF EXISTS sequelize_test;
CREATE DATABASE IF NOT EXISTS sequelize_test;
CREATE USER IF NOT EXISTS sequelize_test;
GRANT admin TO sequelize_test;"
7 changes: 7 additions & 0 deletions dev/cockroachdb/oldest/stop.sh
@@ -0,0 +1,7 @@
#!/usr/bin/env bash
set -Eeuxo pipefail # https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/
cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")" # https://stackoverflow.com/a/17744637

docker compose -p sequelize-cockroachdb-oldest down --remove-orphans

echo "Cockroach db cluster stopped"
6 changes: 5 additions & 1 deletion package.json
Expand Up @@ -28,13 +28,16 @@
"start-mssql-oldest": "bash dev/mssql/oldest/start.sh",
"start-mssql-latest": "bash dev/mssql/latest/start.sh",
"start-db2-oldest": "bash dev/db2/oldest/start.sh",
"start-cockroachdb-oldest": "bash dev/cockroachdb/oldest/start.sh",
"start-cockroachdb-latest": "bash dev/cockroachdb/latest/start.sh",
"start-db2-latest": "bash dev/db2/latest/start.sh",
"start-oldest": "concurrently \"npm:start-*-oldest\"",
"start-latest": "concurrently \"npm:start-*-latest\"",
"stop-mariadb": "bash dev/mariadb/oldest/stop.sh; bash dev/mariadb/latest/stop.sh",
"stop-mysql": "bash dev/mysql/oldest/stop.sh; bash dev/mysql/latest/stop.sh",
"stop-postgres": "bash dev/postgres/oldest/stop.sh; bash dev/postgres/latest/stop.sh",
"stop-mssql": "bash dev/mssql/oldest/stop.sh; bash dev/mssql/latest/stop.sh",
"stop-cockroachdb": "bash dev/cockroachdb/oldest/stop.sh; bash dev/cockroachdb/latest/stop.sh",
"stop-db2": "bash dev/db2/oldest/stop.sh; bash dev/db2/latest/stop.sh",
"stop-all": "concurrently \"npm:stop-*(!all)\"",
"----------------------------------------- SSCCEs ------------------------------------------": "",
Expand All @@ -45,7 +48,8 @@
"sscce-postgres-native": "cross-env DIALECT=postgres-native yarn sscce",
"sscce-sqlite": "cross-env DIALECT=sqlite yarn sscce",
"sscce-mssql": "cross-env DIALECT=mssql yarn sscce",
"sscce-db2": "cross-env DIALECT=db2 yarn sscce"
"sscce-db2": "cross-env DIALECT=db2 yarn sscce",
"sscce-cockroachdb": "cross-env DIALECT=cockroachdb yarn sscce"
},
"workspaces": [
"packages/*"
Expand Down
10 changes: 7 additions & 3 deletions packages/core/package.json
Expand Up @@ -149,7 +149,8 @@
"nodejs",
"object relational mapper",
"database",
"db"
"db",
"cockroachdb"
],
"publishConfig": {
"access": "public",
Expand Down Expand Up @@ -179,7 +180,8 @@
"test-unit-db2": "cross-env DIALECT=db2 yarn _test-unit",
"test-unit-ibmi": "cross-env DIALECT=ibmi yarn _test-unit",
"test-unit-snowflake": "cross-env DIALECT=snowflake yarn _test-unit",
"test-unit-all": "yarn test-unit-mariadb && yarn test-unit-mysql && yarn test-unit-postgres && yarn test-unit-mssql && yarn test-unit-sqlite && yarn test-unit-snowflake && yarn test-unit-db2 && yarn test-unit-ibmi",
"test-unit-cockroachdb": "cross-env DIALECT=cockroachdb yarn _test-unit",
"test-unit-all": "yarn test-unit-mariadb && yarn test-unit-mysql && yarn test-unit-postgres && yarn test-unit-mssql && yarn test-unit-sqlite && yarn test-unit-snowflake && yarn test-unit-db2 && yarn test-unit-ibmi && yarn test-unit-cockroachdb",
"test-unit": "yarn test-unit-all",
"----------------------------------------- integration tests ---------------------------------------------": "",
"test-integration-mariadb": "cross-env DIALECT=mariadb yarn test-integration",
Expand All @@ -191,7 +193,8 @@
"test-integration-db2": "cross-env DIALECT=db2 yarn test-integration",
"test-integration-ibmi": "cross-env DIALECT=ibmi yarn test-integration",
"test-integration-snowflake": "cross-env DIALECT=snowflake yarn test-integration",
"test-integration-all": "yarn test-integration-mariadb && yarn test-integration-mysql && yarn test-integration-postgres && yarn test-integration-postgres-native && yarn test-integration-sqlite && yarn test-integration-mssql && yarn test-integration-db2 && yarn test-integration-ibmi && yarn test-integration-snowflake",
"test-integration-cockroachdb": "cross-env DIALECT=cockroachdb yarn test-integration",
"test-integration-all": "yarn test-integration-mariadb && yarn test-integration-mysql && yarn test-integration-postgres && yarn test-integration-postgres-native && yarn test-integration-sqlite && yarn test-integration-mssql && yarn test-integration-db2 && yarn test-integration-ibmi && yarn test-integration-snowflake && test-integration-cockroachdb",
"----------------------------------------- all tests ---------------------------------------------": "",
"test-mariadb": "cross-env DIALECT=mariadb yarn test",
"test-mysql": "cross-env DIALECT=mysql yarn test",
Expand All @@ -201,6 +204,7 @@
"test-mssql": "cross-env DIALECT=mssql yarn test",
"test-db2": "cross-env DIALECT=db2 yarn test",
"test-ibmi": "cross-env DIALECT=ibmi yarn test",
"test-cockroachdb": "cross-env DIALECT=cockroachdb yarn test",
"----------------------------------------- development ---------------------------------------------": "",
"build": "node ../../build-packages.mjs core"
},
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/dialects/abstract/index.ts
Expand Up @@ -237,6 +237,7 @@ export type DialectSupports = {
removeColumn: {
cascade: boolean,
ifExists: boolean,
primaryKeyColumn: boolean,
},
renameTable: {
changeSchema: boolean,
Expand Down Expand Up @@ -400,6 +401,7 @@ export abstract class AbstractDialect {
removeColumn: {
cascade: false,
ifExists: false,
primaryKeyColumn: true,
},
renameTable: {
changeSchema: true,
Expand Down
@@ -0,0 +1,4 @@
import { PostgresConnectionManager } from '../postgres/connection-manager';

export class CockroachdbConnectionManager extends PostgresConnectionManager {}

81 changes: 81 additions & 0 deletions packages/core/src/dialects/cockroachdb/data-types-db.ts
@@ -0,0 +1,81 @@
import assert from 'node:assert';
import wkx from 'wkx';
import * as BaseTypes from '../abstract/data-types';
import { getDataTypeParser } from '../abstract/data-types-utils';
import { buildRangeParser } from '../postgres/range';
import type { CockroachDbDialect } from './index';

export function registerCockroachDbDataTypeParsers(dialect: CockroachDbDialect) {
// dateonly
dialect.registerDataTypeParser(['date'], (value: unknown) => {
return value;
});

dialect.registerDataTypeParser(['timestamptz', 'timestamp'], (value: unknown) => {
// override default parser to prevent returning a Date object (which is the default behavior in pg).
// return dates as string, not Date objects. Different implementations could be used instead (such as Temporal, dayjs)
return value;
});

dialect.registerDataTypeParser(['decimal', 'numeric'], (value: string) => {
if (value === null) {
return null;
}

if (value === 'NaN') {
return Number.NaN;
}

return value;
});

/**
* Cockroachdb's ALIAS for INTEGER and BIGINT defaults to int(64-bit value). So while retreiving the values from the database both values are
* returned as int8. While parsing them we cannot be sure if the expected behavior is of INTEGER or BIGINT so we make sure if the number is
* < than Number.MAX_SAFE_INTEGER --> Parse it as a number
* > than Number.MAX_SAFE_INTEGER --> Parse it as a string
* Note: Storing INTEGERS as INT4 and BIGINT as int8 would result in an anti-pattern since Cockroachdb uses unique_row_id() to generate a unique ID for
* each row by default to ensure global uniqueness, reseliency and consistency.
*/
dialect.registerDataTypeParser(['int8'], (value: string) => {
if (value === null) {
return null;
}

const parsedValue = BigInt(value);

if (parsedValue > Number.MAX_SAFE_INTEGER || parsedValue < Number.MIN_SAFE_INTEGER) {
return String(value);
} else if (Number(value) >= Number.MIN_SAFE_INTEGER) {
return Number(value);
}

return String(value);

});

dialect.registerDataTypeParser(['geography'], (value: unknown) => {
assert(typeof value === 'string', 'Expected geography value to be a string');

const b = Buffer.from(value, 'hex');

return wkx.Geometry.parse(b).toGeoJSON({ shortCrs: true });
});

dialect.registerDataTypeParser(['geometry'], (value: unknown) => {
assert(typeof value === 'string', 'Expected geometry value to be a string');

const b = Buffer.from(value, 'hex');

return wkx.Geometry.parse(b).toGeoJSON({ shortCrs: true });
});

const parseInteger = getDataTypeParser(dialect, BaseTypes.INTEGER);
dialect.registerDataTypeParser(['int4range'], buildRangeParser(parseInteger));

const parseBigInt = getDataTypeParser(dialect, BaseTypes.BIGINT);
dialect.registerDataTypeParser(['int8range'], buildRangeParser(parseBigInt));

const parseDecimal = getDataTypeParser(dialect, BaseTypes.DECIMAL);
dialect.registerDataTypeParser(['numrange'], buildRangeParser(parseDecimal));
}