Skip to content

Commit

Permalink
fix: allow functions to be default values (#252)
Browse files Browse the repository at this point in the history
* docs: add examples for acceptance test

* fix: allow functions to be default values

* ci: Fix emulator version to 1.5.0

Because newer Spanner emulators have a bug aroud information schema.
They cannot return correct values for COLUMN_DEFAULT.

spanner> SELECT COLUMN_NAME, COLUMN_DEFAULT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME="dynamic_items";
+---------------+--------------------+
| COLUMN_NAME   | COLUMN_DEFAULT     |
+---------------+--------------------+
| id            | NULL               |
| col_timestamp | CURRENT_TIMESTAMP( |
+---------------+--------------------+
2 rows in set (2.079298ms)

* build: use latest emulator version now that the related bug is fixed

* chore: added missing 'the'

* ci: add AR v7.0.5 to nightly tests

---------

Co-authored-by: nownabe <nownabe@users.noreply.github.com>
Co-authored-by: Knut Olav Løite <koloite@gmail.com>
  • Loading branch information
3 people committed Jun 1, 2023
1 parent ecf3ea4 commit f651bbf
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 5 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/nightly-acceptance-tests-on-emulator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
matrix:
# Run acceptance tests all supported combinations of Ruby and ActiveRecord.
ruby: [2.6, 2.7, 3.0, 3.1, 3.2]
ar: [6.0.0, 6.0.1, 6.0.2.2, 6.0.3.7, 6.0.4, 6.1.3.2, 6.1.4.7, 6.1.5.1, 6.1.6.1, 7.0.2.4, 7.0.3.1, 7.0.4]
ar: [6.0.0, 6.0.1, 6.0.2.2, 6.0.3.7, 6.0.4, 6.1.3.2, 6.1.4.7, 6.1.5.1, 6.1.6.1, 7.0.2.4, 7.0.3.1, 7.0.4, 7.0.5]
# Exclude combinations that are not supported.
exclude:
- ruby: 3.0
Expand Down Expand Up @@ -59,6 +59,8 @@ jobs:
ar: 7.0.3.1
- ruby: 2.6
ar: 7.0.4
- ruby: 2.6
ar: 7.0.5
env:
AR_VERSION: ${{ matrix.ar }}
steps:
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/nightly-unit-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
matrix:
# Run unit tests all supported combinations of Ruby and ActiveRecord.
ruby: [2.6, 2.7, 3.0, 3.1, 3.2]
ar: [6.0.0, 6.0.1, 6.0.2.2, 6.0.3.7, 6.0.4, 6.1.3.2, 6.1.4.7, 6.1.5.1, 6.1.6.1, 7.0.2.4, 7.0.3.1, 7.0.4]
ar: [6.0.0, 6.0.1, 6.0.2.2, 6.0.3.7, 6.0.4, 6.1.3.2, 6.1.4.7, 6.1.5.1, 6.1.6.1, 7.0.2.4, 7.0.3.1, 7.0.4, 7.0.5]
# Exclude combinations that are not supported.
exclude:
- ruby: 3.0
Expand Down Expand Up @@ -51,6 +51,8 @@ jobs:
ar: 7.0.3.1
- ruby: 2.6
ar: 7.0.4
- ruby: 2.6
ar: 7.0.5
env:
AR_VERSION: ${{ matrix.ar }}
steps:
Expand Down
18 changes: 17 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,26 @@ Variable|Description|Comment
`activerecord_tests`

#### Example

```shell
bundle exec rake acceptance[appdev-soda-spanner-staging,/home/Downloads/creds.json,activerecord_tests]
```

You can also use the [Cloud Spanner emulator](https://cloud.google.com/spanner/docs/emulator).

```shell
docker run -d --rm -p 9010:9010 gcr.io/cloud-spanner-emulator/emulator
export SPANNER_EMULATOR_HOST=localhost:9010
bundle exec rake "acceptance[dummy-project,,dummy-instance,]"
```

If you want to run only one test, you can specify a test file.

```shell
bundle exec rake "acceptance[dummy-project,,dummy-instance,]" \
TEST=acceptance/cases/models/default_value_test.rb
```

## Coding Style

Please follow the established coding style in the library. The style is is
Expand All @@ -76,4 +92,4 @@ The rubocop settings depend on [googleapis/ruby-style](https://github.com/google

Please note that this project is released with a Contributor Code of Conduct. By
participating in this project you agree to abide by its terms. See
[Code of Conduct](CODE_OF_CONDUCT.md) for more information.
[Code of Conduct](CODE_OF_CONDUCT.md) for more information.
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 f651bbf

Please sign in to comment.