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

Handle tables whose primary keys use non-owned sequences #95

Merged
merged 5 commits into from
Oct 1, 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
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ addons:

before_install:
- sudo sed -i -e '/local.*peer/s/postgres/all/' -e 's/peer\|md5/trust/g' /etc/postgresql/*/main/pg_hba.conf
- sudo sed -i -e "s/^port =.*/port = $PGPORT/" /etc/postgresql/*/main/postgresql.conf
- sudo /etc/init.d/postgresql restart
- sleep 2
- psql -c "CREATE DATABASE chronomodel;" -U postgres
Expand Down
2 changes: 1 addition & 1 deletion lib/chrono_model/adapter/ddl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def remove_history_validity_constraint(table, options = {})
# allow setting the PK to a specific value (think migration scenario).
#
def chrono_create_INSERT_trigger(table, pk, current, history, fields, values)
seq = serial_sequence(current, pk)
seq = pk_and_sequence_for(current).last.to_s

execute <<-SQL
CREATE OR REPLACE FUNCTION chronomodel_#{table}_insert() RETURNS TRIGGER AS $$
Expand Down
4 changes: 2 additions & 2 deletions lib/chrono_model/adapter/migrations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def chrono_make_temporal_table(table_name, options)
end

def chrono_copy_temporal_to_history(table_name, options)
seq = on_history_schema { serial_sequence(table_name, primary_key(table_name)) }
seq = on_history_schema { pk_and_sequence_for(table_name).last.to_s }
from = options[:validity] || '0001-01-01 00:00:00'

execute %[
Expand Down Expand Up @@ -267,7 +267,7 @@ def chrono_undo_temporal_table(table_name)
# Renames a table and its primary key sequence name
#
def rename_table_and_pk(name, new_name)
seq = serial_sequence(name, primary_key(name))
seq = pk_and_sequence_for(name).last.to_s
new_seq = seq.sub(name.to_s, new_name.to_s).split('.').last

execute "ALTER SEQUENCE #{seq} RENAME TO #{new_seq}"
Expand Down
32 changes: 31 additions & 1 deletion spec/chrono_model/adapter/migrations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@
end

describe '.change_table' do

with_temporal_table do
before :all do
adapter.change_table table, :temporal => false
Expand Down Expand Up @@ -127,6 +126,37 @@

it { is_expected.to have_columns([['frupper', 'character varying']]) }
end

# https://github.com/ifad/chronomodel/issues/91
context 'given a table using a sequence not owned by a column' do
before :all do
adapter.execute 'create sequence temporal.foobar owned by none'
adapter.execute "create table #{table} (id integer primary key default nextval('temporal.foobar'::regclass), label character varying)"
end

after :all do
adapter.execute "drop table if exists #{table}"
adapter.execute "drop sequence temporal.foobar"
end

it { is_expected.to have_columns([['id', 'integer'], ['label', 'character varying']]) }

context 'when moving to temporal' do
before :all do
adapter.change_table table, temporal: true
end

after :all do
adapter.drop_table table
end

it { is_expected.to have_columns([['id', 'integer'], ['label', 'character varying']]) }
it { is_expected.to have_temporal_columns([['id', 'integer'], ['label', 'character varying']]) }
it { is_expected.to have_history_columns([['id', 'integer'], ['label', 'character varying']]) }

it { is_expected.to have_function_source("chronomodel_#{table}_insert", /NEW\.id := nextval\('temporal.foobar'\)/) }
end
end
end

describe '.drop_table' do
Expand Down
3 changes: 3 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
require 'support/matchers/column'
require 'support/matchers/index'
require 'support/matchers/function'
require 'support/matchers/source'
require 'support/aruba'

puts "Testing against Active Record #{ActiveRecord::VERSION::STRING} with Arel #{Arel::VERSION}"
Expand All @@ -22,6 +23,8 @@
config.include(ChronoTest::Matchers::Table)
config.include(ChronoTest::Matchers::Column)
config.include(ChronoTest::Matchers::Index)
config.include(ChronoTest::Matchers::Function)
config.include(ChronoTest::Matchers::Source)
config.include(ChronoTest::Aruba, type: :aruba)

ChronoTest.recreate_database!
Expand Down
13 changes: 7 additions & 6 deletions spec/support/matchers/function.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module ChronoTest::Matchers

module Column
module Function
class HaveFunctions < ChronoTest::Matchers::Base
def initialize(functions, schema = 'public')
@functions = functions
Expand Down Expand Up @@ -31,21 +31,21 @@ def failure_message_when_negated

protected
def has_function?(name)
select_value(<<-SQL, [name], 'Check function') == true
select_value(<<-SQL, [@schema, name], 'Check function') == true
SELECT EXISTS(
SELECT 1
FROM pg_catalog.pg_proc p, pg_catalog.pg_namespace n
WHERE p.pronamespace = n.oid
AND n.nspname = 'public'
AND n.nspname = ?
AND p.proname = ?
)
SQL
end

private
def message_matches(message)
(message << ' ').tap do |message|
message << @matches.map do |name, match|
(message << ' ').tap do |m|
m << @matches.map do |name, match|
"a #{name} function"
end.compact.to_sentence
end
Expand All @@ -57,12 +57,13 @@ def have_functions(*args)
end

class HaveHistoryFunctions < HaveFunctions
def initialize
def initialize(schema = 'public')
@function_templates = [
'chronomodel_%s_insert',
'chronomodel_%s_update',
'chronomodel_%s_delete',
]
@schema = schema
end

def matches?(table)
Expand Down
37 changes: 37 additions & 0 deletions spec/support/matchers/source.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module ChronoTest::Matchers

module Source
class HaveFunctionSource < ChronoTest::Matchers::Base
def initialize(function, source_regexp)
@function = function
@regexp = source_regexp
end

def description
"have function source matching #{@regexp}"
end

def matches?(table)
super(table)

source = select_value(<<-SQL, [@function], "Get #@function source")
SELECT prosrc FROM pg_catalog.pg_proc WHERE proname = ?
SQL

!(source =~ @regexp).nil?
end

def failure_message
"expected #{table} to have a #{@function} matching #{@regexp}"
end

def failure_message_when_negated
"expected #{table} to not have a #{@function} matching #{@regexp}"
end
end

def have_function_source(*args)
HaveFunctionSource.new(*args)
end
end
end