From 3b753e3d0e6cc20d6fd3984e60c43963c729e89f Mon Sep 17 00:00:00 2001 From: Ralph Amissah Date: Wed, 23 Jul 2014 18:10:24 -0400 Subject: v5 v6: db, rescue where no tables, attempt to initialize db and retry * internally run the equivalent of --createall * (if successful) permitting completion of --import or --update command * make use of action triggers --- lib/sisu/v6/db_import.rb | 73 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 68 insertions(+), 5 deletions(-) (limited to 'lib/sisu/v6/db_import.rb') diff --git a/lib/sisu/v6/db_import.rb b/lib/sisu/v6/db_import.rb index f3e8b96c..cca02edf 100644 --- a/lib/sisu/v6/db_import.rb +++ b/lib/sisu/v6/db_import.rb @@ -138,16 +138,78 @@ module SiSU_DbImport 'Marshal Load', @fnc ).puts_grey if @opt.act[:verbose][:set]==:on -#% select_first_match=%{ SELECT metadata_and_text.tid FROM metadata_and_text WHERE metadata_and_text.src_filename = '#{@md.fns}' AND metadata_and_text.language_document_char = '#{@opt.lng}' ;} # note, for .ssm: @md.fns (is set during runtime & is) != @opt.fns @md.opt.fns - file_exist=@sql_type==:sqlite \ - ? @conn.get_first_value(select_first_match) - : @conn.exec(select_first_match).field_values("tid")[0] + file_exist=if @sql_type==:sqlite + begin + @conn.get_first_value(select_first_match) + rescue SQLite3::Exception => e + # not tested + puts "Exception occurred" + puts e + SiSU_Utils::CodeMarker.new(__LINE__,__FILE__,:yellow).mark( + "\n" \ + + 'Attempting to initialize db' + "\n" \ + + 'Creating db tables' + ) + sdb= + SiSU_DbDBI::Create.new(@opt,@conn,@file_maint,@sql_type) + sdb_index= + SiSU_DbDBI::Index.new(@opt,@conn,@file_maint,@sql_type) + sdb.create_table.metadata_and_text + sdb.create_table.doc_objects + sdb.create_table.endnotes + sdb.create_table.endnotes_asterisk + sdb.create_table.endnotes_plus + sdb.create_table.urls + sdb_index.create_indexes + end + else + begin + @conn.exec(select_first_match).field_values("tid")[0] + rescue PG::Error => e + err=[ + e.result.error_field( PG::Result::PG_DIAG_SEVERITY ), + e.result.error_field( PG::Result::PG_DIAG_SQLSTATE ), + e.result.error_field( PG::Result::PG_DIAG_MESSAGE_PRIMARY ), + e.result.error_field( PG::Result::PG_DIAG_MESSAGE_DETAIL ), + e.result.error_field( PG::Result::PG_DIAG_MESSAGE_HINT ), + e.result.error_field( PG::Result::PG_DIAG_STATEMENT_POSITION ), + e.result.error_field( PG::Result::PG_DIAG_INTERNAL_POSITION ), + e.result.error_field( PG::Result::PG_DIAG_INTERNAL_QUERY ), + e.result.error_field( PG::Result::PG_DIAG_CONTEXT ), + e.result.error_field( PG::Result::PG_DIAG_SOURCE_FILE ), + e.result.error_field( PG::Result::PG_DIAG_SOURCE_LINE ), + e.result.error_field( PG::Result::PG_DIAG_SOURCE_FUNCTION ), + ] + p err + if err[2] =~/relation "\S+?" does not exist/ \ + or err.inspect =~/relation "\S+?" does not exist/ + SiSU_Utils::CodeMarker.new(__LINE__,__FILE__,:yellow).mark( + "\n" \ + + err[2] + "\n" \ + + 'Attempting to initialize db' + "\n" \ + + 'Creating db tables' + ) + sdb= + SiSU_DbDBI::Create.new(@opt,@conn,@file_maint,@sql_type) + sdb_index= + SiSU_DbDBI::Index.new(@opt,@conn,@file_maint,@sql_type) + sdb.create_table.metadata_and_text + sdb.create_table.doc_objects + sdb.create_table.endnotes + sdb.create_table.endnotes_asterisk + sdb.create_table.endnotes_plus + sdb.create_table.urls + sdb_index.create_indexes + retry + end + end + end if not file_exist t_d=[] # transaction_data t_d << db_import_metadata @@ -199,7 +261,7 @@ module SiSU_DbImport else if file_exist @db=SiSU_Env::InfoDb.new - puts "\nfile #{@opt.fns} in language code #{@opt.lng} already exists in database #{@db.psql.db} update instead?" + puts "\nfile #{@opt.fns} in language code #{@opt.lng} already exists in database #{@db.psql.db}, use --update instead?" end end end @@ -535,6 +597,7 @@ module SiSU_DbImport if inf[/#{Mx[:en_a_o]}(\d+)(.+?)#{Mx[:en_a_c]}/] nr,txt,digest_clean=$1,$2.strip,0 end + @id_n ||=0 @id_n+=1 txt=special_character_escape(txt) body=SiSU_FormatShared::CSS_Format.new(@md,data).endnote(nr,txt) -- cgit v1.2.3