|
| 1 | +#!/usr/bin/env python |
| 2 | +# coding: utf-8 |
| 3 | + |
| 4 | +import os |
| 5 | +import subprocess |
| 6 | +import tempfile |
| 7 | +import testgres |
| 8 | +import time |
| 9 | +import unittest |
| 10 | + |
| 11 | +import pdb |
| 12 | + |
| 13 | +import logging.config |
| 14 | + |
| 15 | +from distutils.version import LooseVersion |
| 16 | + |
| 17 | +from testgres import \ |
| 18 | + InitNodeException, \ |
| 19 | + StartNodeException, \ |
| 20 | + ExecUtilException, \ |
| 21 | + BackupException, \ |
| 22 | + QueryException, \ |
| 23 | + CatchUpException, \ |
| 24 | + TimeoutException |
| 25 | + |
| 26 | +from testgres import \ |
| 27 | + TestgresConfig, \ |
| 28 | + configure_testgres |
| 29 | + |
| 30 | +from testgres import \ |
| 31 | + NodeStatus, \ |
| 32 | + IsolationLevel, \ |
| 33 | + get_new_node |
| 34 | + |
| 35 | +from testgres import \ |
| 36 | + get_bin_path, \ |
| 37 | + get_pg_config |
| 38 | + |
| 39 | +from testgres import bound_ports |
| 40 | + |
| 41 | +# FIRST test from past |
| 42 | +# with testgres.get_new_node('test') as node: |
| 43 | +# node.init() # run initdb |
| 44 | +# node.start() # start PostgreSQL |
| 45 | +# #print(node.execute('postgres', 'select 1')) |
| 46 | +# #print(node.psql('postgres', 'select 1')) |
| 47 | +# print(node.connect('postgres', 'vis')) |
| 48 | +# with node.connect() as con: |
| 49 | +# con.begin('serializable') |
| 50 | +# print(con.execute('select %s', 1)) |
| 51 | +# con.rollback() |
| 52 | +# node.stop() # stop PostgreSQL |
| 53 | + |
| 54 | + |
| 55 | +# test replication behavoiur on HOT_STANDBY_FEEDBACK |
| 56 | +# def set_trace(con, command="pg_debug"): |
| 57 | +# pid = con.execute("select pg_backend_pid()")[0][0] |
| 58 | +# p = subprocess.Popen([command], stdin=subprocess.PIPE) |
| 59 | +# p.communicate(str(pid).encode()) |
| 60 | + |
| 61 | +# with get_new_node() as master: |
| 62 | +# master.init().start() |
| 63 | +# with master.backup() as backup: |
| 64 | +# with backup.spawn_replica() as replica: |
| 65 | +# replica = replica.start() |
| 66 | +# master.execute('postgres', 'create table test (val int4)') |
| 67 | +# master.execute('postgres', 'insert into test values (0), (1), (2)') |
| 68 | +# replica.catchup() # wait until changes are visible |
| 69 | +# with replica.connect() as con1: |
| 70 | +# set_trace(con1) |
| 71 | +# import pdb; pdb.set_trace() # Важно,если последний идет pdb,то pass |
| 72 | +# pass |
| 73 | + |
| 74 | + # print(replica.execute('postgres', 'select count(*) from test')) |
| 75 | + #print(replica.execute('postgres', ':gdb')) |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | +# SECOND test dump new keys |
| 80 | +with get_new_node('node1') as node1: |
| 81 | + node1.init().start() |
| 82 | + |
| 83 | + with node1.connect('postgres') as con: |
| 84 | + con.begin() |
| 85 | + con.execute('create table test (val int)') |
| 86 | + con.execute('insert into test values (1), (2)') |
| 87 | + con.commit() |
| 88 | + |
| 89 | + # take a new dump plain format |
| 90 | + dump = node1.dump('postgres') |
| 91 | + # self.assertTrue(os.path.isfile(dump)) |
| 92 | + with get_new_node('node2') as node2: |
| 93 | + node2.init().start().restore('postgres', dump) |
| 94 | + res = node2.execute('postgres','select * from test order by val asc') |
| 95 | + # self.assertListEqual(res, [(1, ), (2, )]) |
| 96 | + # finally, remove dump |
| 97 | + os.remove(dump) |
| 98 | + |
| 99 | + # take a new dump custom format |
| 100 | + dump = node1.dump('postgres') |
| 101 | + with get_new_node('node2') as node2: |
| 102 | + node2.init().start().restore('postgres', dump) |
| 103 | + res = node2.execute('postgres','select * from test order by val asc') |
| 104 | + os.remove(dump) |
| 105 | + |
| 106 | + # take a new dump directory format |
| 107 | + dump = node1.dump('postgres') |
| 108 | + with get_new_node('node2') as node2: |
| 109 | + node2.init().start().restore('postgres', dump) |
| 110 | + res = node2.execute('postgres','select * from test order by val asc') |
| 111 | + os.remove(dump) |
| 112 | + |
| 113 | + # take a new dump tar format |
| 114 | + dump = node1.dump('postgres') |
| 115 | + with get_new_node('node2') as node2: |
| 116 | + node2.init().start().restore('postgres', dump) |
| 117 | + res = node2.execute('postgres','select * from test order by val asc') |
| 118 | + os.remove(dump) |
| 119 | + |
| 120 | + # take a new dump tar format |
| 121 | + dump = node1.dump('postgres') |
| 122 | + with get_new_node('node2') as node2: |
| 123 | + node2.init().start().restore('postgres', dump) |
| 124 | + res = node2.execute('postgres','select * from test order by val asc') |
| 125 | + os.remove(dump) |
| 126 | + |
| 127 | +# 1) make dump to new place |
| 128 | +# pg_dump mydb > db.sql |
| 129 | +# 2) make dump to non default formate |
| 130 | +# 2.5) Чтобы сформировать выгрузку в формате plain: |
| 131 | +# pg_dump -Fp mydb > db.dump |
| 132 | +# 2.1) Чтобы сформировать выгрузку в формате custom: |
| 133 | +# pg_dump -Fc mydb > db.dump |
| 134 | +# 2.2) Чтобы сформировать выгрузку в формате directory: |
| 135 | +# pg_dump -Fd mydb -f dumpdir |
| 136 | +# 2.3) ? Чтобы сформировать выгрузку в формате directory в 5 параллельных потоков: |
| 137 | +# pg_dump -Fd mydb -j 5 -f dumpdir |
| 138 | +# 2.4) Чтобы сформировать выгрузку в формате tar: |
| 139 | +# pg_dump -Ft mydb > db.dump |
| 140 | + |
0 commit comments