Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # calling from task
- rails g task export all
- # example call from task
- # lib/tasks/export.rb
- namespace :export do
- desc "export seeds data"
- task all: :environment do
- Mstudio::DataTools.export_sql({exclude_tables: ['visits','events', 'versions', 'ahoy_events', 'sessions']})
- end
- end
- # exporting it
- RAILS_ENV=production rake export:all
- # next, adding raw sql to seed file:
- # db/seeds.rb
- import_sql(Rails.root.join('db').join('seeds').join('all.sql'))
- def import_sql(path)
- connection = ActiveRecord::Base.connection()
- sql = File.read(path)
- if Rails.env.development?
- statements = sql.split(/;\r$/)
- else
- statements = sql.split(/;$/)
- end
- statements.pop
- ActiveRecord::Base.transaction do
- statements.each do |statement|
- connection.execute(statement)
- end
- end
- end
- # next, importing it to staging or development
- RAILS_ENV=staging rake db:reset
- # lib/extras/mstudio/data_tools.rb
- module Mstudio
- class DataTools
- def self.export_sql(options = {tables: nil, exclude_tables: nil, output_file: nil })
- path = options[:output_file] || Rails.root.join('db').join('seeds').join('all.sql')
- excluded_tables = options[:exclude_tables]
- tables = []
- if options[:tables].nil? || options[:tables].to_s == "*"
- ActiveRecord::Base.connection.tables.each do |table|
- tables << table if table != 'schema_migrations'
- end
- end
- File.open(path, 'w:UTF-8') { |file|
- tables.each do |table|
- if excluded_tables.nil? || !excluded_tables.include?(table)
- model_class = model_class(table)
- # puts "model: #{model_class.inspect}"
- export_table(model_class.order('id asc'), table, file)
- file.write("\n\n")
- end
- end #table
- } # file write
- end
- def self.export_table(records, table, file)
- records.each do |row|
- export_rows(row, file)
- end
- end
- def self.export_rows(model, file)
- data = model.attributes
- fields = []
- values = []
- data.each do |field, value|
- # skip nil for integer issues
- unless value.nil?
- fields << "#{column_delimiter}#{field}#{column_delimiter}"
- if value.blank? && field.end_with?('_at')
- values << "NULL"
- else
- values << "'#{escape_db_value(value)}'"
- end
- end
- end
- if fields.size > 0
- sql = "INSERT INTO #{column_delimiter}#{model.class.table_name}#{column_delimiter} (#{fields.join(',')}) VALUES "+
- "(#{values.join(',')});"
- # block_row.call(model, model.class.table_name, file) unless block_row.nil?
- file.write(sql+"\n")
- end
- sql
- end
- def self.escape_db_value(value)
- return nil if value.blank?
- if value.is_a?(Date) || value.is_a?(Time)
- return value.iso8601
- end
- return value unless value.is_a?(String)
- clean = value.gsub('\'', '\'\'')
- end
- def self.column_delimiter
- case ActiveRecord::Base.connection.adapter_name.downcase.to_sym
- when :mysql
- "`"
- when :sqlite
- "`"
- when :postgresql
- '"'
- else
- raise NotImplementedError, "Unknown adapter type '#{adapter_type}'"
- end
- end
- def self.model_class(table)
- # external gems models
- # case table
- # when 'versions'
- # return ::Version
- # end
- Dir[Rails.root.join("app", "models", "**", "*.rb")].each do |path|
- unless path.include?('concerns')
- require(path)
- end
- # (&method(:require))
- end
- tables = Hash[ActiveRecord::Base.send(:descendants).collect{|c| [c.table_name, c]}]
- raise Exception, "Table missing: #{table}" unless tables.include?(table)
- tables[table]
- end
- def self.model_name(table)
- model = model_class(table)
- model.class_name
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement