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

Add multiple DB schema support for bal persist proposal #1328

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

daneshk
Copy link
Member

@daneshk daneshk commented Jan 24, 2025

Purpose

Many database providers allow you to organize database tables into named groups. You can use this to make the logical structure of the data model easier to understand or to avoid naming collisions between tables. These groups are known as schemas in PostgreSQL, CockroachDB, and SQL Server.

This proposal introduces support for connecting to tables in multiple database schemas in bal persist. Right now, bal persist only supports connecting to tables in the default schema of the database. However, in real-world scenarios, tables are often distributed across multiple schemas in the database. When working with such existing databases, it is essential to provide a way to explicitly represent the schema of the table in the model file.

Goals

  • Allow users to connect to tables in multiple schemas in the database.
  • Provide a way to represent the schema of the table explicitly in the model file.
  • Maintain backward compatibility with the current bal persist API.

Approach

The current bal persist model supports the following annotations to map the model to a different table name in the database. For example, the following model will be mapped to the patients table in the database.

import ballerinax/persist.sql;

@sql:Name {
    value: "patients"
}
type Patient record {|
    readonly int id;
    string name;
    int age;
    string address;
    string phoneNumber;
    Gender gender;
	Appointment[] appointment;
|};

This proposal introduces a new annotation to map the model to a different schema in the database. For example, the following model will be mapped to the patients table in the healthcare schema in the database.

import ballerinax/persist.sql;

@sql:Schema {
    value: "healthcare"
}
@sql:Name {
    value: "patients"
}
type Patient record {|
    readonly int id;
    string name;
    int age;
    string address;
    string phoneNumber;
|};

Related issues: #1327

ThisaruGuruge
ThisaruGuruge previously approved these changes Jan 24, 2025
Copy link
Member

@ThisaruGuruge ThisaruGuruge left a comment

Choose a reason for hiding this comment

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

LGTM

@gimantha
Copy link
Contributor

LGTM, I have one concern though. The schema is used to group the tables or the part of the database. Usually most of the programs are dealing with just one schema. Interacting with multiple schema is a rare case. Can we provide an alternative to globally assign a schema for all the tables being used in persist module?

@daneshk
Copy link
Member Author

daneshk commented Jan 27, 2025

LGTM, I have one concern though. The schema is used to group the tables or the part of the database. Usually most of the programs are dealing with just one schema. Interacting with multiple schema is a rare case. Can we provide an alternative to globally assign a schema for all the tables being used in persist module?

@gimantha Thanks for the feedback. Shall we move this discussion to #1327, please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants