Skip to content

When Cube runs dbType configuration callback function it sometimes try to find dataSource: undefined #10694

@ipipos56

Description

@ipipos56

Describe the bug
Sometimes when Cube is running it tries to getDbType with dataSource: undefined

To Reproduce
Steps to reproduce the behavior:

  1. Make simple cube.ts as mine
  2. Add simple .env as mine
  3. Add simple postgres init.sql as mine in init/init.sql
  4. Add simple docker-compose as mine to run cube and postgres
  5. Add simple esbuild.config.ts as mine to build cube.ts file
  6. Run tsx ./esbuild.config.ts and docker compose up
  7. Wait until sheduler starts his process
  8. See "datasource: undefined" in dbType calls

Expected behavior
dbType calls with correct dataSource and do not calls with dataSource: undefined

Logs

pnpm start:dev

> cube@0.0.1 start:dev /cube
> pnpm build && docker compose up


> cube@0.0.1 build /cube
> tsx ./esbuild.config.ts

Build complete!
WARN[0000] Found orphan containers ([cube-postgres-test-1]) for this project. If you removed or renamed this service in your compose file, you can run this command with the --remove-orphans flag to clean it up. 
[+] Running 4/4
 ✔ Network cube_default                                            Created                                                                         0.0s 
 ✔ Container test-postgres                                         Created                                                                         0.0s 
 ✔ Container cube-cube-1                                           Created                                                                         0.0s 
 ! cube Published ports are discarded when using host network mode                                                                                 0.0s 
Attaching to cube-1, test-postgres
test-postgres  | 
test-postgres  | PostgreSQL Database directory appears to contain a database; Skipping initialization
test-postgres  | 
test-postgres  | 2026-04-15 14:52:14.853 UTC [1] LOG:  starting PostgreSQL 15.17 (Debian 15.17-1.pgdg13+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 14.2.0-19) 14.2.0, 64-bit
test-postgres  | 2026-04-15 14:52:14.853 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
test-postgres  | 2026-04-15 14:52:14.854 UTC [1] LOG:  listening on IPv6 address "::", port 5432
test-postgres  | 2026-04-15 14:52:14.855 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
test-postgres  | 2026-04-15 14:52:14.858 UTC [30] LOG:  database system was shut down at 2026-04-15 14:52:08 UTC
test-postgres  | 2026-04-15 14:52:14.862 UTC [1] LOG:  database system is ready to accept connections
cube-1         | 🔥 Cube Store (1.6.34) is assigned to 3030 port.
cube-1         | 🔓 Authentication checks are disabled in developer mode. Please use NODE_ENV=production to enable it.
cube-1         | 🦅 Dev environment available at http://localhost:4000
cube-1         | 🔗 Cube SQL (pg) is listening on 0.0.0.0:15432
cube-1         | 🚀 Cube API server (1.6.34) is listening on 4000
cube-1         | 2026-04-15T14:52:15.365Z INFO  [cubestored] <pid:22> Cube Store version 1.6.34
cube-1         | 2026-04-15T14:52:15.369Z INFO  [cubestore::http::status] <pid:22> Serving status probes at 0.0.0.0:3031
cube-1         | 2026-04-15T14:52:15.369Z INFO  [cubestore::metastore::rocks_fs] <pid:22> Using existing metastore in /cube/conf/.cubestore/data/metastore
cube-1         | 2026-04-15T14:52:15.378Z INFO  [cubestore::http] <pid:22> Http Server is listening on 0.0.0.0:3030
cube-1         | 2026-04-15T14:52:15.378Z INFO  [cubestore::mysql] <pid:22> MySQL port open on 0.0.0.0:13306
cube-1         | 2026-04-15T14:52:30.379Z INFO  [cubestore::metastore::rocks_fs] <pid:22> Using existing cachestore in /cube/conf/.cubestore/data/cachestore
cube-1         | Performing query: scheduler-d0224285-fbb0-4fc5-b2fa-3f4c1281123f
cube-1         | Executing SQL: scheduler-d0224285-fbb0-4fc5-b2fa-3f4c1281123f
cube-1         | --
cube-1         |   SELECT FLOOR((UNIX_TIMESTAMP()) / 10) as refresh_key
cube-1         | --
cube-1         | Performing query completed: scheduler-d0224285-fbb0-4fc5-b2fa-3f4c1281123f (9ms)
cube-1         | datasource:  undefined
cube-1         | Performing query: scheduler-f0434fbd-3394-489e-96e8-e822d8736771
cube-1         | Executing SQL: scheduler-f0434fbd-3394-489e-96e8-e822d8736771
cube-1         | --
cube-1         |   SELECT FLOOR((UNIX_TIMESTAMP()) / 10) as refresh_key
cube-1         | --
cube-1         | Performing query completed: scheduler-f0434fbd-3394-489e-96e8-e822d8736771 (2ms)
cube-1         | datasource:  undefined

Cube.ts

import type { CreateOptions } from '@cubejs-backend/server-core';
import { PostgresDriver } from '@cubejs-backend/postgres-driver';

const configuration: CreateOptions = {
  telemetry: false,

  repositoryFactory: () => {
    return {
      dataSchemaFiles: async () => {
        return [
          {
            fileName: 'TestCube.js',
            content: `
              cube(\`TestCube\`, {
                sql: \`SELECT * FROM users\`,
                
                dataSource: 'test-postgres',

                measures: {
                  count: {
                    type: 'count'
                  }
                },

                dimensions: {
                  id: {
                    sql: 'id',
                    type: 'number',
                    primaryKey: true
                  },
                  name: {
                    sql: 'name',
                    type: 'string'
                  }
                }
              });
            `,
          },
        ];
      },
    };
  },

  schemaVersion: async () => 'v1',

  dbType: async ({ dataSource }) => {
    if (!dataSource) console.log("datasource: ", dataSource);
    if (dataSource === 'test-postgres') {
      return 'postgres';
    }

    return 'sqlite';
  },

  driverFactory: async ({ dataSource }) => {
    return new PostgresDriver({
      host: 'localhost',
      port: 5432,
      database: 'test',
      user: 'postgres',
      password: 'postgres',
    });
  },
};

module.exports = configuration;

.env

CUBEJS_DEV_MODE=true
CUBEJS_API_SECRET=testApiToken

esbuild.config.ts

import esbuild from 'esbuild';

async function build() {
  const cubeFilePatterns = ['cube.ts'];
  await esbuild.build({
    entryPoints: cubeFilePatterns,
    format: 'cjs',
    outdir: 'dist',
    outbase: 'src',
    platform: 'node',
    target: 'es2022',
    packages: 'external',
  });
  console.log('Build complete!');
}

build();

docker-compose.yml

services:
  cube:
    image: cubejs/cube:v1.6.34
    ports:
      - 4000:4000
      - 3000:3000
    env_file: .env
    volumes:
      - ./dist:/cube/conf
      - .empty:/cube/conf/node_modules/@cubejs-backend/
    network_mode: host
  
  test-postgres:
    image: postgres:15
    container_name: test-postgres
    restart: always
    environment:
      POSTGRES_DB: test
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
    ports:
      - "5432:5432"
    volumes:
      - ./pg-data:/var/lib/postgresql/data
      - ./init:/docker-entrypoint-initdb.d

init.sql

CREATE TABLE users (
  id SERIAL PRIMARY KEY,
  name TEXT,
  email TEXT,
  created_at TIMESTAMP DEFAULT NOW()
);

INSERT INTO users (name, email) VALUES
  ('Alice', 'alice@example.com'),
  ('Bob', 'bob@example.com'),
  ('Charlie', 'charlie@example.com');

Version:
Cube: 1.6.34
pnpm: 10.11.0
npm: 10.9.4
node: v22.21.1

Additional Context
Here the package.json to exactly run commands as mine

package.json

{
  "name": "cube",
  "version": "0.0.1",
  "devDependencies": {
    "@cubejs-backend/postgres-driver": "1.6.34",
    "@cubejs-backend/schema-compiler": "1.6.34",
    "@cubejs-backend/server-core": "1.6.34",
    "esbuild": "0.27.2",
    "tsx": "4.21.0"
  },
  "private": true,
  "scripts": {
    "build": "tsx ./esbuild.config.ts",
    "start:dev": "pnpm build && docker compose up"
  },
  "template": "docker",
  "templateVersion": "1.6.7"
}

Remark
I am now is looking for Cube code in the cubejs-server-code folder and want to find where exact problem is. Also, if it would be possibe want to make PR with fix. So, I will write here my updates in this research

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions