|
| 1 | +# coding: utf-8 |
| 2 | + |
| 3 | +from six import raise_from |
| 4 | + |
| 5 | +from .defaults import default_dbname, default_username |
| 6 | +from .exceptions import CatchUpException |
| 7 | +from .utils import pg_version_ge |
| 8 | + |
| 9 | + |
| 10 | +class Publication(object): |
| 11 | + def __init__(self, pubname, node, tables=None, dbname=None, username=None): |
| 12 | + """ |
| 13 | + Constructor |
| 14 | +
|
| 15 | + Args: |
| 16 | + pubname: publication name |
| 17 | + node: publisher's node |
| 18 | + tables: tables list or None for all tables |
| 19 | + dbname: database name used to connect and perform subscription |
| 20 | + username: username used to connect to the database |
| 21 | + """ |
| 22 | + self.name = pubname |
| 23 | + self.node = node |
| 24 | + self.dbname = dbname or default_dbname() |
| 25 | + self.username = username or default_username() |
| 26 | + |
| 27 | + # create publication in database |
| 28 | + t = 'table ' + ', '.join(tables) if tables else 'all tables' |
| 29 | + query = "create publication {} for {}" |
| 30 | + node.safe_psql(query.format(pubname, t), |
| 31 | + dbname=dbname, |
| 32 | + username=username) |
| 33 | + |
| 34 | + def close(self, dbname=None, username=None): |
| 35 | + """ |
| 36 | + Drop publication |
| 37 | + """ |
| 38 | + self.node.safe_psql("drop publication {}".format(self.name), |
| 39 | + dbname=dbname, username=username) |
| 40 | + |
| 41 | + def add_tables(self, tables, dbname=None, username=None): |
| 42 | + """ |
| 43 | + Add tables |
| 44 | +
|
| 45 | + Args: |
| 46 | + tables: a list of tables to add to the publication |
| 47 | + """ |
| 48 | + if not tables: |
| 49 | + raise ValueError("Tables list is empty") |
| 50 | + |
| 51 | + query = "alter publication {} add table {}" |
| 52 | + self.node.safe_psql(query.format(self.name, ', '.join(tables)), |
| 53 | + dbname=dbname or self.dbname, |
| 54 | + username=username or self.username) |
| 55 | + |
| 56 | + |
| 57 | +class Subscription(object): |
| 58 | + def __init__(self, |
| 59 | + subname, |
| 60 | + node, |
| 61 | + publication, |
| 62 | + dbname=None, |
| 63 | + username=None, |
| 64 | + **kwargs): |
| 65 | + """ |
| 66 | + Constructor |
| 67 | +
|
| 68 | + Args: |
| 69 | + subname: subscription name |
| 70 | + node: subscriber's node |
| 71 | + publication: Publication object we are subscribing to |
| 72 | + dbname: database name used to connect and perform subscription |
| 73 | + username: username used to connect to the database |
| 74 | + **kwargs: subscription parameters (see CREATE SUBSCRIPTION |
| 75 | + in PostgreSQL documentation for more information) |
| 76 | + """ |
| 77 | + self.name = subname |
| 78 | + self.node = node |
| 79 | + self.pub = publication |
| 80 | + |
| 81 | + # connection info |
| 82 | + conninfo = ( |
| 83 | + u"dbname={} user={} host={} port={}" |
| 84 | + ).format(self.pub.dbname, |
| 85 | + self.pub.username, |
| 86 | + self.pub.node.host, |
| 87 | + self.pub.node.port) |
| 88 | + |
| 89 | + query = ( |
| 90 | + "create subscription {} connection '{}' publication {}" |
| 91 | + ).format(subname, conninfo, self.pub.name) |
| 92 | + |
| 93 | + # additional parameters |
| 94 | + if kwargs: |
| 95 | + params = ','.join('{}={}'.format(k, v) for k, v in kwargs.iteritems()) |
| 96 | + query += " with ({})".format(params) |
| 97 | + |
| 98 | + node.safe_psql(query, dbname=dbname, username=username) |
| 99 | + |
| 100 | + def disable(self, dbname=None, username=None): |
| 101 | + """ |
| 102 | + Disables the running subscription. |
| 103 | + """ |
| 104 | + query = "alter subscription {} disable" |
| 105 | + self.node.safe_psql(query.format(self.name), |
| 106 | + dbname=None, |
| 107 | + username=None) |
| 108 | + |
| 109 | + def enable(self, dbname=None, username=None): |
| 110 | + """ |
| 111 | + Enables the previously disabled subscription. |
| 112 | + """ |
| 113 | + query = "alter subscription {} enable" |
| 114 | + self.node.safe_psql(query.format(self.name), |
| 115 | + dbname=None, |
| 116 | + username=None) |
| 117 | + |
| 118 | + def refresh(self, copy_data=True, dbname=None, username=None): |
| 119 | + """ |
| 120 | + Disables the running subscription. |
| 121 | + """ |
| 122 | + query = "alter subscription {} refresh publication with (copy_data={})" |
| 123 | + self.node.safe_psql(query.format(self.name, copy_data), |
| 124 | + dbname=dbname, |
| 125 | + username=username) |
| 126 | + |
| 127 | + def close(self, dbname=None, username=None): |
| 128 | + """ |
| 129 | + Drops subscription |
| 130 | + """ |
| 131 | + self.node.safe_psql("drop subscription {}".format(self.name), |
| 132 | + dbname=dbname, username=username) |
| 133 | + |
| 134 | + def catchup(self, username=None): |
| 135 | + """ |
| 136 | + Wait until subscription catches up with publication. |
| 137 | +
|
| 138 | + Args: |
| 139 | + username: remote node's user name |
| 140 | + """ |
| 141 | + if pg_version_ge('10'): |
| 142 | + query = ( |
| 143 | + "select pg_current_wal_lsn() - replay_lsn = 0 " |
| 144 | + "from pg_stat_replication where application_name = '{}'" |
| 145 | + ).format(self.name) |
| 146 | + else: |
| 147 | + query = ( |
| 148 | + "select pg_current_xlog_location() - replay_location = 0 " |
| 149 | + "from pg_stat_replication where application_name = '{}'" |
| 150 | + ).format(self.name) |
| 151 | + |
| 152 | + try: |
| 153 | + # wait until this LSN reaches subscriber |
| 154 | + self.pub.node.poll_query_until( |
| 155 | + query=query, |
| 156 | + dbname=self.pub.dbname, |
| 157 | + username=username or self.pub.username, |
| 158 | + max_attempts=60, |
| 159 | + zero_rows_is_ok=True) # statistics may have not updated yet |
| 160 | + except Exception as e: |
| 161 | + raise_from(CatchUpException("Failed to catch up", query), e) |
0 commit comments