Exemple #1
0
 def test_str_with_changes(self):
     initial = 'dbname=foo host=bar'
     expected = 'dbname=foo user=baz host=blah'
     cs = ConnectionString(initial)
     cs.host = 'blah'
     cs.user = '******'
     self.assertEqual(expected, str(cs))
def get_all_cluster_nodes(con):
    """Return a list of all Nodes in the cluster.

    node.is_master will be None, as this boolean doesn't make sense
    in the context of a cluster rather than a single replication set.
    """
    if not slony_installed(con):
        return []
    nodes = _get_nodes(con, """
        SELECT DISTINCT
            pa_server AS node_id,
            'node' || pa_server || '_node',
            pa_conninfo AS connection_string,
            NULL
        FROM _sl.sl_path
        ORDER BY node_id
        """)
    if not nodes:
        # There are no subscriptions yet, so no paths. Generate the
        # master Node.
        cur = con.cursor()
        cur.execute("SELECT no_id from _sl.sl_node")
        node_ids = [row[0] for row in cur.fetchall()]
        if len(node_ids) == 0:
            return []
        assert len(node_ids) == 1, "Multiple nodes but no paths."
        master_node_id = node_ids[0]
        master_connection_string = ConnectionString(
            config.database.rw_main_master)
        master_connection_string.user = '******'
        return [Node(
            master_node_id, 'node%d_node' % master_node_id,
            master_connection_string, True)]
    return nodes
def connect_string(user=None, dbname=None):
    """Return a PostgreSQL connection string.

    Allows you to pass the generated connection details to external
    programs like pg_dump or embed in slonik scripts.
    """
    # We must connect to the read-write DB here, so we use rw_main_master
    # directly.
    from lp.services.database.postgresql import ConnectionString
    con_str = ConnectionString(dbconfig.rw_main_master)
    if user is not None:
        con_str.user = user
    if dbname is not None:
        con_str.dbname = dbname
    return str(con_str)
def connect_string(user=None, dbname=None):
    """Return a PostgreSQL connection string.

    Allows you to pass the generated connection details to external
    programs like pg_dump or embed in slonik scripts.
    """
    # We must connect to the read-write DB here, so we use rw_main_master
    # directly.
    from lp.services.database.postgresql import ConnectionString
    con_str = ConnectionString(dbconfig.rw_main_master)
    if user is not None:
        con_str.user = user
    if dbname is not None:
        con_str.dbname = dbname
    return str(con_str)
Exemple #5
0
    def raw_connect(self):
        if config.launchpad_session.database is not None:
            dsn = ConnectionString(config.launchpad_session.database)
            dsn.user = config.launchpad_session.dbuser
            self._dsn = str(dsn)
        else:
            # This is fallback code for old config files. It can be
            # removed when all live configs have been updated to use the
            # 'database' setting instead of 'dbname' + 'dbhost' settings.
            self._dsn = 'dbname=%s user=%s' % (config.launchpad_session.dbname,
                                               config.launchpad_session.dbuser)
            if config.launchpad_session.dbhost:
                self._dsn += ' host=%s' % config.launchpad_session.dbhost

        flags = _get_dirty_commit_flags()
        raw_connection = super(LaunchpadSessionDatabase, self).raw_connect()
        if safe_hasattr(raw_connection, 'auto_close'):
            raw_connection.auto_close = False
        raw_connection.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
        _reset_dirty_commit_flags(*flags)
        return raw_connection
    def raw_connect(self):
        if config.launchpad_session.database is not None:
            dsn = ConnectionString(config.launchpad_session.database)
            dsn.user = config.launchpad_session.dbuser
            self._dsn = str(dsn)
        else:
            # This is fallback code for old config files. It can be
            # removed when all live configs have been updated to use the
            # 'database' setting instead of 'dbname' + 'dbhost' settings.
            self._dsn = 'dbname=%s user=%s' % (
                config.launchpad_session.dbname,
                config.launchpad_session.dbuser)
            if config.launchpad_session.dbhost:
                self._dsn += ' host=%s' % config.launchpad_session.dbhost

        flags = _get_dirty_commit_flags()
        raw_connection = super(LaunchpadSessionDatabase, self).raw_connect()
        if safe_hasattr(raw_connection, 'auto_close'):
            raw_connection.auto_close = False
        raw_connection.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
        _reset_dirty_commit_flags(*flags)
        return raw_connection
def get_all_cluster_nodes(con):
    """Return a list of all Nodes in the cluster.

    node.is_master will be None, as this boolean doesn't make sense
    in the context of a cluster rather than a single replication set.
    """
    if not slony_installed(con):
        return []
    nodes = _get_nodes(
        con, """
        SELECT DISTINCT
            pa_server AS node_id,
            'node' || pa_server || '_node',
            pa_conninfo AS connection_string,
            NULL
        FROM _sl.sl_path
        ORDER BY node_id
        """)
    if not nodes:
        # There are no subscriptions yet, so no paths. Generate the
        # master Node.
        cur = con.cursor()
        cur.execute("SELECT no_id from _sl.sl_node")
        node_ids = [row[0] for row in cur.fetchall()]
        if len(node_ids) == 0:
            return []
        assert len(node_ids) == 1, "Multiple nodes but no paths."
        master_node_id = node_ids[0]
        master_connection_string = ConnectionString(
            config.database.rw_main_master)
        master_connection_string.user = '******'
        return [
            Node(master_node_id, 'node%d_node' % master_node_id,
                 master_connection_string, True)
        ]
    return nodes