def add_table_tags(request): """ Updates the tags on a table according to the tag values in request. The update will delete all tags that are not present in request and add all tags that are. :param request: A HTTP-request object sent by the Django framework. The *POST* field must contain the following values: * schema: The name of a schema * table: The name of a table * Any number of values that start with 'tag_' followed by the id of a tag. :return: Redirects to the previous page """ ids = { int(field[len("tag_"):]) for field in request.POST if field.startswith("tag_") } schema = request.POST["schema"] table = request.POST.get("table", None) engine = actions._get_engine() metadata = sqla.MetaData(bind=engine) Session = sessionmaker() session = Session(bind=engine) session.query(TableTags).filter( TableTags.table_name == table and TableTags.schema_name == schema).delete() for id in ids: t = TableTags(**{ "schema_name": schema, "table_name": table, "tag": id }) session.add(t) session.commit() actions.update_meta_search(table, schema) return redirect(request.META["HTTP_REFERER"])
def upgrade(): # ### commands auto generated by Alembic - please adjust! ### op.create_table( "meta_search", sa.Column("schema", sa.String(length=100), nullable=False), sa.Column("table", sa.String(length=100), nullable=False), sa.Column("comment", postgresql.TSVECTOR(), nullable=True), sa.PrimaryKeyConstraint("schema", "table"), schema="public", ) conn = op.get_bind() meta = sa.MetaData(bind=conn) meta.reflect() Session = sessionmaker() session = Session(bind=conn) try: for table in meta.tables.values(): update_meta_search(session, table.name, table.schema, insert_only=True) session.commit() except: session.rollback() raise finally: session.close()
def upgrade(): # ### commands auto generated by Alembic - please adjust! ### op.create_table( "meta_search", sa.Column("schema", sa.String(length=100), nullable=False), sa.Column("table", sa.String(length=100), nullable=False), sa.Column("comment", postgresql.TSVECTOR(), nullable=True), sa.PrimaryKeyConstraint("schema", "table"), schema="public", ) conn = op.get_bind() meta = sa.MetaData(bind=conn) meta.reflect() for table in meta.tables.values(): if table.schema in schema_whitelist: update_meta_search(table.name, table.schema)
def post(self, request, schema, table): table_obj = actions._get_table(schema=schema, table=table) raw_input = request.data metadata, error = actions.try_parse_metadata(raw_input) if metadata is not None: compiler = JSONCompiler() table_obj.comment = json.dumps(compiler.visit(metadata)) cursor = actions.load_cursor_from_context(request.data) # Surprisingly, SQLAlchemy does not seem to escape comment strings # properly. Certain strings cause errors database errors. # This MAY be a security issue. Therefore, we do not use # SQLAlchemy's compiler here but do it manually. sql = "COMMENT ON TABLE {schema}.{table} IS %s".format( schema=table_obj.schema, table=table_obj.name) cursor.execute(sql, (table_obj.comment, )) actions.update_meta_search(table, schema) return JsonResponse(raw_input) else: raise APIError(error)
def handle(self, *args, **options): engine = _get_engine() inspector = sqla.inspect(engine) for schema in schema_whitelist: for table_name in inspector.get_table_names(schema=schema): update_meta_search(table_name, schema)