Skip to content

Conversation

@nduthoit
Copy link
Contributor

@nduthoit nduthoit commented Mar 16, 2017

Allows for the use of database URL environment variables with no database name specified. This will in turn result in less environment variables needed to be set when there are many shards per database node.

For example imagine an app with 1 primary database and 8 shards distributed over 3 database servers:

DB_NODE_001_URL=postgres://user:passwd@node001.company.org:5432
DB_SHARD_NODE_001_URL=postgres://user:passwd@shardnode001.company.org:5432
DB_SHARD_NODE_002_URL=postgres://user:passwd@shardnode002.company.org:5432
    {
        'unsharded_databases': [
            {
                'name': 'DB01',
                'environment_variable': 'DB_NODE_001_URL',
                'default_database_url': 'postgres:://...',
                'database_name': 'db01'
            }
        ],
        'sharded_databases': [
            {
                'name': 'SHARD_01',
                'environment_variable': 'DB_SHARD_NODE_001_URL',
                'default_database_url': 'postgres:://...',
                'database_name': 'shard01',
                'shard_group': 'default',
            }, {
                'name': 'SHARD_02',
                'environment_variable': 'DB_SHARD_NODE_001_URL',
                'default_database_url': 'postgres:://...',
                'database_name': 'shard02',
                'shard_group': 'default',
            }, {
                'name': 'SHARD_03',
                'environment_variable': 'DB_SHARD_NODE_001_URL',
                'default_database_url': 'postgres:://...',
                'database_name': 'shard03',
                'shard_group': 'default',
            }, {
                'name': 'SHARD_04',
                'environment_variable': 'DB_SHARD_NODE_001_URL',
                'default_database_url': 'postgres:://...',
                'database_name': 'shard04',
                'shard_group': 'default',
            }, {
                'name': 'SHARD_05',
                'environment_variable': 'DB_SHARD_NODE_002_URL',
                'default_database_url': 'postgres:://...',
                'database_name': 'shard05,
                'shard_group': 'default',
            }, {
                'name': 'SHARD_06',
                'environment_variable': 'DB_SHARD_NODE_002_URL',
                'default_database_url': 'postgres:://...',
                'database_name': 'shard06',
                'shard_group': 'default',
            }, {
                'name': 'SHARD_07',
                'environment_variable': 'DB_SHARD_NODE_002_URL',
                'default_database_url': 'postgres:://...',
                'database_name': 'shard07',
                'shard_group': 'default',
            }, {
                'name': 'SHARD_08',
                'environment_variable': 'DB_SHARD_NODE_002_URL',
                'default_database_url': 'postgres:://...',
                'database_name': 'shard08',
                'shard_group': 'default',
            },
        ]
    }

db_config['TEST'] = db_config.get('TEST', {})
db_config['SHARD_GROUP'] = shard_group

if not db_config['NAME'] and database_name is not None:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could also override the existing value of db_config['NAME'] even if it is set by replacing this with:

if database_name is not None

Copy link
Owner

Choose a reason for hiding this comment

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

Hmmm I agree that if there's a precedence it should be the name you set, rather than the one in the URL.

@coveralls
Copy link

coveralls commented Mar 16, 2017

Coverage Status

Coverage decreased (-0.1%) to 89.717% when pulling f32d5c3 on nduthoit:optional-database-name-in-url into eb6d9ab on JBKahn:master.

@JBKahn
Copy link
Owner

JBKahn commented Mar 16, 2017

@JBKahn
Copy link
Owner

JBKahn commented Mar 17, 2017

What happens if you don't have a name in the args or the URL?

@coveralls
Copy link

coveralls commented Mar 17, 2017

Coverage Status

Coverage increased (+0.03%) to 89.846% when pulling c95406a on nduthoit:optional-database-name-in-url into eb6d9ab on JBKahn:master.

2 similar comments
@coveralls
Copy link

Coverage Status

Coverage increased (+0.03%) to 89.846% when pulling c95406a on nduthoit:optional-database-name-in-url into eb6d9ab on JBKahn:master.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.03%) to 89.846% when pulling c95406a on nduthoit:optional-database-name-in-url into eb6d9ab on JBKahn:master.

@nduthoit
Copy link
Contributor Author

nduthoit commented Mar 17, 2017

@JBKahn same as what would happen now if it's not in the URL: it wouldn't be set

I didn't want to change the existing behaviour which doesn't validate the URL (other than using dj-database-url)

Copy link
Owner

@JBKahn JBKahn left a comment

Choose a reason for hiding this comment

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

I'll release it this weekend and update the changelog and all that.

@JBKahn JBKahn merged commit 58d633b into JBKahn:master Mar 19, 2017
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