<commit_before>
require "sequel"
class Duck_Duck_Duck

  class << self

    def create name, action, sub_sction
      `mkdir -p Server/#{name}/migrates`

      files = Dir.glob("Server/#{name}/migrates/*.sql").sort

      if files.empty?
        ver=1
      else
        last=files.last.split('/').last || "/0"
        ver=last.split('/').last.split('-').first.to_i + 1
      end


      if ver < 10
        ver_str = "00#{ver}"
      elsif ver < 100
        ver_str = "0#{ver}"
      else
        ver_str = "#{ver}"
      end

      sub_str=[name, action, sub_action].compact
      if sub_str.size > 2
        sub_str.shift
      end

      new_file = "Server/#{name}/migrates/#{ver_str}-#{sub_str.join('-')}.sql"
      File.open(new_file, 'a') do |f|
        f.puts "\n\n\n\n-- DOWN\n\n\n\n"
      end

      puts new_file
    end # === def create

    def up
      rec = DB.fetch("SELECT version FROM _schema WHERE name = :name",  :name=>NAME).all.first

      if !rec
        ds = DB["INSERT INTO _schema (name, version) VALUES (?, ?)", NAME, 0]
        ds.insert
        rec = {:version=>0}
      end

      if rec[:version] < 0
        puts "#{NAME} is at invalid version: #{rec[:version]}\n"
        exit 1
      end

      files = FILES.sort.map { |f|
        ver = file_to_ver(f)
        if ver > rec[:version]
          [ ver, File.read(f).split('-- DOWN').first ]
        end
      }.compact

      files.each { |pair|
        ver = pair.first
        sql = pair[1]
        DB << sql
        DB[" UPDATE _schema SET version = ? WHERE name = ? ", ver, NAME].update
        puts "#{NAME} schema is now : #{ver}"
      }

      if files.empty?
        puts "#{NAME} is already the latest: #{rec[:version]}"
      end
    end # === def up

    def migrate_schema
      DB = Sequel.connect(ENV['DATABASE_URL'])

      DB << %!
      CREATE TABLE IF NOT EXISTS _schema (
        name      varchar(255) NOT NULL PRIMARY KEY ,
        version   smallint     NOT NULL DEFAULT 0
      )
      !

      NAME = ARGV[0]
      if !NAME
        puts "Name required."
        exit 1
      end

      def file_to_ver str
        str.split('/').pop.split('-').first.to_i
      end

      FILES = Dir.glob("Server/#{NAME}/migrates/*.sql")
    end # === def migrate_schema

    def down
      rec = DB.fetch("SELECT version FROM _schema WHERE name = :name",  :name=>NAME).all.first

      if !rec
        ds = DB["INSERT INTO _schema (name, version) VALUES (?, ?)", NAME, 0]
        ds.insert
        rec = {:version=>0}
      end

      if rec[:version] == 0
        puts "#{NAME} is already the latest: #{rec[:version]}\n"
        exit 0
      end

      if rec[:version] < 0
        puts "#{NAME} is at invalid version: #{rec[:version]}\n"
        exit 1
      end

      files = FILES.sort.reverse.map { |f|
        ver = file_to_ver(f)

        if ver <= rec[:version]
          [ ver, File.read(f).split('-- DOWN').last ]
        end
      }.compact

      if files.empty?
        puts "#{NAME} is already the latest: #{rec[:version]}\n"
      end

      new_ver = nil

      files.each { |pair|
        ver = pair.first - 1
        sql = pair[1]
        DB << sql
        DB[" UPDATE _schema SET version = ? WHERE name = ? ", ver, NAME].update
        puts "#{NAME} schema is now : #{ver}"
      }

    end # === def down

  end # === class self ===

  def initialize
  end

end # === class Duck_Duck_Duck ===<commit_msg>Fixed: various<commit_after>
require "sequel"

class Duck_Duck_Duck

  SCHEMA_TABLE = ENV['SCHEMA_TABLE'] || '_schema'

  class << self

    def create name, action, sub_sction
      `mkdir -p Server/#{name}/migrates`

      files = Dir.glob("Server/#{name}/migrates/*.sql").sort

      if files.empty?
        ver=1
      else
        last=files.last.split('/').last || "/0"
        ver=last.split('/').last.split('-').first.to_i + 1
      end


      if ver < 10
        ver_str = "00#{ver}"
      elsif ver < 100
        ver_str = "0#{ver}"
      else
        ver_str = "#{ver}"
      end

      sub_str=[name, action, sub_action].compact
      if sub_str.size > 2
        sub_str.shift
      end

      new_file = "Server/#{name}/migrates/#{ver_str}-#{sub_str.join('-')}.sql"
      File.open(new_file, 'a') do |f|
        f.puts "\n\n\n\n-- DOWN\n\n\n\n"
      end

      puts new_file
    end # === def create

    def migrate_schema name
      DB = Sequel.connect(ENV['DATABASE_URL'])

      DB << <<-EOF
        CREATE TABLE IF NOT EXISTS #{SCHEMA_TABLE} (
          name      varchar(255) NOT NULL PRIMARY KEY ,
          version   smallint     NOT NULL DEFAULT 0
        )
      EOF

      def file_to_ver str
        str.split('/').pop.split('-').first.to_i
      end

      @files = Dir.glob("Server/#{name}/migrates/*.sql")
    end # === def migrate_schema

    def up name
      migrate_schema name

      rec = DB.fetch("SELECT version FROM #{SCHEMA_TABLE} WHERE name = :name",  :name=>name).all.first

      if !rec
        ds = DB["INSERT INTO #{SCHEMA_TABLE} (name, version) VALUES (?, ?)", name, 0]
        ds.insert
        rec = {:version=>0}
      end

      if rec[:version] < 0
        puts "#{name} has an invalid version: #{rec[:version]}\n"
        exit 1
      end

      files = @files.sort.map { |f|
        ver = file_to_ver(f)
        if ver > rec[:version]
          [ ver, File.read(f).split('-- DOWN').first ]
        end
      }.compact

      files.each { |pair|
        ver = pair.first
        sql = pair[1]
        DB << sql
        DB[" UPDATE #{SCHEMA_TABLE} SET version = ? WHERE name = ? ", ver, name].update
        puts "#{name} schema is now : #{ver}"
      }

      if files.empty?
        puts "#{name} is already the latest: #{rec[:version]}"
      end
    end # === def up

    def down name
      migrate_schema name

      rec = DB.fetch("SELECT version FROM #{SCHEMA_TABLE} WHERE name = :name",  :name=>NAME).all.first

      if !rec
        ds = DB["INSERT INTO #{SCHEMA_TABLE} (name, version) VALUES (?, ?)", NAME, 0]
        ds.insert
        rec = {:version=>0}
      end

      if rec[:version] == 0
        puts "#{NAME} is already the latest: #{rec[:version]}\n"
        exit 0
      end

      if rec[:version] < 0
        puts "#{NAME} is at invalid version: #{rec[:version]}\n"
        exit 1
      end

      files = @files.sort.reverse.map { |f|
        ver = file_to_ver(f)

        if ver <= rec[:version]
          [ ver, File.read(f).split('-- DOWN').last ]
        end
      }.compact

      if files.empty?
        puts "#{NAME} is already the latest: #{rec[:version]}\n"
      end

      new_ver = nil

      files.each { |pair|
        ver = pair.first - 1
        sql = pair[1]
        DB << sql
        DB[" UPDATE #{SCHEMA_TABLE} SET version = ? WHERE name = ? ", ver, NAME].update
        puts "#{NAME} schema is now : #{ver}"
      }

    end # === def down

  end # === class self ===

  def initialize
  end

end # === class Duck_Duck_Duck ===
{'fraction_non_alphanumeric': 0.14909248055315472, 'fraction_numerical': 0.00864304235090752, 'mean_word_length': 1.786115569823435, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6235067', 'n_tokens_mistral': 2410, 'n_tokens_neox': 2265, 'n_words': 617}