Skip to content
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

Closed
wants to merge 19 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions caravel/bin/caravel
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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("")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?


if (prefix == "" and database is None) or (database == 'main' and prefix == ""):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this main special case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because if you use cmd to load examples, the db name: main
and the main db is same as Caravel core db

Copy link
Contributor

@xrmx xrmx Aug 23, 2016

Choose a reason for hiding this comment

The 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")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

print not needed

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not usual to use exit in python


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 == []:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if not caravel_dbs:

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()