<s> from __future__ import unicode_literals <EOL> from django . db . migrations . operations . base import Operation <EOL> from pg_fts . fields import TSVectorField <EOL> __all__ = ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> """<STR_LIT>""" <EOL> class PgFtsSQL ( object ) : <EOL> sql_delete_trigger = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> sql_create_trigger = """<STR_LIT>""" <EOL> sql_create_index = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> sql_delete_index = '<STR_LIT>' <EOL> sql_update_vector = '<STR_LIT>' <EOL> def delete_trigger ( self , model , field ) : <EOL> return self . sql_delete_trigger . format ( <EOL> model = model . _meta . db_table , <EOL> fts_name = field . get_attname_column ( ) [ <NUM_LIT:1> ] <EOL> ) <EOL> def create_fts_trigger ( self , model , vector_field ) : <EOL> fields = [ ] <EOL> vectors = [ ] <EOL> if not isinstance ( vector_field , TSVectorField ) : <EOL> raise AttributeError <EOL> try : <EOL> dict_field = model . _meta . get_field_by_name ( vector_field . dictionary ) [ <NUM_LIT:0> ] <EOL> dictionary = "<STR_LIT>" % ( <EOL> dict_field . get_attname_column ( ) [ <NUM_LIT:1> ] ) <EOL> fields . append ( '<STR_LIT>' . format ( vector_field . dictionary ) ) <EOL> except : <EOL> dictionary = "<STR_LIT>" % vector_field . dictionary <EOL> for field , rank in vector_field . _get_fields_and_ranks ( ) : <EOL> fields . append ( '<STR_LIT>' . format ( <EOL> field . get_attname_column ( ) [ <NUM_LIT:1> ] ) ) <EOL> vectors . append ( self . _get_vector_for_field ( field , rank , dictionary ) ) <EOL> return self . sql_create_trigger . format ( <EOL> model = model . _meta . db_table , <EOL> fts_name = vector_field . get_attname_column ( ) [ <NUM_LIT:1> ] , <EOL> fts_fields = '<STR_LIT>' . join ( fields ) , <EOL> vectors = '<STR_LIT>' . join ( vectors ) <EOL> ) <EOL> def update_vector ( self , model , vector_field ) : <EOL> vectors = [ ] <EOL> sql_fn = "<STR_LIT>" <EOL> if not isinstance ( vector_field , TSVectorField ) : <EOL> raise AttributeError <EOL> try : <EOL> dict_field = model . _meta . get_field_by_name ( vector_field . dictionary ) [ <NUM_LIT:0> ] <EOL> dictionary = "<STR_LIT>" % ( <EOL> dict_field . get_attname_column ( ) [ <NUM_LIT:1> ] ) <EOL> except : <EOL> dictionary = "<STR_LIT>" % vector_field . dictionary <EOL> for field , rank in vector_field . _get_fields_and_ranks ( ) : <EOL> vectors . append ( sql_fn % ( <EOL> dictionary , field . get_attname_column ( ) [ <NUM_LIT:1> ] , rank ) ) <EOL> return self . sql_update_vector . format ( <EOL> model = model . _meta . db_table , <EOL> vector = vector_field . get_attname_column ( ) [ <NUM_LIT:1> ] , <EOL> fields = '<STR_LIT>' . join ( vectors ) <EOL> ) <EOL> def _get_vector_for_field ( self , field , weight , dictionary ) : <EOL> return "<STR_LIT>" % ( <EOL> dictionary , field . get_attname_column ( ) [ <NUM_LIT:1> ] , weight <EOL> ) <EOL> def create_index ( self , model , vector_field , index ) : <EOL> return self . sql_create_index . format ( <EOL> model = model . _meta . db_table , <EOL> fts_index = index , <EOL> fts_name = vector_field . get_attname_column ( ) [ <NUM_LIT:1> ] <EOL> ) <EOL> def delete_index ( self , model , vector_field ) : <EOL> return self . sql_delete_index . format ( <EOL> model = model . _meta . db_table , <EOL> fts_name = vector_field . get_attname_column ( ) [ <NUM_LIT:1> ] <EOL> ) <EOL> class BaseVectorOperation ( Operation ) : <EOL> """<STR_LIT>""" <EOL> reduces_to_sql = True <EOL> reversible = True <EOL> sql_creator = PgFtsSQL ( ) <EOL> forward_fn = None <EOL> backward_fn = None <EOL> def __init__ ( self , name , fts_vector ) : <EOL> self . name = name <EOL> self . fts_vector = fts_vector <EOL> def state_forwards ( self , app_label , state ) : <EOL> pass <EOL> def database_forwards ( self , app_label , schema_editor , from_state , <EOL> to_state ) : <EOL> model = from_state . render ( ) . get_model ( app_label , self . name ) <EOL> vector_field = model . _meta . get_field_by_name ( self . fts_vector ) [ <NUM_LIT:0> ] <EOL> schema_editor . execute ( self . forward_fn ( <EOL> model , <EOL> vector_field <EOL> ) ) <EOL> def database_backwards ( self , app_label , schema_editor , from_state , <EOL> to_state ) : <EOL> model = from_state . render ( ) . get_model ( app_label , self . name ) <EOL> vector_field = model . _meta . get_field_by_name ( self . fts_vector ) [ <NUM_LIT:0> ] <EOL> schema_editor . execute ( self . backward_fn ( <EOL> model , <EOL> vector_field <EOL> ) ) <EOL> def describe ( self ) : <EOL> return "<STR_LIT>" % ( <EOL> self . fts_vector , self . name <EOL> ) <EOL> class UpdateVectorOperation ( BaseVectorOperation ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , fts_vector ) : <EOL> self . name = name <EOL> self . fts_vector = fts_vector <EOL> self . forward_fn = self . sql_creator . update_vector <EOL> def database_backwards ( self , app_label , schema_editor , from_state , <EOL> to_state ) : <EOL> pass <EOL> def describe ( self ) : <EOL> return "<STR_LIT>" % ( <EOL> self . fts_vector , self . name <EOL> ) <EOL> class CreateFTSTriggerOperation ( BaseVectorOperation ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , fts_vector ) : <EOL> self . name = name <EOL> self . fts_vector = fts_vector <EOL> self . forward_fn = self . sql_creator . create_fts_trigger <EOL> self . backward_fn = self . sql_creator . delete_trigger <EOL> def database_backwards ( self , app_label , schema_editor , from_state , <EOL> to_state ) : <EOL> model = from_state . render ( ) . get_model ( app_label , self . name ) <EOL> vector_field = model . _meta . get_field_by_name ( self . fts_vector ) [ <NUM_LIT:0> ] <EOL> schema_editor . execute ( self . backward_fn ( <EOL> model , <EOL> vector_field <EOL> ) ) <EOL> def describe ( self ) : <EOL> return "<STR_LIT>" % ( <EOL> self . fts_vector , self . name <EOL> ) <EOL> class DeleteFTSTriggerOperation ( BaseVectorOperation ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , fts_vector ) : <EOL> self . name = name <EOL> self . fts_vector = fts_vector <EOL> self . forward_fn = self . sql_creator . delete_trigger <EOL> self . backward_fn = self . sql_creator . create_fts_trigger <EOL> def describe ( self ) : <EOL> return "<STR_LIT>" % ( <EOL> self . fts_vector , self . name <EOL> ) <EOL> class CreateFTSIndexOperation ( BaseVectorOperation ) : <EOL> """<STR_LIT>""" <EOL> INDEXS = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def __init__ ( self , name , fts_vector , index ) : <EOL> assert index in self . INDEXS , "<STR_LIT>" % ( <EOL> index , '<STR_LIT:U+002CU+0020>' . join ( self . INDEXS ) ) <EOL> self . name = name <EOL> self . fts_vector = fts_vector <EOL> self . index = index <EOL> def state_forwards ( self , app_label , state ) : <EOL> pass <EOL> def database_forwards ( self , app_label , schema_editor , from_state , <EOL> to_state ) : <EOL> model = from_state . render ( ) . get_model ( app_label , self . name ) <EOL> vector_field = model . _meta . get_field_by_name ( self . fts_vector ) [ <NUM_LIT:0> ] <EOL> if not isinstance ( vector_field , TSVectorField ) : <EOL> raise AttributeError <EOL> schema_editor . execute ( self . sql_creator . create_index ( <EOL> model , vector_field , self . index <EOL> ) ) <EOL> def database_backwards ( self , app_label , schema_editor , from_state , <EOL> to_state ) : <EOL> model = from_state . render ( ) . get_model ( app_label , self . name ) <EOL> vector_field = model . _meta . get_field_by_name ( self . fts_vector ) [ <NUM_LIT:0> ] <EOL> schema_editor . execute ( self . sql_creator . delete_index ( <EOL> model , <EOL> vector_field <EOL> ) ) <EOL> def describe ( self ) : <EOL> return "<STR_LIT>" % ( <EOL> self . index , self . fts_vector , self . name <EOL> ) <EOL> class DeleteFTSIndexOperation ( CreateFTSIndexOperation ) : <EOL> """<STR_LIT>""" <EOL> def database_forwards ( self , app_label , schema_editor , from_state , <EOL> to_state ) : <EOL> model = from_state . render ( ) . get_model ( app_label , self . name ) <EOL> vector_field = model . _meta . get_field_by_name ( self . fts_vector ) [ <NUM_LIT:0> ] <EOL> schema_editor . execute ( self . sql_creator . delete_index ( <EOL> model , <EOL> vector_field <EOL> ) ) <EOL> def database_backwards ( self , app_label , schema_editor , from_state , <EOL> to_state ) : <EOL> model = from_state . render ( ) . get_model ( app_label , self . name ) <EOL> vector_field = model . _meta . get_field_by_name ( self . fts_vector ) [ <NUM_LIT:0> ] <EOL> if not isinstance ( vector_field , TSVectorField ) : <EOL> raise AttributeError <EOL> schema_editor . execute ( self . sql_creator . create_index ( <EOL> model , vector_field , self . index ) ) <EOL> def describe ( self ) : <EOL> return "<STR_LIT>" % ( <EOL> self . index , self . fts_vector , self . name <EOL> ) </s>
