-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimport_sql_2_couch.py
More file actions
189 lines (153 loc) · 7.53 KB
/
Copy pathimport_sql_2_couch.py
File metadata and controls
189 lines (153 loc) · 7.53 KB
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
179
180
181
182
183
184
185
186
187
188
189
#!/usr/bin/python
#! -*- coding: utf-8 -*-
# ---- Import required libraries
# System/standard packages
import getpass
# deb-package: python-argparse
from argparse import ArgumentParser
from connections import *
from tools import *
# ----- Main import part
if __name__=="__main__":
# Name
PROG_NAME=__file__
# Version
PROG_VERSION="$Rev$"
# Code Pages URL
PROG_CODE_PAGES="http://code.google.com/p/import-sql-2-couch/"
# Description
PROG_SUMMARY="Simple SQL to CouchDb importer. Define a source, get it in a couch."
# Epilog
PROG_EPILOG="For more descriptive information, newer versions and tracking issues/bugs take a look on the code-pages (%s)." % (PROG_CODE_PAGES)
# Defines sql-database-type
SQL_TYPE_MYSQL="mysql"
sqlTypeChoices=[SQL_TYPE_MYSQL]
# Initialize/Create arguments-parser-object
argParser=ArgumentParser(description=PROG_SUMMARY, epilog=PROG_EPILOG, add_help=False, prog=PROG_NAME)
# Adding arguments
argParser.add_argument("-s", "--sql-connection", help="Defines the SQL-connection, e.g. [http|https://][username:password@]sql-host[:port]", required=True, type=str)
argParser.add_argument("-ss", "--sql-sources", help="Adds one or more sql db[.table]-sources to the import list, e.g. -ss my_db.my_table -ss second_db", action="append", type=str)
argParser.add_argument("-st", "--sql-type", help="Defines which type of database 'sql' is, e.g. MySQL, ...", required=True, choices=sqlTypeChoices)
argParser.add_argument("-sl", "--sql-limit", help="Limits the sql-query to select rows from a db[.table]-source.", default=None, type=int)
argParser.add_argument("-scsql", "--sql-connect-sql", help="Adds one or more sqls which are executed directly after connecting to the sql-host, useful to set charset-settings.", action="append", type=str)
argParser.add_argument("-c", "--couch-connection", help="Defines the Couch-connection, e.g. [http|https://][username:password@]couch-host[:port]", required=True, type=str)
argParser.add_argument("-cct", "--couch-creation-type", help="Defines how the import creates databases within the couch, from db[.table]-source.", choices=[CouchConnection.CREATION_TYPE_DB_TBL, CouchConnection.CREATION_TYPE_DB, CouchConnection.CREATION_TYPE_TBL], default=CouchConnection.CREATION_TYPE_DB_TBL)
argParser.add_argument("-v", "--verbose", help="Sets verbosity of the import's debug-/information-output.", action="store_const", const=True, default=False)
argParser.add_argument("-d", "--debug", help="Sets debug-level, actually not executing any changes-operations.", action="store_const", const=True, default=False)
argParser.add_argument("-h", "--help", help="Prints out this help.", action="help")
argParser.add_argument("--version", help="Prints out the current version.", action="version", version="%s, %s" % (PROG_NAME, PROG_VERSION) )
# Parse arguments
params=argParser.parse_args()
# Verbosity
verbosity=Verbosity(params.verbose)
# SQL Connection
verbosity.Say("Connect to SQL, %s ..." % params.sql_connection)
# SQL-Type: MySQL
if params.sql_type==SQL_TYPE_MYSQL: sqlConnection=MySQLConnection(connectionString=params.sql_connection)
# Read username from stdinput
# Enter username/password via std input
if sqlConnection.GetUsername()=="?" or sqlConnection.GetPassword()=="?":
if not params.verbose: print "Connection to sql ..."
if sqlConnection.GetUsername()=="?": sqlConnection.SetUsername(raw_input("Username: "))
if sqlConnection.GetPassword()=="?": sqlConnection.SetPassword(getpass.getpass("Password: "))
# End if
sqlConnection.Connect()
# Executing separated sql settings sql-database-settings
if params.sql_connect_sql:
for sqlQuery in params.sql_connect_sql:
verbosity.Say("Executing sql: %s" % sqlQuery)
if not params.debug: sqlConnection.ExecuteSQL(sqlQuery)
# End for
# End if
verbosity.Say("... connected.")
# Couch Connection
verbosity.Say("Connect to Couch, %s ..." % params.couch_connection)
couchConnection=CouchConnection(connectionString=params.couch_connection)
# Enter username/password via std input
if couchConnection.GetUsername()=="?" or couchConnection.GetPassword()=="?":
if not params.verbose: print "Connection to couch ..."
if couchConnection.GetUsername()=="?": couchConnection.SetUsername(raw_input("Username: "))
if couchConnection.GetPassword()=="?": couchConnection.SetPassword(getpass.getpass("Password: "))
# End if
couchConnection.Connect()
verbosity.Say("... connected.")
# Extend/Validate SQL-Sources
verbosity.Say("Extend/Validate SQL-Sources ... ")
if params.sql_sources:
for source in params.sql_sources:
# Split source-parts
sourceParts=SourceParts(source)
# Check if database is valid
try:
sqlConnection.GetDatabases([sourceParts.GetDatabase()])
except NoDatabaseFound:
sys.exit("Error: SQL database '%s' not found." % sourceParts.GetDatabase())
# End try/except
# Check if table is available and valid
if sourceParts.GetTable():
try:
sqlConnection.GetTablesFromDatabase(sourceParts.GetDatabase(), sourceParts.GetTable())
except NoTableFound:
sys.exit("Error: SQL table '%s' in database '%s' not found." % (sourceParts.GetDatabase(), sourceParts.GetTable()))
# End try/except
# End if
# End for
else:
try:
params.sql_sources=sqlConnection.GetDatabases()
except NoDatabasesFound:
sys.exit("Error: SQL databases not found on host '%s'." % sqlConnection.GetHost())
# End try/except
# End if
verbosity.Say("... extended/validated.")
verbosity.Say("Using following SQL-Sources for an import: ", params.sql_sources)
# Import sources
verbosity.Say("Importing ...")
for source in params.sql_sources:
# Split source-parts
source=SourceParts(source)
selectDb=source.GetDatabase()
selectTables=[source.GetTable()] if source.GetTable() else sqlConnection.GetTablesFromDatabase(selectDb)
for selectTable in selectTables:
# Create used couch-db-name
if params.couch_creation_type==CouchConnection.CREATION_TYPE_DB_TBL: couchUsingDb="%s_%s" % (selectDb, selectTable)
if params.couch_creation_type==CouchConnection.CREATION_TYPE_DB: couchUsingDb=selectDb
if params.couch_creation_type==CouchConnection.CREATION_TYPE_TBL: couchUsingDb=selectTable
# Check and eliminate not valid characters for couch-db-name
couchUsingDb=couchConnection.GetValidDatabaseName(couchUsingDb)
verbosity.Say("%s.%s (%s)... " % (selectDb, selectTable, couchUsingDb))
# Check if couch-db is already created
if couchConnection.IsDatabase(couchUsingDb):
verbosity.Say("Couch-Database available, already created.")
else:
verbosity.Say("Couch-Database not available, creating ...")
# If not debugging, execute/create database
try:
if not params.debug: couchConnection.CreateDatabase(couchUsingDb)
except:
sys.exit("Error: Cannot create couch database '%s'" % couchUsingDb)
# End try/except
verbosity.Say("... created.")
# End if
rowCounter=0
for row in sqlConnection.YieldRowsFromTable(selectDb, selectTable, params.sql_limit):
# Increase row-counter
rowCounter+=1
# Verbose and write row to couch-doc
verbosity.Say("Writing couch-doc #%i ..." % rowCounter)
# If not debugging, execute/write document
docResult={"docId": "", "docRev": ""}
try:
if not params.debug: docResult=couchConnection.WriteDoc(couchUsingDb, row)
except CouchWriteDocument:
print "Document: ", row
sys.exit("Error: Cannot write document to couch.")
# End try/except
if docResult:
verbosity.Say("... written (id: %s, rev: %s)." % (docResult["docId"], docResult["docRev"]))
# End if
# End for
# End for
verbosity.Say("")
# End for
# End if