class ActiveSchema::Schema
Public Class Methods
new(connection, table_name, table_opts, &schema)
click to toggle source
# File lib/active_schema/schema.rb, line 4 def initialize connection, table_name, table_opts, &schema @connection, @table_name, @table_opts, @schema = \ connection, table_name, table_opts, schema @strict = @table_opts.keys.map(&:to_sym).include?(:strict) ? @table_opts.delete(:strict) : true @columns, @indexes = {}, {} yield_schema table = Table.new(@connection, @table_name, @table_opts, @columns) table.exists? ? handle_columns : table.create handle_indexes end
Public Instance Methods
column(name, type, opts = {})
click to toggle source
# File lib/active_schema/schema.rb, line 46 def column name, type, opts = {} @columns[name.to_s] = [type, opts] end
index(columns, opts = {})
click to toggle source
# File lib/active_schema/schema.rb, line 50 def index columns, opts = {} @indexes[Array(columns).map(&:to_sym)] = opts end
method_missing(type, name, opts = {})
click to toggle source
# File lib/active_schema/schema.rb, line 54 def method_missing type, name, opts = {} column(name, type, opts) end
strict?()
click to toggle source
by default ActiveSchema
will drop db columns that’s not defined in schema. so the table will reflect the exact structure of your schema use ‘strict: false` option to allow columns not defined in schema.
@example
# => \d users # Table "public.users" # Column | Type # ----------------------+------------------------ # id | integer # email | character varying(255) # first_name | character varying(255) # last_name | character varying(255) # username | character varying(255) schema :users, strict: false do |t| # this schema WILL KEEP username column t.string :email t.string :first_name t.string :last_name end schema :users do |t| # this schema WILL DROP username column t.string :email t.string :first_name t.string :last_name end
# File lib/active_schema/schema.rb, line 44 def strict?; @strict end
Private Instance Methods
handle_columns()
click to toggle source
# File lib/active_schema/schema.rb, line 63 def handle_columns @columns.each_pair do |column, (type, opts)| Column.new(@connection, @table_name, column, type, opts) end Column::Pruner.new(@connection, @table_name, @columns.keys) if strict? end
handle_indexes()
click to toggle source
# File lib/active_schema/schema.rb, line 70 def handle_indexes @indexes.each_pair do |columns, opts| Index.new(@connection, @table_name, columns, opts) end Index::Pruner.new(@connection, @table_name, @indexes.keys) if strict? end
yield_schema()
click to toggle source
# File lib/active_schema/schema.rb, line 59 def yield_schema @schema.call(self) if @schema end