Skip to content

Commit

Permalink
Allows to defined int64 primart field
Browse files Browse the repository at this point in the history
  • Loading branch information
imdrasil committed Jul 5, 2018
1 parent b764126 commit d6a49c1
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 17 deletions.
@@ -1,6 +1,7 @@
class AddModelWithOneField20170815112803321 < Jennifer::Migration::Base
def up
create_table(:one_field_models) do |t|
create_table(:one_field_models, false) do |t|
t.bigint :id, { :primary => true, :auto_increment => true }
end
end

Expand Down
6 changes: 6 additions & 0 deletions examples/run.cr
Expand Up @@ -4,6 +4,12 @@ require "./migrations/*"
require "sam"
require "../src/jennifer/sam"

Jennifer::Config.configure do |conf|
# conf.logger = Logger.new(STDOUT)
conf.logger.level = Logger::DEBUG
# conf.logger.level = Logger::ERROR
end

Sam.namespace "script" do
task "drop_models" do
Jennifer::Model::Base.models.select(&.has_table?).each(&.all.delete)
Expand Down
2 changes: 1 addition & 1 deletion spec/model/relation_definition_spec.cr
Expand Up @@ -22,7 +22,7 @@ describe Jennifer::Model::RelationDefinition do
ContactWithDependencies::CALLBACKS[:destroy][:before].includes?("__delete_callback_addresses").should be_true
end

it "doen't invoke callbacks on associated model" do
it "doesn't invoke callbacks on associated model" do
c = Factory.create_contact
Factory.create_address(contact_id: c.id)
count = Address.destroy_counter
Expand Down
4 changes: 2 additions & 2 deletions spec/models.cr
Expand Up @@ -260,7 +260,7 @@ end

class OneFieldModel < Jennifer::Model::Base
mapping(
id: Primary32
id: Primary64
)
end

Expand Down Expand Up @@ -340,7 +340,7 @@ class OneFieldModelWithExtraArgument < Jennifer::Model::Base
table_name "one_field_models"

mapping(
id: Primary32,
id: Primary64,
missing_field: String
)
end
Expand Down
2 changes: 1 addition & 1 deletion src/jennifer/adapter/base.cr
Expand Up @@ -113,7 +113,7 @@ module Jennifer
exec(*parsed_query)
if klass.primary_auto_incrementable?
klass.all.order({klass.primary => :desc}).limit(collection.size).pluck(:id).reverse_each.each_with_index do |id, i|
collection[i].init_primary_field(id)
collection[i].init_primary_field(id.as(Int))
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions src/jennifer/adapter/postgres.cr
Expand Up @@ -24,7 +24,7 @@ module Jennifer
:double => "double precision", # Float64

:numeric => "numeric", # PG::Numeric
:decimal => "decimal", # PG::Numeric - is alias for numeric
:decimal => "decimal", # PG::Numeric - alias for numeric

:string => "varchar",
:char => "char",
Expand Down Expand Up @@ -190,7 +190,7 @@ module Jennifer
id = -1i64
affected = 0i64
if obj.class.primary_auto_incrementable?
id = scalar(*query_opts).as(Int32).to_i64
id = scalar(*query_opts).as(Int).to_i64
affected += 1 if id > 0
else
affected = exec(*query_opts).rows_affected
Expand Down
15 changes: 9 additions & 6 deletions src/jennifer/adapter/postgres/schema_processor.cr
Expand Up @@ -121,13 +121,8 @@ module Jennifer
end

private def column_type_definition(options, io)
type = if options[:serial]? || options[:auto_increment]?
"serial"
else
options[:sql_type]? || adapter.translate_type(options[:type].as(Symbol))
end
size = options[:size]? || adapter.default_type_size(options[:type]?)
io << " " << type
io << " " << column_type(options)
io << "(#{size})" if size
io << " ARRAY" if options[:array]?
end
Expand All @@ -142,6 +137,14 @@ module Jennifer
raise ArgumentError.new("Unknown index type: #{name}")
end
end

private def column_type(options)
if options[:serial]? || options[:auto_increment]?
options[:type] == :bigint ? "bigserial" : "serial"
else
options[:sql_type]? || adapter.translate_type(options[:type].as(Symbol))
end
end
end
end
end
2 changes: 1 addition & 1 deletion src/jennifer/model/base.cr
Expand Up @@ -265,7 +265,7 @@ module Jennifer
private def store_record : Bool
return false unless __before_create_callback
res = self.class.adapter.insert(self)
init_primary_field(res.last_insert_id.to_i) if primary.nil? && res.last_insert_id > -1
init_primary_field(res.last_insert_id.as(Int)) if primary.nil? && res.last_insert_id > -1
raise ::Jennifer::BaseException.new("Record hasn't been stored to the db") if res.rows_affected == 0
@new_record = false
__after_create_callback
Expand Down
8 changes: 5 additions & 3 deletions src/jennifer/model/mapping.cr
Expand Up @@ -64,13 +64,15 @@ module Jennifer
{{value[:parsed_type].id}}
end

# Inits primary field
def init_primary_field(value)
def init_primary_field(value : Int)
{% if primary_auto_incrementable %}
raise ::Jennifer::AlreadyInitialized.new(@{{key.id}}, value) if @{{key.id}}
@{{key.id}} = value.as({{value[:type]}})
@{{key.id}} = value{% if value[:parsed_type] =~ /32/ %}.to_i{% else %}.to_i64{% end %}
{% end %}
end

# Inits primary field
def init_primary_field(value); end
{% end %}
{% end %}
{% end %}
Expand Down

0 comments on commit d6a49c1

Please sign in to comment.