diff --git a/docker-compose.yml b/docker-compose.yml index f22d956..1253e57 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,6 +6,7 @@ services: ports: - "127.0.0.1:5432:5432" environment: + - POSTGRES_USER=root - POSTGRES_HOST_AUTH_METHOD=trust volumes: - .data/postgresql-15/:/var/lib/postgresql diff --git a/fixtures/diesel/postgres/diesel-postgres-structure.sql b/fixtures/diesel/postgres/diesel-postgres-structure.sql new file mode 100644 index 0000000..e9930db --- /dev/null +++ b/fixtures/diesel/postgres/diesel-postgres-structure.sql @@ -0,0 +1,104 @@ +-- +-- PostgreSQL database dump +-- + +-- Dumped from database version 15.2 +-- Dumped by pg_dump version 15.4 (Homebrew) + +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; + +SET default_tablespace = ''; + +SET default_table_access_method = heap; + +-- +-- Name: __diesel_schema_migrations; Type: TABLE; Schema: public; Owner: - +-- + +CREATE TABLE public.__diesel_schema_migrations ( + version character varying(50) NOT NULL, + run_on timestamp without time zone DEFAULT CURRENT_TIMESTAMP NOT NULL +); + + +-- +-- Name: _sqlx_migrations; Type: TABLE; Schema: public; Owner: - +-- + +CREATE TABLE public._sqlx_migrations ( + version bigint NOT NULL, + description text NOT NULL, + installed_on timestamp with time zone DEFAULT now() NOT NULL, + success boolean NOT NULL, + checksum bytea NOT NULL, + execution_time bigint NOT NULL +); + + +-- +-- Name: diesel_users; Type: TABLE; Schema: public; Owner: - +-- + +CREATE TABLE public.diesel_users ( + id text NOT NULL, + email text NOT NULL, + created_at timestamp with time zone DEFAULT now() NOT NULL +); + + +-- +-- Name: sqlx_users; Type: TABLE; Schema: public; Owner: - +-- + +CREATE TABLE public.sqlx_users ( + id character varying(32) NOT NULL, + email text NOT NULL, + created_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP NOT NULL +); + + +-- +-- Name: __diesel_schema_migrations __diesel_schema_migrations_pkey; Type: CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.__diesel_schema_migrations + ADD CONSTRAINT __diesel_schema_migrations_pkey PRIMARY KEY (version); + + +-- +-- Name: _sqlx_migrations _sqlx_migrations_pkey; Type: CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public._sqlx_migrations + ADD CONSTRAINT _sqlx_migrations_pkey PRIMARY KEY (version); + + +-- +-- Name: diesel_users diesel_users_pkey; Type: CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.diesel_users + ADD CONSTRAINT diesel_users_pkey PRIMARY KEY (id); + + +-- +-- Name: sqlx_users sqlx_users_pkey; Type: CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.sqlx_users + ADD CONSTRAINT sqlx_users_pkey PRIMARY KEY (id); + + +-- +-- PostgreSQL database dump complete +-- + diff --git a/fixtures/diesel/postgres/migrations/2023-08-27-215620_add_users/down.sql b/fixtures/diesel/postgres/migrations/2023-08-27-215620_add_users/down.sql new file mode 100644 index 0000000..291a97c --- /dev/null +++ b/fixtures/diesel/postgres/migrations/2023-08-27-215620_add_users/down.sql @@ -0,0 +1 @@ +-- This file should undo anything in `up.sql` \ No newline at end of file diff --git a/fixtures/diesel/postgres/migrations/2023-08-27-215620_add_users/up.sql b/fixtures/diesel/postgres/migrations/2023-08-27-215620_add_users/up.sql new file mode 100644 index 0000000..ab93f65 --- /dev/null +++ b/fixtures/diesel/postgres/migrations/2023-08-27-215620_add_users/up.sql @@ -0,0 +1,5 @@ +CREATE TABLE diesel_users ( + id TEXT PRIMARY KEY NOT NULL, + email TEXT NOT NULL, + created_at TIMESTAMPTZ DEFAULT NOW() NOT NULL +); diff --git a/fixtures/sqlx/postgres/migrations/20230827160610_add_users.sql b/fixtures/sqlx/postgres/migrations/20230827160610_add_users.sql new file mode 100644 index 0000000..29f513e --- /dev/null +++ b/fixtures/sqlx/postgres/migrations/20230827160610_add_users.sql @@ -0,0 +1,5 @@ +CREATE TABLE sqlx_users ( + id VARCHAR(32) PRIMARY KEY NOT NULL, + email TEXT NOT NULL, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL +); diff --git a/fixtures/sqlx/postgres/sqlx-postgres-structure.sql b/fixtures/sqlx/postgres/sqlx-postgres-structure.sql new file mode 100644 index 0000000..e9930db --- /dev/null +++ b/fixtures/sqlx/postgres/sqlx-postgres-structure.sql @@ -0,0 +1,104 @@ +-- +-- PostgreSQL database dump +-- + +-- Dumped from database version 15.2 +-- Dumped by pg_dump version 15.4 (Homebrew) + +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; + +SET default_tablespace = ''; + +SET default_table_access_method = heap; + +-- +-- Name: __diesel_schema_migrations; Type: TABLE; Schema: public; Owner: - +-- + +CREATE TABLE public.__diesel_schema_migrations ( + version character varying(50) NOT NULL, + run_on timestamp without time zone DEFAULT CURRENT_TIMESTAMP NOT NULL +); + + +-- +-- Name: _sqlx_migrations; Type: TABLE; Schema: public; Owner: - +-- + +CREATE TABLE public._sqlx_migrations ( + version bigint NOT NULL, + description text NOT NULL, + installed_on timestamp with time zone DEFAULT now() NOT NULL, + success boolean NOT NULL, + checksum bytea NOT NULL, + execution_time bigint NOT NULL +); + + +-- +-- Name: diesel_users; Type: TABLE; Schema: public; Owner: - +-- + +CREATE TABLE public.diesel_users ( + id text NOT NULL, + email text NOT NULL, + created_at timestamp with time zone DEFAULT now() NOT NULL +); + + +-- +-- Name: sqlx_users; Type: TABLE; Schema: public; Owner: - +-- + +CREATE TABLE public.sqlx_users ( + id character varying(32) NOT NULL, + email text NOT NULL, + created_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP NOT NULL +); + + +-- +-- Name: __diesel_schema_migrations __diesel_schema_migrations_pkey; Type: CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.__diesel_schema_migrations + ADD CONSTRAINT __diesel_schema_migrations_pkey PRIMARY KEY (version); + + +-- +-- Name: _sqlx_migrations _sqlx_migrations_pkey; Type: CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public._sqlx_migrations + ADD CONSTRAINT _sqlx_migrations_pkey PRIMARY KEY (version); + + +-- +-- Name: diesel_users diesel_users_pkey; Type: CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.diesel_users + ADD CONSTRAINT diesel_users_pkey PRIMARY KEY (id); + + +-- +-- Name: sqlx_users sqlx_users_pkey; Type: CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.sqlx_users + ADD CONSTRAINT sqlx_users_pkey PRIMARY KEY (id); + + +-- +-- PostgreSQL database dump complete +-- + diff --git a/src/postgres/mod.rs b/src/postgres/mod.rs index 182f52a..f357d30 100644 --- a/src/postgres/mod.rs +++ b/src/postgres/mod.rs @@ -62,3 +62,44 @@ async fn migrate>( .map(|_| ()); Ok(()) } + +#[cfg(test)] +mod tests { + async fn call_write_structure_sql, D: AsRef>( + fixtures_path: M, + destination_filename: D, + ) -> Result<(), crate::error::Error> { + let destination_path = std::env::temp_dir().join(destination_filename.as_ref()); + let migrations_path = std::path::PathBuf::from(fixtures_path.as_ref()).join("migrations"); + let _ = super::write_structure_sql( + super::DEFAULT_CONNECTION_URL, + migrations_path, + &destination_path, + ) + .await?; + let expected = std::fs::read_to_string(dbg!(format!( + "./{}/{}", + fixtures_path.as_ref(), + destination_filename.as_ref() + )))?; + let contents = std::fs::read_to_string(destination_path)?; + assert_eq!(contents, expected); + Ok(()) + } + + #[cfg(all(feature = "sqlx", feature = "postgres"))] + #[tokio::test] + async fn test_write_structure_sql() -> Result<(), crate::error::Error> { + call_write_structure_sql("./fixtures/sqlx/postgres", "sqlx-postgres-structure.sql").await + } + + #[cfg(all(feature = "diesel", feature = "postgres"))] + #[tokio::test] + async fn test_write_structure_sql() -> Result<(), crate::error::Error> { + call_write_structure_sql( + "./fixtures/diesel/postgres", + "diesel-postgres-structure.sql", + ) + .await + } +} diff --git a/src/process.rs b/src/process.rs index 36962a6..a4d846b 100644 --- a/src/process.rs +++ b/src/process.rs @@ -42,12 +42,10 @@ mod tests { Ok(()) } - // IMPORTANT: The tests below this notice will fail if mysqldump is not found in the - // PATH. #[tokio::test] async fn test_run_found() -> Result<(), crate::Error> { use super::{run, Command}; - let result = run(Command::new("mysqldump").arg("--version")).await; + let result = run(Command::new("which").arg("which")).await; assert!(matches!(result, Ok(()))); Ok(()) } @@ -55,10 +53,10 @@ mod tests { #[tokio::test] async fn test_run_invalid_arguments() -> Result<(), crate::Error> { use super::{run, Command}; - let result = run(Command::new("mysqldump").arg("--norberto")).await; + let result = run(Command::new("which").arg("--norberto")).await; assert!(matches!( result, - Err(crate::Error::CommandRunError(error_message)) if error_message == "output: \nstderr: mysqldump: [ERROR] unknown option '--norberto'.\n")); + Err(crate::Error::CommandRunError(error_message)) if error_message.contains("stderr: which:"))); Ok(()) } }