-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add database tasks test coverage (#5)
* Move TestHelper to separate file * Add tests for PostgreSQLProxy database tasks
- Loading branch information
1 parent
c6e6aee
commit f4d8188
Showing
9 changed files
with
414 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
SET statement_timeout = 0; | ||
SET lock_timeout = 0; | ||
SET idle_in_transaction_session_timeout = 0; | ||
SET client_encoding = 'UTF8'; | ||
SET standard_conforming_strings = on; | ||
SELECT pg_catalog.set_config('search_path', '', false); | ||
SET check_function_bodies = false; | ||
SET xmloption = content; | ||
SET client_min_messages = warning; | ||
SET row_security = off; | ||
|
||
-- | ||
-- Name: public; Type: SCHEMA; Schema: -; Owner: - | ||
-- | ||
|
||
-- *not* creating schema, since initdb creates it | ||
|
||
|
||
SET default_tablespace = ''; | ||
|
||
SET default_table_access_method = heap; | ||
|
||
-- | ||
-- Name: users; Type: TABLE; Schema: public; Owner: - | ||
-- | ||
|
||
CREATE TABLE public.users ( | ||
id integer NOT NULL, | ||
name text NOT NULL, | ||
email text NOT NULL, | ||
created_at timestamp without time zone DEFAULT now() NOT NULL, | ||
updated_at timestamp without time zone DEFAULT now() NOT NULL | ||
); | ||
|
||
|
||
-- | ||
-- Name: users_id_seq; Type: SEQUENCE; Schema: public; Owner: - | ||
-- | ||
|
||
CREATE SEQUENCE public.users_id_seq | ||
AS integer | ||
START WITH 1 | ||
INCREMENT BY 1 | ||
NO MINVALUE | ||
NO MAXVALUE | ||
CACHE 1; | ||
|
||
|
||
-- | ||
-- Name: users_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: - | ||
-- | ||
|
||
ALTER SEQUENCE public.users_id_seq OWNED BY public.users.id; | ||
|
||
|
||
-- | ||
-- Name: users id; Type: DEFAULT; Schema: public; Owner: - | ||
-- | ||
|
||
ALTER TABLE ONLY public.users ALTER COLUMN id SET DEFAULT nextval('public.users_id_seq'::regclass); | ||
|
||
|
||
-- | ||
-- Name: users users_pkey; Type: CONSTRAINT; Schema: public; Owner: - | ||
-- | ||
|
||
ALTER TABLE ONLY public.users | ||
ADD CONSTRAINT users_pkey PRIMARY KEY (id); | ||
|
||
|
||
-- | ||
-- PostgreSQL database dump complete | ||
-- | ||
|
||
SET search_path TO "$user", public; | ||
|
129 changes: 129 additions & 0 deletions
129
spec/active_record/tasks/postgresql_proxy_database_tasks_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
# frozen_string_literal: true | ||
|
||
# rubocop:disable RSpec/MultipleMemoizedHelpers | ||
RSpec.describe ActiveRecord::Tasks::PostgreSQLProxyDatabaseTasks do # rubocop:disable RSpec/SpecFilePathFormat | ||
let(:public_schema_config) do | ||
configuration.configuration_hash.merge(adapter: "postgresql", database: "postgres", schema_search_path: "public") | ||
end | ||
let(:configuration) do | ||
TestHelper.postgresql_database_tasks_configuration | ||
end | ||
|
||
def database_exists? | ||
proc do | ||
with_master_connection do |conn| | ||
conn.select_value <<~SQL.squish | ||
SELECT COUNT(*)::int::boolean | ||
FROM pg_database WHERE datname = '#{configuration.database}'; | ||
SQL | ||
end | ||
end | ||
end | ||
|
||
def schema_loaded? | ||
proc do | ||
any_tables = TestHelper::PostgreSQLDatabaseTaskRecord.connection.tables.any? | ||
TestHelper::PostgreSQLDatabaseTaskRecord.connection_pool.disconnect! | ||
|
||
any_tables | ||
end | ||
end | ||
|
||
def with_master_connection(&) | ||
pool = ActiveRecord::Base.connection_handler.establish_connection(public_schema_config, | ||
role: :admin) | ||
pool.with_connection(&) | ||
ensure | ||
pool.disconnect | ||
end | ||
|
||
describe "#drop" do | ||
subject(:drop) { ActiveRecord::Tasks::DatabaseTasks.drop(configuration) } | ||
|
||
before do | ||
ActiveRecord::Tasks::DatabaseTasks.create(configuration) | ||
end | ||
|
||
it "drops the database" do | ||
expect { drop }.to change(&database_exists?).from(true).to(false) | ||
end | ||
end | ||
|
||
describe "#create" do | ||
subject(:create) { ActiveRecord::Tasks::DatabaseTasks.create(configuration) } | ||
|
||
before do | ||
ActiveRecord::Tasks::DatabaseTasks.drop(configuration) | ||
end | ||
|
||
after do | ||
ActiveRecord::Tasks::DatabaseTasks.drop(configuration) | ||
end | ||
|
||
it "creates the database" do | ||
expect { create }.to change(&database_exists?).from(false).to(true) | ||
end | ||
end | ||
|
||
describe "#structure_load" do | ||
subject(:structure_load) do | ||
ActiveRecord::Tasks::DatabaseTasks.structure_load(configuration, "db/postgresql_structure.sql") | ||
end | ||
|
||
before do | ||
ActiveRecord::Tasks::DatabaseTasks.create(configuration) | ||
end | ||
|
||
after do | ||
ActiveRecord::Tasks::DatabaseTasks.drop(configuration) | ||
end | ||
|
||
it "loads the schema" do | ||
expect { structure_load }.to change(&schema_loaded?).from(false).to(true) | ||
end | ||
end | ||
|
||
describe "#structure_dump" do | ||
subject(:structure_dump) { ActiveRecord::Tasks::DatabaseTasks.structure_dump(configuration, dump_out) } | ||
|
||
let(:dump_out) { temp_file.path } | ||
let(:dump_in) { "db/postgresql_structure.sql" } | ||
let(:temp_file) { Tempfile.create("postgresql_structure.sql") } | ||
let(:schema) { File.read(dump_in) } | ||
|
||
before do | ||
ActiveRecord::Tasks::DatabaseTasks.create(configuration) | ||
ActiveRecord::Tasks::DatabaseTasks.structure_load(configuration, dump_in) | ||
end | ||
|
||
after do | ||
ActiveRecord::Tasks::DatabaseTasks.drop(configuration) | ||
end | ||
|
||
it "dumps the schema onto the given path" do | ||
schema_matches = proc { temp_file.read == schema } | ||
|
||
expect { structure_dump }.to change(&schema_matches).from(false).to(true) | ||
end | ||
end | ||
|
||
describe "#purge" do | ||
subject(:purge) do | ||
ActiveRecord::Tasks::DatabaseTasks.purge(configuration) | ||
end | ||
|
||
before do | ||
ActiveRecord::Tasks::DatabaseTasks.create(configuration) | ||
ActiveRecord::Tasks::DatabaseTasks.structure_load(configuration, "db/postgresql_structure.sql") | ||
end | ||
|
||
after do | ||
ActiveRecord::Tasks::DatabaseTasks.drop(configuration) | ||
end | ||
|
||
it "recreates the database with an empty schema" do | ||
expect { purge }.to change(&schema_loaded?).from(true).to(false) | ||
end | ||
end | ||
end | ||
# rubocop:enable RSpec/MultipleMemoizedHelpers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
test: | ||
postgresql_primary: &postgresql_primary | ||
adapter: postgresql_proxy | ||
username: <%= ENV.fetch("PG_PRIMARY_USER", "postgres") %> | ||
password: <%= ENV.fetch("PG_PRIMARY_PASSWORD", "postgres") %> | ||
host: <%= ENV.fetch("PG_PRIMARY_HOST", "localhost") %> | ||
port: <%= Integer(ENV.fetch("PG_PRIMARY_PORT", 5432)) %> | ||
database: primary_replica_proxy_test | ||
|
||
postgresql_replica: | ||
adapter: postgresql | ||
username: <%= ENV.fetch("PG_REPLICA_USER", "postgres") %> | ||
password: <%= ENV.fetch("PG_REPLICA_PASSWORD", "postgres") %> | ||
host: <%= ENV.fetch("PG_REPLICA_HOST", "postgres_replica") %> | ||
port: <%= Integer(ENV.fetch("PG_REPLICA_PORT", 5433)) %> | ||
database: primary_replica_proxy_test | ||
replica: true | ||
|
||
postgresql_database_tasks: | ||
<<: *postgresql_primary | ||
database: primary_replica_proxy_database_tasks_test |
Oops, something went wrong.