Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Full support for Int16 #131

Merged
merged 2 commits into from
Jul 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions db/migrations/20190702125912_create_menu_options.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class CreateMenuOptions::V20190702125912 < Avram::Migrator::Migration::V1
def migrate
create table_for(MenuOption) do
primary_key id : Int16
add_timestamps
add title : String
add option_value : Int16
end
end

def rollback
drop :menu_options
end
end
14 changes: 13 additions & 1 deletion spec/migrator/create_table_statement_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ describe Avram::Migrator::CreateTableStatement do
CREATE TABLE users (
custom_id_name bigserial PRIMARY KEY);
SQL

built = Avram::Migrator::CreateTableStatement.new(:users).build do
primary_key id : Int16
end

built.statements.size.should eq 1
built.statements.first.should eq <<-SQL
CREATE TABLE users (
id smallserial PRIMARY KEY);
SQL
end

it "sets default values" do
Expand All @@ -75,6 +85,7 @@ describe Avram::Migrator::CreateTableStatement do
add meta : JSON::Any, default: JSON::Any.new(Hash(String, JSON::Any).new)
add joined_at : Time, default: :now
add future_time : Time, default: Time.local
add friend_count : Int16, default: 1
end

built.statements.size.should eq 1
Expand All @@ -88,7 +99,8 @@ describe Avram::Migrator::CreateTableStatement do
completed boolean NOT NULL DEFAULT 'false',
meta jsonb NOT NULL DEFAULT '{}',
joined_at timestamptz NOT NULL DEFAULT NOW(),
future_time timestamptz NOT NULL DEFAULT '#{Time.local.to_utc}');
future_time timestamptz NOT NULL DEFAULT '#{Time.local.to_utc}',
friend_count smallint NOT NULL DEFAULT '1');
SQL
end

Expand Down
6 changes: 6 additions & 0 deletions spec/support/boxes/menu_option_box.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class MenuOptionBox < BaseBox
def initialize
title "Option"
option_value 1_i16
end
end
11 changes: 11 additions & 0 deletions spec/support/menu_option.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class MenuOption < Avram::Model
skip_default_columns

table do
primary_key id : Int16
timestamps

column title : String
column option_value : Int16
end
end
12 changes: 12 additions & 0 deletions spec/type_extension_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ class SaveCompany < Company::SaveOperation
end
end

class MenuOptionQuery < MenuOption::BaseQuery
end

describe "TypeExtensions" do
it "should work in boxes" do
CompanyBox.create
Expand All @@ -38,4 +41,13 @@ describe "TypeExtensions" do
using_earnings = CompanyQuery.new.earnings(1).first
using_earnings.earnings.should eq 1.0
end

it "Int16 should allow querying with Int32" do
MenuOptionBox.create &.title("test").option_value(4_i16)
opt = MenuOptionQuery.new.option_value(4).first
opt.option_value.should eq 4_i16

opt = MenuOptionQuery.new.option_value(33000).first?
opt.should eq nil
end
end
6 changes: 6 additions & 0 deletions src/avram/blank_extensions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ struct Time
end
end

struct Int16
def blank?
nil?
end
end

struct Int32
def blank?
nil?
Expand Down
31 changes: 31 additions & 0 deletions src/avram/charms/int16_extensions.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
struct Int16
module Lucky
alias ColumnType = Int16
include Avram::Type

def self.from_db!(value : Int16)
value
end

def self.parse(value : Int16)
SuccessfulCast(Int16).new(value)
end

def self.parse(value : String)
SuccessfulCast(Int16).new value.to_i16
rescue ArgumentError
FailedCast.new
end

def self.parse(value : Int32)
SuccessfulCast(Int16).new value.to_i16
end

def self.to_db(value : Int16)
value.to_s
end

class Criteria(T, V) < Avram::Criteria(T, V)
end
end
end
14 changes: 14 additions & 0 deletions src/avram/migrator/columns/int16_column.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require "./base"

module Avram::Migrator::Columns
class Int16Column < Base
@default : Int16 | Int32 | Nil = nil

def initialize(@name, @nilable, @default)
end

def column_type
"smallint"
end
end
end
12 changes: 12 additions & 0 deletions src/avram/migrator/columns/primary_keys/int16_primary_key.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require "./base"

module Avram::Migrator::Columns::PrimaryKeys
class Int16PrimaryKey < Base
def initialize(@name)
end

def column_type : String
"smallserial"
end
end
end