Module: E621::CsvImportable

Extended by:
T::Sig
Included in:
Artist, BulkUpdateRequest, Pool, Post, PostReplacement, PostVersion, Tag, TagAlias, TagImplication, WikiPage
Defined in:
lib/e621/csv_importable.rb

Instance Method Summary collapse

Instance Method Details

#import_from_csv(csv_path, truncate: false, recreate_indexes: false, chunk_bytes: 1 << 20) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/e621/csv_importable.rb', line 62

def import_from_csv(csv_path, truncate: false, recreate_indexes: false, chunk_bytes: 1 << 20)
  model = T.unsafe(self)
  indexes = recreate_indexes ? secondary_index_definitions : {}

  if truncate
    ActiveSupport::Notifications.instrument("truncate.e621_export_downloader", table: model.table_name) do
      model.connection.execute("TRUNCATE #{model.quoted_table_name}")
    end
  end
  indexes.each_key do |name|
    ActiveSupport::Notifications.instrument("drop_index.e621_export_downloader", table: model.table_name, index: name) do
      model.connection.execute("DROP INDEX IF EXISTS #{name}")
    end
  end

  count = begin
    ActiveSupport::Notifications.instrument("copy.e621_export_downloader", table: model.table_name) do |payload|
      copy_csv(csv_path, chunk_bytes: chunk_bytes).tap { |rows| payload[:rows] = rows }
    end
  ensure
    indexes.each do |name, ddl|
      ActiveSupport::Notifications.instrument("create_index.e621_export_downloader", table: model.table_name, index: name) do
        model.connection.execute(ddl)
      end
    end
  end
  self.row_count = count
  count
end

#row_countObject



11
12
13
# File 'lib/e621/csv_importable.rb', line 11

def row_count
  E621::RowCount[T.unsafe(self).table_name.split(".").last]
end

#row_count=(count) ⇒ Object



16
17
18
# File 'lib/e621/csv_importable.rb', line 16

def row_count=(count)
  E621::RowCount.set(T.unsafe(self).table_name.split(".").last, count)
end