mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
remove old import/export code
This commit is contained in:
@@ -1,35 +0,0 @@
|
||||
module Export
|
||||
|
||||
class UnsupportedExportSource < RuntimeError; end
|
||||
class FormatInvalidError < RuntimeError; end
|
||||
class FilenameMissingError < RuntimeError; end
|
||||
class ExportInProgressError < RuntimeError; end
|
||||
|
||||
def self.current_schema_version
|
||||
ActiveRecord::Migrator.current_version.to_s
|
||||
end
|
||||
|
||||
def self.models_included_in_export
|
||||
@models_included_in_export ||= begin
|
||||
Rails.application.eager_load! # So that all models get loaded now
|
||||
ActiveRecord::Base.descendants - [ActiveRecord::SchemaMigration]
|
||||
end
|
||||
end
|
||||
|
||||
def self.export_running_key
|
||||
'exporter_is_running'
|
||||
end
|
||||
|
||||
def self.is_export_running?
|
||||
$redis.get(export_running_key) == '1'
|
||||
end
|
||||
|
||||
def self.set_export_started
|
||||
$redis.set export_running_key, '1'
|
||||
end
|
||||
|
||||
def self.set_export_is_not_running
|
||||
$redis.del export_running_key
|
||||
end
|
||||
|
||||
end
|
||||
@@ -1,70 +0,0 @@
|
||||
require_dependency 'directory_helper'
|
||||
|
||||
module Export
|
||||
|
||||
class SchemaArgumentsError < RuntimeError; end
|
||||
|
||||
class JsonEncoder
|
||||
attr_accessor :stream_creator
|
||||
|
||||
include DirectoryHelper
|
||||
|
||||
def initialize(stream_creator = nil)
|
||||
@stream_creator = stream_creator
|
||||
@stream_creator ||= lambda do |filename|
|
||||
File.new(filename, 'w+b' )
|
||||
end
|
||||
|
||||
@schema_data = {
|
||||
schema: {}
|
||||
}
|
||||
|
||||
@table_info = {}
|
||||
end
|
||||
|
||||
|
||||
def write_json(name, data)
|
||||
filename = File.join( tmp_directory('export'), "#{name}.json")
|
||||
filenames << filename
|
||||
stream = stream_creator.call(filename)
|
||||
Oj.to_stream(stream, data, :mode => :compat)
|
||||
stream.close
|
||||
end
|
||||
|
||||
def write_schema_info(args)
|
||||
raise SchemaArgumentsError unless args[:source].present? && args[:version].present?
|
||||
|
||||
@schema_data[:schema][:source] = args[:source]
|
||||
@schema_data[:schema][:version] = args[:version]
|
||||
end
|
||||
|
||||
def write_table(table_name, columns)
|
||||
rows ||= []
|
||||
|
||||
while true
|
||||
current_rows = yield(rows.count)
|
||||
break unless current_rows && current_rows.size > 0
|
||||
rows.concat current_rows
|
||||
end
|
||||
|
||||
# TODO still way too big a chunk, needs to be split up
|
||||
write_json(table_name, rows)
|
||||
|
||||
@table_info[table_name] ||= {
|
||||
fields: columns.map(&:name),
|
||||
row_count: rows.size
|
||||
}
|
||||
|
||||
end
|
||||
|
||||
def finish
|
||||
@schema_data[:schema][:table_count] = @table_info.keys.count
|
||||
write_json("schema", @schema_data.merge(@table_info))
|
||||
end
|
||||
|
||||
def filenames
|
||||
@filenames ||= []
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
@@ -1,31 +0,0 @@
|
||||
module Import
|
||||
module Adapter
|
||||
class Base
|
||||
|
||||
def self.register(opts={})
|
||||
Import.add_import_adapter self, opts[:version], opts[:tables]
|
||||
@table_names = opts[:tables]
|
||||
end
|
||||
|
||||
def apply_to_column_names(table_name, column_names)
|
||||
up_column_names(table_name, column_names)
|
||||
end
|
||||
|
||||
def apply_to_row(table_name, row)
|
||||
up_row(table_name, row)
|
||||
end
|
||||
|
||||
|
||||
# Implement the following methods in subclasses:
|
||||
|
||||
def up_column_names(table_name, column_names)
|
||||
column_names
|
||||
end
|
||||
|
||||
def up_row(table_name, row)
|
||||
row
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,28 +0,0 @@
|
||||
module Import
|
||||
module Adapter
|
||||
class MergeMuteOptionsOnTopicUsers < Base
|
||||
|
||||
register version: '20130115012140', tables: [:topic_users]
|
||||
|
||||
def up_column_names(table_name, column_names)
|
||||
# rename_column :topic_users, :notifications, :notification_level
|
||||
# remove_column :topic_users, :muted_at
|
||||
if table_name.to_sym == :topic_users
|
||||
column_names.map {|col| col == 'notifications' ? 'notification_level' : col}.reject {|col| col == 'muted_at'}
|
||||
else
|
||||
column_names
|
||||
end
|
||||
end
|
||||
|
||||
def up_row(table_name, row)
|
||||
# remove_column :topic_users, :muted_at
|
||||
if table_name.to_sym == :topic_users
|
||||
row[0..6] + row[8..-1]
|
||||
else
|
||||
row
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,27 +0,0 @@
|
||||
module Import
|
||||
module Adapter
|
||||
class RemoveSubTagFromTopics < Base
|
||||
|
||||
register version: '20130116151829', tables: [:topics]
|
||||
|
||||
def up_column_names(table_name, column_names)
|
||||
# remove_column :topics, :sub_tag
|
||||
if table_name.to_sym == :topics
|
||||
column_names.reject {|col| col == 'sub_tag'}
|
||||
else
|
||||
column_names
|
||||
end
|
||||
end
|
||||
|
||||
def up_row(table_name, row)
|
||||
# remove_column :topics, :sub_tag
|
||||
if table_name.to_sym == :topics
|
||||
row[0..29] + row[31..-1]
|
||||
else
|
||||
row
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,58 +0,0 @@
|
||||
require_dependency 'import/adapter/base'
|
||||
|
||||
module Import
|
||||
|
||||
class UnsupportedExportSource < RuntimeError; end
|
||||
class FormatInvalidError < RuntimeError; end
|
||||
class FilenameMissingError < RuntimeError; end
|
||||
class ImportInProgressError < RuntimeError; end
|
||||
class ImportDisabledError < RuntimeError; end
|
||||
class UnsupportedSchemaVersion < RuntimeError; end
|
||||
class WrongTableCountError < RuntimeError; end
|
||||
class WrongFieldCountError < RuntimeError; end
|
||||
|
||||
def self.import_running_key
|
||||
'importer_is_running'
|
||||
end
|
||||
|
||||
def self.is_import_running?
|
||||
$redis.get(import_running_key) == '1'
|
||||
end
|
||||
|
||||
def self.set_import_started
|
||||
$redis.set import_running_key, '1'
|
||||
end
|
||||
|
||||
def self.set_import_is_not_running
|
||||
$redis.del import_running_key
|
||||
end
|
||||
|
||||
def self.backup_tables_count
|
||||
User.exec_sql("select count(*) as count from information_schema.tables where table_schema = '#{Jobs::Importer::BACKUP_SCHEMA}'")[0]['count'].to_i
|
||||
end
|
||||
|
||||
|
||||
def self.clear_adapters
|
||||
@adapters = {}
|
||||
@adapter_instances = {}
|
||||
end
|
||||
|
||||
def self.add_import_adapter(klass, version, tables)
|
||||
@adapters ||= {}
|
||||
@adapter_instances ||= {}
|
||||
unless @adapter_instances[klass]
|
||||
@adapter_instances[klass] = klass.new
|
||||
tables.each do |table|
|
||||
@adapters[table.to_s] ||= []
|
||||
@adapters[table.to_s] << [version, @adapter_instances[klass]]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def self.adapters_for_version(version)
|
||||
a = Hash.new([])
|
||||
@adapters.each {|table_name,adapters| a[table_name] = adapters.reject {|i| i[0].to_i <= version.to_i}.map {|j| j[1]} } if defined?(@adapters)
|
||||
a
|
||||
end
|
||||
|
||||
end
|
||||
@@ -1,43 +0,0 @@
|
||||
module Import
|
||||
|
||||
class JsonDecoder
|
||||
|
||||
def initialize(filenames, loader = nil)
|
||||
@filemap = Hash[*
|
||||
filenames.map do |filename|
|
||||
[File.basename(filename, '.*'), filename]
|
||||
end.flatten
|
||||
]
|
||||
@loader = loader || lambda{|filename| Oj.load_file(filename)}
|
||||
end
|
||||
|
||||
def load_schema
|
||||
@loader.call(@filemap['schema'])
|
||||
end
|
||||
|
||||
def each_table
|
||||
@filemap.each do |name, filename|
|
||||
next if name == 'schema'
|
||||
yield name, @loader.call(filename)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def input_stream
|
||||
@input_stream ||= begin
|
||||
end
|
||||
end
|
||||
|
||||
def start( opts )
|
||||
schema = load_schema
|
||||
opts[:callbacks][:schema_info].call( source: schema['schema']['source'], version: schema['schema']['version'], table_count: schema.keys.size - 1)
|
||||
|
||||
each_table do |name, data|
|
||||
info = schema[name]
|
||||
opts[:callbacks][:table_data].call( name, info['fields'], data, info['row_count'] )
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
Reference in New Issue
Block a user