From 913506bf47bc352b7d23ad371b9ed89b73cc28c1 Mon Sep 17 00:00:00 2001 From: "Tj (bougyman) Vanderpoel" Date: Sat, 15 Feb 2025 17:23:38 -0600 Subject: [PATCH] feat: Add `outboxify` cli --- Gemfile | 1 + Readme.adoc | 40 ++++++++++++++++++++++++++++++++++++++++ ci/build_image.sh | 4 ++-- exe/outboxify | 29 +++++++++++++++++++++++++++++ 4 files changed, 72 insertions(+), 2 deletions(-) create mode 100755 exe/outboxify diff --git a/Gemfile b/Gemfile index 2d81450..0cb1181 100644 --- a/Gemfile +++ b/Gemfile @@ -8,6 +8,7 @@ gemspec gem 'minitest', '~> 5.16' gem 'minitest-global_expectations' gem 'pg' +gem 'pry' gem 'rake', '~> 13.0' gem 'rubocop', '~> 1.21' gem 'rubocop-minitest' diff --git a/Readme.adoc b/Readme.adoc index 7866eb7..24b6a4a 100644 --- a/Readme.adoc +++ b/Readme.adoc @@ -63,6 +63,46 @@ irb(main):007> DB[:foo_outbox].first metadata: nil} ``` +### Using `outboxify` + +```bash + % psql spgt_test +psql (16.4) +Type "help" for help. + +spgt_test=# create table foo (id serial primary key, s text, i integer); +CREATE TABLE +spgt_test=# \d + List of relations + Schema | Name | Type | Owner +--------+------------+----------+---------- + public | foo | table | bougyman + public | foo_id_seq | sequence | bougyman +(2 rows) + +spgt_test=# \q +bougyman@framezotz ~/rubyists/sequel-pgt_outbox + % be ./exe/outboxify postgres:///spgt_test foo +bougyman@framezotz ~/rubyists/sequel-pgt_outbox + % psql spgt_test +psql (16.4) +Type "help" for help. + +spgt_test=# \d foo + Table "public.foo" + Column | Type | Collation | Nullable | Default +--------+---------+-----------+----------+--------------------------------- + id | integer | | not null | nextval('foo_id_seq'::regclass) + s | text | | | + i | integer | | | +Indexes: + "foo_pkey" PRIMARY KEY, btree (id) +Triggers: + pgt_outbox_foo_outbox AFTER INSERT OR DELETE OR UPDATE ON foo FOR EACH ROW EXECUTE FUNCTION pgt_outbox_foo_outbox() + +spgt_test=# +``` + ## Development After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. diff --git a/ci/build_image.sh b/ci/build_image.sh index 359ad9d..3d044bd 100755 --- a/ci/build_image.sh +++ b/ci/build_image.sh @@ -209,7 +209,7 @@ fi service=$(basename "$owner_and_repo" .git) owner=$(dirname "$owner_and_repo") -full_tag=$IMAGE_NAME:$tag-ruby$RUBY_VERSION-$UBUNTU_VERSION +full_tag=$IMAGE_NAME:$tag # Pass any extra arguments to the build command ("$@" contains the rest of the arguments) $runtime build --tag "$full_tag" "$@" \ --label org.opencontainers.image.created="$(date --utc --iso-8601=seconds)" \ @@ -237,7 +237,7 @@ fi mapfile -t tags < <(echo "$tag" | awk -F'.' 'NF==3{print; print $1"."$2; print $1; next} NF==2{print; print $1; next} {print}') for t in "${tags[@]}" do - new_tag=$IMAGE_NAME:$t-ruby$RUBY_VERSION-$UBUNTU_VERSION + new_tag=$IMAGE_NAME:$t registry_image_name="$REGISTRY/$owner/$new_tag" if [ "$runtime" = "podman" ] then diff --git a/exe/outboxify b/exe/outboxify new file mode 100755 index 0000000..8484dd9 --- /dev/null +++ b/exe/outboxify @@ -0,0 +1,29 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +require 'sequel/pgt_outbox' +require 'optparse' + +opts = OptionParser.new do |o| + o.banner = 'Usage: outboxify [options] ' +end + +db_uri = ARGV.shift +if db_uri.nil? + warn 'missing db uri' + puts opts + exit 1 +end + +DB = Sequel.connect db_uri +DB.extension :pgt_outbox + +table = ARGV.shift +if table.nil? + warn 'missing table' + puts opts + exit 2 +end + +function = DB.pgt_outbox_setup(table) +DB.pgt_outbox_events(table, function)