Skip to content

feat(core): add sql.random, improve Order typing - #18203

Merged
WikiRik merged 9 commits into
mainfrom
ephys/sql-random
Apr 6, 2026
Merged

WikiRik merged 9 commits into
mainfrom
ephys/sql-random

Conversation

@ephys

@ephys ephys commented Apr 6, 2026 •

Copy link
Copy Markdown
Member

Pull Request Checklist

  • Have you added new tests to prevent regressions?
  • If a documentation update is necessary, have you opened a PR to the documentation repository?
  • Did you update the typescript typings accordingly (if applicable)?
  • Does the description below contain a link to an existing issue (Closes #[issue]) or a description of the issue you are solving?
  • Does the name of your PR follow our conventions?

Description of Changes

This PR adds sql.random as a replacement for sequelize.random. Unlike the latter, sql.random is:

  • Not hardcoded in sequelize, allowing third party dialects to support it
  • Not bound to a sequelize instance, so can be used statically

I then migrated order.test.js to TypeScript, because I edited it to use sql.random instead of sequelize.random, which uncovered that the typings for Order was wrong, so I also fixed these typings.

Finally, and sorry that this wasn't really related to this PR, I renamed all sql variables that are actually queryGenerator, because it was confusing to have sql point to two different objects in our tests

List of Breaking Changes

None

Summary by CodeRabbit

  • New Features

    • Added sql.random to produce random float values (0–1) with dialect-specific SQL and JS fallback.
  • Deprecations

    • Deprecated sequelize.random(); use sql.random instead (deprecation warning added).
  • Bug Fixes

    • Improved dummy SELECT handling for dialects that require a dummy table during connection/authentication checks.
  • Tests

    • Added integration/unit tests for sql.random and updated query-generator tests to use the dialect query generator.

@coderabbitai

coderabbitai Bot commented Apr 6, 2026 •

Copy link
Copy Markdown
Contributor

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Adds a dialect-aware Random expression exposed as sql.random, extends dialect capabilities with randomFloatGeneration and select.dummyTable, implements per-dialect SQL random function calls, deprecates Sequelize#random(), tightens ordering types, and updates tests to use queryGenerator and cover sql.random.

Changes

Cohort / File(s) Summary
Core Random Builder & API
packages/core/src/expression-builders/random.ts, packages/core/src/expression-builders/sql.ts, packages/core/src/utils/deprecations.ts, packages/core/src/sequelize.js
Added Random expression builder (JS: Math.random(), SQL: dialect-gated) exposed as sql.random. Introduced noSequelizeRandom deprecation helper and updated Sequelize#random() to return sql.random and use the deprecation.
Abstract Dialect / Query Generator API
packages/core/src/abstract-dialect/dialect.ts, packages/core/src/abstract-dialect/query-generator-typescript.ts
Added randomFloatGeneration: boolean and `select: { dummyTable: null
Dialect Implementations
packages/postgres/src/query-generator-typescript.internal.ts, packages/sqlite3/src/query-generator-typescript.internal.ts, packages/snowflake/src/query-generator-typescript.internal.ts, packages/mysql/src/query-generator-typescript.internal.ts, packages/mariadb/src/query-generator-typescript.internal.ts, packages/mssql/src/query-generator-typescript.internal.ts, packages/db2/src/query-generator-typescript.internal.ts, packages/ibmi/src/query-generator-typescript.internal.ts, packages/oracle/src/query-generator-typescript.internal.ts
Implemented getRandomFloatFunctionCall() per dialect returning dialect-specific SQL fragments (e.g., RANDOM(), RAND(), DBMS_RANDOM.VALUE(), or SQLite normalized expression).
Dialect select.dummyTable entries
packages/ibmi/src/dialect.ts, packages/oracle/src/dialect.ts
Added select.dummyTable values for IBM i ('SYSIBM.SYSDUMMY1') and Oracle ('DUAL') to dialect support metadata.
Type Definitions
packages/core/src/model.d.ts
Imported BaseSqlExpression; refactored ordering types (added AssociationName, revised OrderItemAssociation, narrowed OrderItemColumn, added OrderDirection, adjusted OrderItem/Order shapes).
QueryGenerator usage & tests
packages/core/test/unit/sql/*, packages/core/test/unit/dialects/postgres/enum.test.js, packages/core/test/unit/query-interface/create-table.test.ts, packages/core/test/integration/sql/random.test.ts, packages/core/test/unit/sql/order.test.ts, packages/core/test/unit/utils/utils.test.ts
Renamed local test aliases to queryGenerator and updated call sites. Added unit + integration tests for sql.random (createTable defaultValue and integration SELECT numeric range). Extended cloneDeep test to include sql.random. Mostly test-local changes and minor formatting normalizations.
Test helper typings
packages/core/test/support.ts
Adjusted expectsql overload declaration ordering and added doc comment (no runtime change).

Sequence Diagram

sequenceDiagram
    participant Client
    participant SqlRandom as sql.random
    participant Dialect as AbstractDialect
    participant QueryGen as QueryGenerator
    participant DialectImpl as DialectImplementation

    Client->>SqlRandom: use sql.random (in SELECT / DEFAULT)
    activate SqlRandom
    SqlRandom->>Dialect: supportsDialect(dialect) -> check randomFloatGeneration
    Dialect-->>SqlRandom: supported / unsupported

    alt supported
        SqlRandom->>QueryGen: getRandomFloatFunctionCall()
        QueryGen->>DialectImpl: return dialect-specific SQL fragment
        DialectImpl-->>QueryGen: "RANDOM()" / "RAND()" / "DBMS_RANDOM.VALUE()" / ...
        QueryGen-->>SqlRandom: SQL fragment
        SqlRandom-->>Client: SQL random expression (embedded in query)
    else unsupported
        SqlRandom-->>Client: throw unsupported error
    end
    deactivate SqlRandom
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested reviewers

  • sdepold
  • WikiRik

Poem

🐇 I nibble on chance and hop to find,
A tiny random spark for humankind.
sql.random hums from JS to SQL,
From dialects small to servers full,
A rabbit's leap — a roll of delight ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the two main changes: introducing sql.random and improving Order typing, both of which are prominently featured in the changeset.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch ephys/sql-random

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@WikiRik

WikiRik commented Apr 6, 2026

Copy link
Copy Markdown
Member

Finally, and sorry that this wasn't really related to this PR, I renamed all sql variables that are actually queryGenerator, because it was confusing to have sql point to two different objects in our tests

No worries, I remember you doing this all the time. Good change

coderabbitai[bot]

This comment was marked as resolved.

coderabbitai[bot]

This comment was marked as resolved.

@WikiRik WikiRik left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We could benefit from (an update to) an integration test for sql.random

Comment thread packages/core/src/model.d.ts Resolved
@ephys
ephys requested a review from WikiRik April 6, 2026 15:58
@ephys

ephys commented Apr 6, 2026

Copy link
Copy Markdown
Member Author

Crazy that we do not have an integration test for sequelize/sql.random, I'll add one

@ephys
ephys removed the request for review from WikiRik April 6, 2026 16:03
@ephys
ephys requested a review from WikiRik April 6, 2026 16:49
@ephys

ephys commented Apr 6, 2026

Copy link
Copy Markdown
Member Author

New changes:

  • To be able to write the integration test without adding dialect-specific code I added dialect.supports.select.dummyTable to control how to generate queries without a table
  • sequelize.authenticate therefore does not have dialect-specific code anymore
  • Fixed the random generated by sqlite3 as all dialects except sqlite3 generate a float between 0 and 1

coderabbitai[bot]

This comment was marked as resolved.

@ephys
ephys requested a review from WikiRik April 6, 2026 17:02
@WikiRik

WikiRik commented Apr 6, 2026

Copy link
Copy Markdown
Member

And I now remember that I shouldn't approve before CI is passing. Luckily we added that integration test

@ephys

ephys commented Apr 6, 2026

Copy link
Copy Markdown
Member Author

Thankfully it was a simple case of some dialects uppercasing identifiers by default

@ephys
ephys requested a review from WikiRik April 6, 2026 17:59
@WikiRik
WikiRik merged commit 1547f94 into main Apr 6, 2026
76 checks passed
@WikiRik
WikiRik deleted the ephys/sql-random branch April 6, 2026 18:20
@github-project-automation github-project-automation Bot moved this from Waiting for merge to To document in PRs & Issues to document on website Apr 6, 2026
@ephys ephys moved this from To document to Documented in PRs & Issues to document on website Apr 6, 2026
papandreou added a commit to papandreou/sequelize that referenced this pull request Apr 15, 2026
* main: (200 commits)
  meta: update actions/upload-artifact action to v7.0.1 (sequelize#18212)
  meta: update sequelize AUTHORS (sequelize#18208)
  feat(cli): add migration generate, run, status & undo (sequelize#18193)
  feat(core): Drop the ability to specify the dialect as a string (sequelize#18204)
  feat: update some dialect adapters (sequelize#18189)
  feat(postgres): replace pg-hstore with inline hstore parser (sequelize#18151)
  feat(core): add `sql.random`, improve `Order` typing (sequelize#18203)
  meta: use Sequelize Bot for drafting PRs (sequelize#18201)
  meta: automatically mark unfinished PRs as draft (sequelize#18197)
  meta: update dependency @oclif/test to v4.1.18 (sequelize#18198)
  fix(core): replace @typeparam with @template in JSDoc (sequelize#18094)
  feat(core): Add support for UUID v7 (sequelize#17832)
  meta: update sequelize AUTHORS (sequelize#18190)
  meta: update dependency @oclif/plugin-help to ^6.2.43 (sequelize#18195)
  meta: update dependency esbuild to v0.28.0 (sequelize#18192)
  meta: update dependency @oclif/plugin-help to ^6.2.42 (sequelize#18191)
  meta: update dependency @oclif/core to ^4.10.5 (sequelize#18187)
  meta: update sequelize AUTHORS (sequelize#18158)
  feat: add Node 24 support, drop Node 18 (sequelize#18185)
  feat(core): add support for readonly attribute arrays (sequelize#18186)
  ...
@coderabbitai coderabbitai Bot mentioned this pull request Jul 11, 2026
4 of 5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Documented

Development

Successfully merging this pull request may close these issues.

2 participants