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

Allow user to configure default MotherDuck database in postgresql.conf #470

Merged
merged 8 commits into from
Dec 4, 2024

Conversation

naoyak
Copy link
Contributor

@naoyak naoyak commented Dec 3, 2024

Fixes #459

Allow the user to configure a different default MotherDuck database from my_db in postgresql.conf. This affords flexibility because there is currently no user-facing API to change the internal "default" DB in MotherDuck.

Copy link
Collaborator

@JelteF JelteF left a comment

Choose a reason for hiding this comment

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

Thanks a lot for the PR!

Comment on lines 167 to 171
if (duckdb_motherduck_default_database[0] == '\0') {
default_dbname = db_manager.GetDefaultDatabase(context);
} else {
default_dbname = duckdb_motherduck_default_database;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Instead of overriding the default database that motherduck sets, let's instead make motherduck set the correct default database by providing the database in the connection string:

connection_string = psprintf("md:%s?motherduck_token=%s", duckdb_motherduck_default_database, duckdb_motherduck_token);

For that you need to escape the database name though using URI escaping. I think the following function should work for that:

char *
uri_escape(const char *input)
{
    StringInfoData buf;
    initStringInfo(&buf);

    while (*str)
    {
        char c = *str++;
        if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '-' || c == '_' || c == '.' || c == '~')
        {
            appendStringInfoChar(&buf, c);
        }
        else
        {
            appendStringInfo(&buf, "%%%02X", (unsigned char)c);
        }
    }

    return buf.data;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the tip! I didn't realize you could just stick the db name in the connection string, clean solution.

Is it ok that I just stuck the uri_escape function in the same file?

@naoyak naoyak force-pushed the config-md-default-db branch 2 times, most recently from 619aef4 to c13448d Compare December 3, 2024 22:06
@naoyak naoyak requested a review from JelteF December 3, 2024 22:07
@naoyak naoyak force-pushed the config-md-default-db branch from c13448d to 8c2d0f2 Compare December 4, 2024 09:00
@JelteF
Copy link
Collaborator

JelteF commented Dec 4, 2024

Thanks a lot for your work on this. I played a bit with your changes and realized that duckdb tables were not being cleaned up when changing the default database. So I implemented a fix for that. I also updated did some small other tweaks, and updated the docs in some more places.

@JelteF JelteF requested a review from Y-- December 4, 2024 11:32
Copy link
Collaborator

@Y-- Y-- left a comment

Choose a reason for hiding this comment

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

LGTM!

@naoyak naoyak force-pushed the config-md-default-db branch from 0812a06 to e7643a6 Compare December 4, 2024 11:47
@JelteF JelteF enabled auto-merge (squash) December 4, 2024 12:20
@JelteF JelteF merged commit dff3a16 into duckdb:main Dec 4, 2024
5 checks passed
@naoyak naoyak deleted the config-md-default-db branch December 4, 2024 17:25
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