Module: ActiveRecord::BigQuerySchemaMigration

Defined in:
lib/active_record/connection_adapters/bigquery_adapter.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/active_record/connection_adapters/bigquery_adapter.rb', line 187

def self.included base
  attr_accessor :migration_file_pwd
  base.instance_eval do
    def schema_migration_hash
      file = schema_migration_file("r")
      json = JSON.parse(file.read)
    end

    def schema_migration_path
      Dir.pwd + "/db/schema_migrations.json"
    end

    def schema_migration_file(mode="w+")
      file_pwd = Dir.pwd + "/db/schema_migrations.json"
      File.open( file_pwd, mode )
    end

    def create_table(limit=nil)
      @migration_file_pwd = Dir.pwd + "/db/schema_migrations.json"
      unless File.exists?(@migration_file_pwd)
        puts "SCHEMA MIGRATION HERE"
        version_options = {null: false}
        version_options[:limit] = limit if limit

        #connection.create_table(table_name, id: false) do |t|
        #  t.column :version, :string, version_options
        #end
        file = schema_migration_file
        file.puts({ db:{ table_name.to_sym => [] } }.to_json )
        file.close
        #connection.add_index table_name, :version, unique: true, name: index_name
      end
    end

    #def self.drop_table
    #  binding.pry
    #  File.delete(schema_migration_path)
    #end

    def delete_version(options)
      #versions = ActiveRecord::SchemaMigration.where(:version => version.to_s)
      version = options[:version]
      new_data = SchemaMigration.schema_migration_hash["db"]["schema_migrations"].delete_if{|o| o["version"] == version.to_s}
      hsh = {:db=>{:schema_migrations => new_data } }
      f = schema_migration_file
      f.puts hsh.to_json
      f.close
    end

    def create!(args, *opts)
      current_data = schema_migration_hash
      unless schema_migration_hash["db"]["schema_migrations"].map{|o| o["version"]}.include?(args[:version].to_s)
        hsh = {:db=>{:schema_migrations => current_data["db"]["schema_migrations"] << args } }
        f = schema_migration_file
        f.puts hsh.to_json
        f.close
      end
      true
    end

    def all
      schema_migration_hash["db"]["schema_migrations"]
    end

    def where(args)
      all.select{|o| o[args.keys.first.to_s] == args.values.first}
    end
  end
end