Skip to content

Commit

Permalink
fix: allow functions to be default values
Browse files Browse the repository at this point in the history
  • Loading branch information
nownabe committed May 16, 2023
1 parent 02a6171 commit 875fc04
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
22 changes: 22 additions & 0 deletions acceptance/cases/models/default_value_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

require "test_helper"
require "test_helpers/with_separate_database"

module Models
class DefaultValueTest < SpannerAdapter::TestCase
include TestHelpers::WithSeparateDatabase

class DynamicItem < ActiveRecord::Base; end

def test_dynamic_default_values
connection.create_table :dynamic_items do |t|
t.column :col_timestamp, :datetime, default: -> { "CURRENT_TIMESTAMP()" }
end

item = DynamicItem.create!
item.reload
assert(item.col_timestamp)
end
end
end
12 changes: 10 additions & 2 deletions lib/activerecord_spanner_adapter/information_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ def table_columns table_name, column_name: nil
column_name = row["COLUMN_NAME"]
options = column_options[column_name]

default = row["COLUMN_DEFAULT"]
default_function = row["GENERATION_EXPRESSION"]

if /\w+\(.*\)/.match?(default)
default_function ||= default
default = nil
end

Table::Column.new \
table_name,
column_name,
Expand All @@ -87,8 +95,8 @@ def table_columns table_name, column_name: nil
allow_commit_timestamp: options["allow_commit_timestamp"],
ordinal_position: row["ORDINAL_POSITION"],
nullable: row["IS_NULLABLE"] == "YES",
default: row["COLUMN_DEFAULT"],
default_function: row["GENERATION_EXPRESSION"],
default: default,
default_function: default_function,
generated: row["GENERATION_EXPRESSION"].present?
end
end
Expand Down

0 comments on commit 875fc04

Please sign in to comment.