Sha256: 8149d0e188d8908ef6e27416ee87fe42b58adae89378e7a887727650ac7af259

Contents?: true

Size: 1.67 KB

Versions: 23

Compression:

Stored size: 1.67 KB

Contents

# frozen_string_literal: true

# database handling for eDen Db
class Database
  class << self
    Sequel.extension :migration

    def db_access
      Sequel.connect("sqlite://eden.sqlite3")
    end

    def create_table(table_name, type)
      eden = Sequel.connect("sqlite://eden.sqlite3")
      type= case type

            when 'string'
              String
            when 'int'
              Integer
            when 'hash'
              JSON
            when 'date'
              DateTime
            else
              Integer
      end
      unless eden.table_exists?(table_name)
        eden.create_table table_name.to_sym do
          # primary_key "#{table_name}_id".to_sym
          column "#{table_name}_id".to_sym, type, primary_key: true
        end
      end
    end

    # def create_column(table, column_name, type)
    #   eden = Sequel.connect("sqlite://eden.sqlite3")
    #   if eden.table_exists?(table)
    #     unless eden.schema(table).any? { |column| column.first == column_name }
    #       Sequel.migration do
    #         change do
    #           add_column table, column_name, type
    #         end
    #       end.apply(eden, :up)
    #     end
    #   end
    # end
    def create_column(table, column_name, type)
      eden = Sequel.connect("sqlite://eden.sqlite3")
      if eden.table_exists?(table)
        begin
          Sequel.migration do
            change do
              add_column table, column_name, type unless eden.schema(table).any? { |column| column.first == column_name }
            end
          end.apply(eden, :up)
        rescue Sequel::DatabaseError => e
          puts "error adding column  : #{e.message}"
        end
      end
    end
  end

end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
atome-0.5.7.1.0 vendor/assets/server/database.rb
atome-0.5.7.0.7 vendor/assets/server/database.rb
atome-0.5.7.0.5 vendor/assets/server/database.rb
atome-0.5.7.0.4 vendor/assets/server/database.rb
atome-0.5.7.0.3 vendor/assets/server/database.rb
atome-0.5.7.0.2 vendor/assets/server/database.rb
atome-0.5.6.8.7 vendor/assets/server/database.rb
atome-0.5.6.8.6 vendor/assets/server/database.rb
atome-0.5.6.8.5 vendor/assets/server/database.rb
atome-0.5.6.8.3 vendor/assets/server/database.rb
atome-0.5.6.7.8 vendor/assets/server/database.rb
atome-0.5.6.7.7 vendor/assets/server/database.rb
atome-0.5.6.7.6 vendor/assets/server/database.rb
atome-0.5.6.7.5 vendor/assets/server/database.rb
atome-0.5.6.7.4 vendor/assets/server/database.rb
atome-0.5.6.7.3 vendor/assets/server/database.rb
atome-0.5.6.6.9 vendor/assets/server/database.rb
atome-0.5.6.6.7 vendor/assets/server/database.rb
atome-0.5.6.6.5 vendor/assets/server/database.rb
atome-0.5.6.6.1 vendor/assets/server/database.rb