Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions dbml-homepage/docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ $ dbml2sql <path-to-dbml-file>
[-o|--out-file <output-filepath>]
```

## Convert a SQL file to DBML
## Convert a SQL file to DBML

To convert SQL to DBML file:

Expand All @@ -92,7 +92,7 @@ $ sql2dbml --mysql dump.sql -o mydatabase.dbml

```bash
$ sql2dbml <path-to-sql-file>
[--mysql|--postgres|--mssql|--postgres-legacy|--mysql-legacy|--mssql-legacy|--snowflake]
[--mysql|--postgres|--mssql|--postgres-legacy|--mysql-legacy|--mssql-legacy|--snowflake|--oracle]
[-o|--out-file <output-filepath>]
```

Expand Down
4 changes: 2 additions & 2 deletions dbml-homepage/docs/js-module/core.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const { importer } = require('@dbml/core');

* **Arguments:**
* ```{string} str```
* ```{'mysql'|'mysqlLegacy'|'postgres'|'postgresLegacy'|'dbml'|'schemarb'|'mssql'|'mssqlLegacy'|'snowflake'|'json'} format```
* ```{'mysql'|'mysqlLegacy'|'postgres'|'postgresLegacy'|'dbml'|'schemarb'|'mssql'|'mssqlLegacy'|'snowflake'|'json'|'oracle'} format```

* **Returns:**
* ```{string} DBML```
Expand Down Expand Up @@ -120,7 +120,7 @@ const parser = new Parser();

* **Arguments:**
* ```{string} str```
* ```{'mysql'|'mysqlLegacy'|'postgres'|'postgresLegacy'|'dbml'|'schemarb'|'mssql'|'mssqlLegacy'|'snowflake'|'json'|'dbmlv2'} format```
* ```{'mysql'|'mysqlLegacy'|'postgres'|'postgresLegacy'|'dbml'|'schemarb'|'mssql'|'mssqlLegacy'|'snowflake'|'json'|'dbmlv2'|'oracle'} format```

* **Returns:** ```Database``` object

Expand Down
4 changes: 2 additions & 2 deletions packages/dbml-cli/__tests__/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ describe('@dbml/cli', () => {
const expectStdout = fs.readFileSync(path.join(dirName, './stdout.txt'), 'utf-8');
const actualStdout = stripAnsi(stdout);

// folder name contains `syntax-error`
if (path.basename(dirName).includes('syntax-error')) {
// folder name contains `error`
if (path.basename(dirName).includes('error')) {
expect(actualStdout).toContain(expectStdout);
} else {
expect(actualStdout).toBe(expectStdout);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE Users (
id TEXT PRIMARY KEY
);

ALTER TABLE Users
ADD CONSTRAINT unq UNIQUE (name);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"args": [
"./in-files/schema.sql",
"--oracle",
"-o",
"./out-files/schema.dbml"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ERROR:
You have a syntax error at "schema.sql" line 1 column 1. Column "name" do not exist in table "Users"

A complete log can be found in:
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE Users
ADD CONSTRAINT unq UNIQUE (id);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"args": [
"./in-files/schema.sql",
"--oracle",
"-o",
"./out-files/schema.dbml"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ERROR:
You have a syntax error at "schema.sql" line 1 column 13. Table "Users" not found

A complete log can be found in:
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE Users (
id TEXT
);

COMMENT ON COLUMN Users.name IS 'fullname';

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"args": [
"./in-files/schema.sql",
"--oracle",
"-o",
"./out-files/schema.dbml"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ERROR:
You have a syntax error at "schema.sql" line 5 column 19. Column "name" not found in table "Users"

A complete log can be found in:
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE Users (
id TEXT
);

COMMENT ON COLUMN Posts.title IS 'title';
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"args": [
"./in-files/schema.sql",
"--oracle",
"-o",
"./out-files/schema.dbml"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ERROR:
You have a syntax error at "schema.sql" line 5 column 19. Table "Posts" not found

A complete log can be found in:
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE UNIQUE INDEX idx ON Users(id);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"args": [
"./in-files/schema.sql",
"--oracle",
"-o",
"./out-files/schema.dbml"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ERROR:
You have a syntax error at "schema.sql" line 1 column 28. Table Users not found

A complete log can be found in:
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE Users (
id TEXT
);

COMMENT ON TABLE Posts IS 'id';
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"args": [
"./in-files/schema.sql",
"--oracle",
"-o",
"./out-files/schema.dbml"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ERROR:
You have a syntax error at "schema.sql" line 5 column 18. Table "Posts" not found

A complete log can be found in:
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
Table "departments" {
"dept_id" NUMBER(10) [pk]
"dept_name" VARCHAR2(100)
"location_id" NUMBER(10)
"manager_id" NUMBER(10)
}

Table "locations" {
"location_id" NUMBER(10) [pk]
"city" VARCHAR2(100)
"country_code" CHAR(2)
}

Table "countries" {
"country_code" CHAR(2) [pk]
"country_name" VARCHAR2(100)
}

Table "employees" {
"emp_id" NUMBER(10) [pk]
"emp_name" VARCHAR2(100)
"department_id" NUMBER(10)
"manager_id" NUMBER(10)
"hire_date" DATE
"location_id" NUMBER(10)
}

Table "projects" {
"project_id" NUMBER(10) [pk]
"project_name" VARCHAR2(200)
"dept_id" NUMBER(10)
"lead_emp_id" NUMBER(10)
"backup_emp_id" NUMBER(10)
}

Table "assignments" {
"assignment_id" NUMBER(10) [pk]
"emp_id" NUMBER(10)
"project_id" NUMBER(10)
"start_date" DATE
}

Table "project_tasks" {
"project_id" NUMBER(10)
"task_id" NUMBER(10)
"task_name" VARCHAR2(200)
"assigned_emp_id" NUMBER(10)

Indexes {
(project_id, task_id) [pk]
}
}

Table "task_hours" {
"hour_id" NUMBER(10) [pk]
"project_id" NUMBER(10)
"task_id" NUMBER(10)
"hours_worked" NUMBER(5,2)
}

Ref "fk_emp_dept":"departments"."dept_id" < "employees"."department_id"

Ref "fk_dept_location":"locations"."location_id" < "departments"."location_id"

Ref "fk_loc_country":"countries"."country_code" < "locations"."country_code"

Ref "fk_emp_manager":"employees"."emp_id" < "employees"."manager_id"

Ref "fk_dept_manager":"employees"."emp_id" < "departments"."manager_id"

Ref:"departments"."dept_id" < "projects"."dept_id"

Ref:"employees"."emp_id" < "assignments"."emp_id"

Ref:"projects"."project_id" < "assignments"."project_id"

Ref "fk_task_hours_project_task":"project_tasks".("project_id", "task_id") < "task_hours".("project_id", "task_id")

Ref "fk_proj_lead_emp":"employees"."emp_id" < "projects"."lead_emp_id"

Ref "fk_proj_backup_emp":"employees"."emp_id" < "projects"."backup_emp_id"

Ref "fk_task_assigned_emp":"employees"."emp_id" < "project_tasks"."assigned_emp_id"
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
-- Oracle SQL ALTER TABLE ADD FOREIGN KEY Constraint Test Cases
-- Test setup: Create base tables with primary keys
CREATE TABLE departments (
dept_id NUMBER(10) PRIMARY KEY,
dept_name VARCHAR2(100),
location_id NUMBER(10),
manager_id NUMBER(10)
);

CREATE TABLE locations (
location_id NUMBER(10) PRIMARY KEY,
city VARCHAR2(100),
country_code CHAR(2)
);

CREATE TABLE countries (
country_code CHAR(2) PRIMARY KEY,
country_name VARCHAR2(100)
);

CREATE TABLE employees (
emp_id NUMBER(10) PRIMARY KEY,
emp_name VARCHAR2(100),
department_id NUMBER(10),
manager_id NUMBER(10),
hire_date DATE,
location_id NUMBER(10)
);

CREATE TABLE projects (
project_id NUMBER(10) PRIMARY KEY,
project_name VARCHAR2(200),
dept_id NUMBER(10),
lead_emp_id NUMBER(10),
backup_emp_id NUMBER(10)
);

CREATE TABLE assignments (
assignment_id NUMBER(10) PRIMARY KEY,
emp_id NUMBER(10),
project_id NUMBER(10),
start_date DATE
);

-- Table with composite primary key
CREATE TABLE project_tasks (
project_id NUMBER(10),
task_id NUMBER(10),
task_name VARCHAR2(200),
assigned_emp_id NUMBER(10),
PRIMARY KEY (project_id, task_id)
);

CREATE TABLE task_hours (
hour_id NUMBER(10) PRIMARY KEY,
project_id NUMBER(10),
task_id NUMBER(10),
hours_worked NUMBER(5,2)
);

-- ============================================
-- NAMED FOREIGN KEY CONSTRAINTS - BASIC
-- ============================================

-- Simple single-column FK with constraint name
ALTER TABLE employees
ADD CONSTRAINT fk_emp_dept
FOREIGN KEY (department_id)
REFERENCES departments(dept_id);

-- Single-column FK referencing different table
ALTER TABLE departments
ADD CONSTRAINT fk_dept_location
FOREIGN KEY (location_id)
REFERENCES locations(location_id);

-- FK with explicit column name in referenced table
ALTER TABLE locations
ADD CONSTRAINT fk_loc_country
FOREIGN KEY (country_code)
REFERENCES countries(country_code);

-- Self-referencing FK (hierarchical)
ALTER TABLE employees
ADD CONSTRAINT fk_emp_manager
FOREIGN KEY (manager_id)
REFERENCES employees(emp_id);

-- Another self-referencing FK on different table
ALTER TABLE departments
ADD CONSTRAINT fk_dept_manager
FOREIGN KEY (manager_id)
REFERENCES employees(emp_id);

-- ============================================
-- UNNAMED FOREIGN KEY CONSTRAINTS
-- ============================================

-- FK without explicit constraint name (Oracle auto-generates)
ALTER TABLE projects
ADD FOREIGN KEY (dept_id)
REFERENCES departments(dept_id);

-- Another unnamed FK
ALTER TABLE assignments
ADD FOREIGN KEY (emp_id)
REFERENCES employees(emp_id);

-- Unnamed FK with different columns
ALTER TABLE assignments
ADD FOREIGN KEY (project_id)
REFERENCES projects(project_id);

-- ============================================
-- COMPOSITE FOREIGN KEYS (Multiple Columns)
-- ============================================

-- Two-column composite FK
ALTER TABLE task_hours
ADD CONSTRAINT fk_task_hours_project_task
FOREIGN KEY (project_id, task_id)
REFERENCES project_tasks(project_id, task_id);

-- ============================================
-- FOREIGN KEYS WITH ON DELETE ACTIONS
-- ============================================

-- ON DELETE CASCADE - delete child records when parent is deleted
ALTER TABLE projects
ADD CONSTRAINT fk_proj_lead_emp
FOREIGN KEY (lead_emp_id)
REFERENCES employees(emp_id)
ON DELETE CASCADE;

-- ON DELETE SET NULL - set FK to NULL when parent is deleted
ALTER TABLE projects
ADD CONSTRAINT fk_proj_backup_emp
FOREIGN KEY (backup_emp_id)
REFERENCES employees(emp_id)
ON DELETE SET NULL;

-- ON DELETE CASCADE with named constraint
ALTER TABLE project_tasks
ADD CONSTRAINT fk_task_assigned_emp
FOREIGN KEY (assigned_emp_id)
REFERENCES employees(emp_id)
ON DELETE CASCADE;
Loading
Loading