forked from azat385/mercury
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbl2r.py
178 lines (148 loc) · 5.94 KB
/
dbl2r.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/python
# -*- coding: utf-8 -*-
import logging
from logging.handlers import RotatingFileHandler
# format the log entries
formatter = logging.Formatter('%(asctime)s %(name)s %(levelname)s %(message)s')
handler = RotatingFileHandler('dbl2r.log',
mode='a',
maxBytes=20*1024*1024,
backupCount=5,
encoding=None,
delay=0)
handler.setFormatter(formatter)
logger = logging.getLogger(__name__)
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
from sqlalchemy.orm import sessionmaker
from sql_create_table import engine as local_engine, Data, Base
from sqlalchemy import Column, Integer, String, REAL
from sqlalchemy.ext.declarative import declarative_base
# from sql_remote_create import engine as remote_engine
from sqlalchemy import create_engine, Table, MetaData
url = 'postgresql://{}:{}@{}:{}/{}'
url = url.format('dbraw', ',fpflfyys[', '194.87.99.184', 5432, 'dbraw')
remote_engine = create_engine(url, client_encoding='utf8', echo=False)
logger.debug('creating connection to local db')
local_DBSession = sessionmaker(bind=local_engine)
local_session = local_DBSession()
logger.debug('creating connection to remote db')
remote_DBSession = sessionmaker(bind=remote_engine)
remote_session = remote_DBSession()
logger.debug('Pulling schema from local server')
local_meta = MetaData(bind=local_engine)
tt = Table(Data.__tablename__, local_meta, autoload=True)
columns = tt.columns.keys()
remote_meta = MetaData(bind=remote_engine)
# class rData(Base):
# __tablename__ = 'web_data'
# id = Column(Integer, primary_key=True)
# tag_id = Column(Integer, nullable=False, default=999)
# value = Column(REAL)
# stime = Column(String(30))
# rData = Table('web_data', remote_meta,
# Column('id', Integer, primary_key=True),
# Column('tag_id', Integer, nullable=False, default=999),
# Column('value', REAL),
# Column('stime', String(30))
# )
def quick_mapper(table):
Base = declarative_base()
class GenericMapper(Base):
__table__ = table
return GenericMapper
rData_table = Table('web_data', remote_meta, autoload=True, autoload_with=remote_engine)
rData = quick_mapper(rData_table)
remote_last_initial = remote_session.query(rData).order_by(rData.id.desc()).first()
if remote_last_initial is None:
remote_last_initial_id = 1
else:
remote_last_initial_id = remote_last_initial.id
logger.info('Starting up adding rows')
while 1:
local_last = local_session.query(Data).order_by(Data.id.desc()).first()
local_last_id = local_last.id
remote_last = remote_session.query(rData).order_by(rData.id.desc()).first()
if remote_last is None:
remote_last_id = 1
else:
remote_last_id = remote_last.id
logger.debug('db rows remote={}, local={}'.format(remote_last_id, local_last_id))
if remote_last_id >= local_last_id - 5:
logger.info('db rows remote={}, local={}'.format(remote_last_id, local_last_id))
logger.info('Finishing the cycles: {} has been added'.format(remote_last_id-remote_last_initial_id))
break
new_records = local_session.query(Data).filter(Data.id>remote_last_id).limit(100).all()
for record in new_records:
d = dict(
[(str(column), getattr(record, column)) for column in columns]
)
remote_session.merge(rData(**d))
logger.debug('Commit remote db')
remote_session.commit()
'''
http://www.tylerlesmann.com/2009/apr/27/copying-databases-across-platforms-sqlalchemy/
import getopt
import sys
from sqlalchemy import create_engine, MetaData, Table
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
def make_session(connection_string):
if 'sqlite' in connection_string :
engine = create_engine(connection_string, echo=True)
else:
engine = create_engine(connection_string, echo=True, client_encoding='utf8')
Session = sessionmaker(bind=engine)
return Session(), engine
def pull_data(from_db, to_db, tables):
source, sengine = make_session(from_db)
smeta = MetaData(bind=sengine)
destination, dengine = make_session(to_db)
for table_name in tables:
print 'Processing', table_name
print 'Pulling schema from source server'
table = Table(table_name, smeta, autoload=True)
# print 'Creating table on destination server'
# table.metadata.create_all(dengine) #creates table
NewRecord = quick_mapper(table)
columns = table.columns.keys()
remote_last_row = destination.query(table).order_by(table.id.desc()).first()
print 'Transferring records'
for record in source.query(table).filter(table.id>remote_last_row.id).limit(50).all():
data = dict(
[(str(column), getattr(record, column)) for column in columns]
)
destination.merge(NewRecord(**data))
print 'Committing changes'
destination.commit()
def print_usage():
print """
Usage: %s -f source_server -t destination_server table [table ...]
-f, -t = driver://user[:password]@host[:port]/database
Example: %s -f oracle://someuser:PaSsWd@db1/TSH1 \\
-t mysql://root@db2:3307/reporting table_one table_two
""" % (sys.argv[0], sys.argv[0])
def quick_mapper(table):
Base = declarative_base()
class GenericMapper(Base):
__table__ = table
return GenericMapper
if __name__ == '__main__':
# optlist, tables = getopt.getopt(sys.argv[1:], 'f:t:')
#
# options = dict(optlist)
# if '-f' not in options or '-t' not in options or not tables:
# print_usage()
# raise SystemExit, 1
url = 'postgresql://{}:{}@{}:{}/{}'
url = url.format('dbraw', ',fpflfyys[', '192.168.201.200', 5432, 'dbraw')
tables = ['data']
options = {'-f': 'sqlite:///common.db',
'-t': url,
}
pull_data(
options['-f'],
options['-t'],
tables,
)
'''