Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Changed
- [Fix an SQL script generation order issue when there are multiple associations](https://github.com/ballerina-platform/ballerina-library/issues/7921)

## [1.4.0] - 2024-08-20

### Changed
- Fix an issue where client API is still generated even if all entities contain unsupported field(s)
- Fix an issue where unique indexes are declared twice in script.sql in one-to-one associations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1094,6 +1094,18 @@ public void testGenerateEntitiesWithMultipleDBSchemas() {
assertGeneratedSources(subDir);
}

@Test(enabled = true)
@Description("The model has multiple tables with different relations and test table creation order")
public void testGenerateSQLScriptWithRelations() {
String subDir = "tool_test_generate_114";
updateOutputBallerinaToml(subDir);
executeGenerateCommand(subDir, "--datastore", "mysql", "--module", "entities");
executeGenerateCommand(subDir, "--datastore", "mssql", "--module", "mssql_entities");
executeGenerateCommand(subDir, "--datastore", "h2", "--module", "h2_entities");
executeGenerateCommand(subDir, "--datastore", "postgresql", "--module", "postgresql_entities");
assertGeneratedSources(subDir);
}

private void updateOutputBallerinaToml(String fileName) {
String tomlFileName = "Ballerina.toml";
Path filePath = Paths.get("src", "test", "resources", "test-src", "output", fileName, tomlFileName);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
org = "foo"
name = "tool_test_generate_114"
version = "0.1.0"
distribution = "2201.12.0"

[build-options]
observabilityIncluded = true
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) 2025 WSO2 LLC. (http://www.wso2.com).
//
// WSO2 LLC. licenses this file to you under the Apache License,
// Version 2.0 (the "License"); you may not use this file except
// in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

import ballerina/io;

public function main() {
io:println("Hello, World!");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Copyright (c) 2025 WSO2 LLC. (http://www.wso2.org).
//
// WSO2 LLC. licenses this file to you under the Apache License,
// Version 2.0 (the "License"); you may not use this file except
// in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

import ballerina/persist as _;
import ballerinax/persist.sql;

@sql:Name {value: "codesystems"}
public type CodeSystem record {|
@sql:Generated
readonly int codeSystemId;
string id;
string url;
string version;
string name;
string title;
string status;
string date;
string publisher;
byte[] codeSystem;
Concept[] concepts;
|};

@sql:Name {value: "concepts"}
public type Concept record {|
@sql:Generated
readonly int conceptId;
string code;
byte[] concept;
int? parentConceptId;
CodeSystem codeSystem;
ValueSetComposeIncludeConcept[] valuesetcomposeincludeconcept;
|};

@sql:Name {value: "valuesets"}
public type ValueSet record {|
@sql:Generated
readonly int valueSetId;
string id;
string url;
string version;
string name;
string title;
string status;
string date;
string publisher;
byte[] valueSet;
ValueSetComposeInclude[] composes;
ValueSetComposeIncludeValueSet[] conceptsInValueSetConcepts;
|};

@sql:Name {value: "valueset_compose_includes"}
public type ValueSetComposeInclude record {|
@sql:Generated
readonly int valueSetComposeIncludeId;
boolean systemFlag;
boolean valueSetFlag;
boolean conceptFlag;
ValueSet valueSet;
ValueSetComposeIncludeValueSet[] valuesetcomposeincludevalueset;
ValueSetComposeIncludeConcept[] valuesetcomposeincludeconcept;
int? codeSystemId;
|};

@sql:Name {value: "valueset_compose_include_value_sets"}
public type ValueSetComposeIncludeValueSet record {|
@sql:Generated
readonly int valueSetComposeIncludeValueSetId;
ValueSetComposeInclude valuesetCompose;
ValueSet valueset;
|};

@sql:Name {value: "valueset_compose_include_concepts"}
public type ValueSetComposeIncludeConcept record {|
@sql:Generated
readonly int valueSetComposeIncludeConceptId;
ValueSetComposeInclude valuesetCompose;
Concept concept;
|};
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,8 @@

DROP TABLE IF EXISTS `Workspace`;
DROP TABLE IF EXISTS `Employee`;
DROP TABLE IF EXISTS `Building`;
DROP TABLE IF EXISTS `Department`;

CREATE TABLE `Department` (
`deptNo` VARCHAR(10) NOT NULL,
`deptName` VARCHAR(191) NOT NULL,
PRIMARY KEY(`deptNo`)
);
DROP TABLE IF EXISTS `Building`;

CREATE TABLE `Building` (
`buildingCode` VARCHAR(191) NOT NULL,
Expand All @@ -23,6 +17,12 @@ CREATE TABLE `Building` (
PRIMARY KEY(`buildingCode`)
);

CREATE TABLE `Department` (
`deptNo` VARCHAR(10) NOT NULL,
`deptName` VARCHAR(191) NOT NULL,
PRIMARY KEY(`deptNo`)
);

CREATE TABLE `Employee` (
`empNo` VARCHAR(191) NOT NULL,
`firstName` VARCHAR(191) NOT NULL,
Expand All @@ -44,3 +44,5 @@ CREATE TABLE `Workspace` (
FOREIGN KEY(`employeeEmpNo`) REFERENCES `Employee`(`empNo`),
PRIMARY KEY(`workspaceId`)
);


Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ CREATE TABLE "Car" (
"id" INT NOT NULL,
"make" VARCHAR(191) NOT NULL,
"model" VARCHAR(191) NOT NULL,
"ownerId" INT NOT NULL,
FOREIGN KEY("ownerId") REFERENCES "User"("id"),
PRIMARY KEY("id")
);`);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ CREATE TABLE `Car` (
`id` INT NOT NULL,
`make` VARCHAR(191) NOT NULL,
`model` VARCHAR(191) NOT NULL,
`ownerId` INT NOT NULL,
FOREIGN KEY(`ownerId`) REFERENCES `User`(`id`),
PRIMARY KEY(`id`)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ CREATE TABLE "Car" (
"id" INT NOT NULL,
"make" VARCHAR(191) NOT NULL,
"model" VARCHAR(191) NOT NULL,
"ownerId" INT NOT NULL,
FOREIGN KEY("ownerId") REFERENCES "User"("id"),
PRIMARY KEY("id")
);`);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ CREATE TABLE "Car" (
"id" INT NOT NULL,
"make" VARCHAR(191) NOT NULL,
"model" VARCHAR(191) NOT NULL,
"ownerId" INT NOT NULL,
FOREIGN KEY("ownerId") REFERENCES "User"("id"),
PRIMARY KEY("id")
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ CREATE TABLE "Car" (
"id" INT NOT NULL,
"make" VARCHAR(191) NOT NULL,
"model" VARCHAR(191) NOT NULL,
"ownerId" INT NOT NULL,
FOREIGN KEY("ownerId") REFERENCES "User"("id"),
PRIMARY KEY("id")
);`);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ CREATE TABLE [Car] (
[id] INT NOT NULL,
[make] VARCHAR(191) NOT NULL,
[model] VARCHAR(191) NOT NULL,
[ownerId] INT NOT NULL,
FOREIGN KEY([ownerId]) REFERENCES [User]([id]),
PRIMARY KEY([id])
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ CREATE TABLE "Car" (
"id" INT NOT NULL,
"make" VARCHAR(191) NOT NULL,
"model" VARCHAR(191) NOT NULL,
"ownerId" INT NOT NULL,
FOREIGN KEY("ownerId") REFERENCES "User"("id"),
PRIMARY KEY("id")
);`);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ CREATE TABLE "Car" (
"id" INT NOT NULL,
"make" VARCHAR(191) NOT NULL,
"model" VARCHAR(191) NOT NULL,
"ownerId" INT NOT NULL,
FOREIGN KEY("ownerId") REFERENCES "User"("id"),
PRIMARY KEY("id")
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@

DROP TABLE IF EXISTS `Car2`;
DROP TABLE IF EXISTS `Car`;
DROP TABLE IF EXISTS `User`;
DROP TABLE IF EXISTS `User2`;
DROP TABLE IF EXISTS `User`;

CREATE TABLE `User2` (
CREATE TABLE `User` (
`id` INT NOT NULL,
`nic` VARCHAR(191) NOT NULL,
`name` VARCHAR(191) NOT NULL,
`nic` VARCHAR(191) NOT NULL,
`salary` DECIMAL(65,30),
PRIMARY KEY(`id`,`nic`)
PRIMARY KEY(`id`)
);

CREATE TABLE `User` (
CREATE TABLE `User2` (
`id` INT NOT NULL,
`name` VARCHAR(191) NOT NULL,
`nic` VARCHAR(191) NOT NULL,
`name` VARCHAR(191) NOT NULL,
`salary` DECIMAL(65,30),
PRIMARY KEY(`id`)
PRIMARY KEY(`id`,`nic`)
);

CREATE TABLE `Car` (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@

DROP TABLE IF EXISTS "Car2";
DROP TABLE IF EXISTS "Car";
DROP TABLE IF EXISTS "User";
DROP TABLE IF EXISTS "User2";
DROP TABLE IF EXISTS "User";

CREATE TABLE "User2" (
CREATE TABLE "User" (
"id" INT NOT NULL,
"nic" VARCHAR(191) NOT NULL,
"name" VARCHAR(191) NOT NULL,
"nic" VARCHAR(191) NOT NULL,
"salary" DECIMAL(65,30),
PRIMARY KEY("id","nic")
PRIMARY KEY("id")
);

CREATE TABLE "User" (
CREATE TABLE "User2" (
"id" INT NOT NULL,
"name" VARCHAR(191) NOT NULL,
"nic" VARCHAR(191) NOT NULL,
"name" VARCHAR(191) NOT NULL,
"salary" DECIMAL(65,30),
PRIMARY KEY("id")
PRIMARY KEY("id","nic")
);

CREATE TABLE "Car" (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@

DROP TABLE IF EXISTS [Car2];
DROP TABLE IF EXISTS [Car];
DROP TABLE IF EXISTS [User];
DROP TABLE IF EXISTS [User2];
DROP TABLE IF EXISTS [User];

CREATE TABLE [User2] (
CREATE TABLE [User] (
[id] INT NOT NULL,
[nic] VARCHAR(191) NOT NULL,
[name] VARCHAR(191) NOT NULL,
[nic] VARCHAR(191) NOT NULL,
[salary] DECIMAL(38,30),
PRIMARY KEY([id],[nic])
PRIMARY KEY([id])
);

CREATE TABLE [User] (
CREATE TABLE [User2] (
[id] INT NOT NULL,
[name] VARCHAR(191) NOT NULL,
[nic] VARCHAR(191) NOT NULL,
[name] VARCHAR(191) NOT NULL,
[salary] DECIMAL(38,30),
PRIMARY KEY([id])
PRIMARY KEY([id],[nic])
);

CREATE TABLE [Car] (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@

DROP TABLE IF EXISTS "Car2";
DROP TABLE IF EXISTS "Car";
DROP TABLE IF EXISTS "User";
DROP TABLE IF EXISTS "User2";
DROP TABLE IF EXISTS "User";

CREATE TABLE "User2" (
CREATE TABLE "User" (
"id" INT NOT NULL,
"nic" VARCHAR(191) NOT NULL,
"name" VARCHAR(191) NOT NULL,
"nic" VARCHAR(191) NOT NULL,
"salary" DECIMAL(65,30),
PRIMARY KEY("id","nic")
PRIMARY KEY("id")
);

CREATE TABLE "User" (
CREATE TABLE "User2" (
"id" INT NOT NULL,
"name" VARCHAR(191) NOT NULL,
"nic" VARCHAR(191) NOT NULL,
"name" VARCHAR(191) NOT NULL,
"salary" DECIMAL(65,30),
PRIMARY KEY("id")
PRIMARY KEY("id","nic")
);

CREATE TABLE "Car" (
Expand Down
Loading
Loading