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 10 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
29 changes: 29 additions & 0 deletions caravel/bin/caravel
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,35 @@ def refresh_druid():
"[" + cluster.cluster_name + "]")
session.commit()

@manager.option(
'-p', '--prefix', default='data_',
Copy link
Contributor

Choose a reason for hiding this comment

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

just default to None and if it's None just don't filter

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think no filter is not good, because main database (for caravel system use) have many table not store data, It doesn't need sync.

Copy link
Contributor

Choose a reason for hiding this comment

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

Well, first thing is that you should not install caravel on the same database of your data. Second the feature should be generic and not tied to your own usage :)

help="Sync Table Prefix")
def synctable(prefix):
''' Sync DB Table with Caravel Table'''
existing_tables = []
for row in db.session.query(caravel.models.SqlaTable).all():
existing_tables += [(row.database.database_name, row.name)]
all_tables = []
for caravel_db in db.session.query(caravel.models.Database).all():
for table_name in caravel_db.get_sqla_engine().table_names():
all_tables += [(caravel_db.database_name, table_name)]

all_tables = [row for row in all_tables if row[1].startswith(prefix)]

need_insert = list(set(all_tables) - set(existing_tables))
Copy link
Contributor

Choose a reason for hiding this comment

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

If you transform this list of tuples in a dict you can save some work later:

from collections import defaultdict

# can't find a better name atm :)
insert_dict = defaultdict(list)
for db_name, table_name in need_insert:
    insert_dict[db].append(table)

for db_name, tables in insert_dict.items():
    database = db.session.query(caravel.models.Database).filter_by(database_name=db_name).first()
    for table in tables:
       tbl = ...
       tbl.database = database
       ...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea, thank you very much!


for db_name, table_name in need_insert:
tbl = caravel.models.SqlaTable(table_name=table_name)
tbl.description = ""
tbl.is_featured = False
tbl.database = db.session.query(caravel.models.Database).filter_by(database_name=db_name).first()
db.session.merge(tbl)
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we really need merge here instead of add?

db.session.commit()
tbl.fetch_metadata()

print("[{db}] {table} insert success.".format(db=db_name, table=table_name))

print("[{}] Sync table complete.".format(len(need_insert)))

if __name__ == "__main__":
manager.run()