-
Notifications
You must be signed in to change notification settings - Fork 14.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add synctable command into caravel #977
Changes from all commits
886a160
3a28a5d
18f845c
3f7a84f
ee7cd87
2a383f0
2438334
5db3c69
a165f43
21cb7cf
eec6c5f
0239ca3
11c7c01
f70ad9b
c57200e
d0cb663
06a65d0
6aa9d3e
e264d20
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ from __future__ import unicode_literals | |
import logging | ||
from datetime import datetime | ||
from subprocess import Popen | ||
from collections import defaultdict | ||
|
||
from flask_migrate import MigrateCommand | ||
from flask_script import Manager | ||
|
@@ -125,6 +126,67 @@ def refresh_druid(): | |
"[" + cluster.cluster_name + "]") | ||
session.commit() | ||
|
||
@manager.option( | ||
'-p', '--prefix', default="", | ||
help="Sync Table Prefix") | ||
@manager.option( | ||
'-d', '--database', default=None, | ||
help="Specifies database (use caravel db name, not real db name)") | ||
def synctable(prefix, database): | ||
''' Sync DB Table with Caravel Table''' | ||
print("") | ||
|
||
if (prefix == "" and database is None) or (database == 'main' and prefix == ""): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why this main special case? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. because if you use cmd to load examples, the db name: main There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the explaination. Shouldn't we just reject 'main' as database name and make database mandatory? |
||
print("If you not set prefix and db name, some system table may be sync to caravel.") | ||
|
||
check_status = raw_input("Are you sure do this? (Y/N)").lower().strip() | ||
if check_status not in ['y', 'yes']: | ||
print("Exit sync table") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. print not needed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Line 137 print is for separate synctable output and large console info by caravel cmd use |
||
exit() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not usual to use |
||
|
||
if database is None: | ||
caravel_dbs = db.session.query(caravel.models.Database).all() | ||
else: | ||
caravel_dbs = (db.session.query(caravel.models.Database) | ||
.filter_by(database_name=database).all()) | ||
|
||
if caravel_dbs == []: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
print("Database not exists, please check database name") | ||
exit() | ||
|
||
existing_tables = [] | ||
for row in db.session.query(caravel.models.SqlaTable).all(): | ||
existing_tables += [(row.database.database_name, row.name)] | ||
|
||
# Get all need insert table | ||
insert_dict = defaultdict(list) | ||
for caravel_db in caravel_dbs: | ||
for table_name in caravel_db.get_sqla_engine().table_names(): | ||
table = (caravel_db.database_name, table_name) | ||
|
||
if (table not in existing_tables) and table_name.startswith(prefix): | ||
insert_dict[caravel_db].append(table_name) | ||
|
||
# Insert to caravel tables | ||
count_insert = 0 | ||
for caravel_db, tables in insert_dict.items(): | ||
for table_name in tables: | ||
count_insert += 1 | ||
|
||
tbl = caravel.models.SqlaTable(table_name=table_name) | ||
tbl.description = "" | ||
tbl.is_featured = False | ||
tbl.database = caravel_db | ||
db.session.add(tbl) | ||
db.session.commit() | ||
tbl.fetch_metadata() | ||
|
||
print("[{db}] {table} insert success.".format( | ||
db=caravel_db.database_name, | ||
table=table_name | ||
)) | ||
|
||
print("[{}] Sync table complete.".format(count_insert)) | ||
|
||
if __name__ == "__main__": | ||
manager.run() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
?