From 5485b004544cfe2cb73807bae1d935b8fb471a58 Mon Sep 17 00:00:00 2001 From: jiacai2050 Date: Wed, 27 Sep 2023 15:54:43 +0800 Subject: [PATCH] add pg ci --- .github/workflows/ci.yml | 18 +++++++++ Makefile | 3 +- .../tests/{ => mysql}/local/input.result | 0 sqlness-cli/tests/{ => mysql}/local/input.sql | 0 .../tests/postgresql/local/input.result | 38 +++++++++++++++++++ sqlness-cli/tests/postgresql/local/input.sql | 22 +++++++++++ sqlness/src/database_impl/postgresql.rs | 9 +++-- 7 files changed, 85 insertions(+), 5 deletions(-) rename sqlness-cli/tests/{ => mysql}/local/input.result (100%) rename sqlness-cli/tests/{ => mysql}/local/input.sql (100%) create mode 100644 sqlness-cli/tests/postgresql/local/input.result create mode 100644 sqlness-cli/tests/postgresql/local/input.sql diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index bd8ada6..8d43500 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -78,6 +78,24 @@ jobs: sqlness-cli: runs-on: ubuntu-latest timeout-minutes: 60 + # Service containers to run with `container-job` + services: + # Label used to access the service container + postgres: + # Docker Hub image + image: postgres + # Provide the password for postgres + env: + POSTGRES_PASSWORD: postgres + # Set health checks to wait until postgres has started + options: >- + --health-cmd pg_isready + --health-interval 10s + --health-timeout 5s + --health-retries 5 + ports: + # Maps tcp port 5432 on service container to the host + - 5432:5432 strategy: matrix: rust: [stable] diff --git a/Makefile b/Makefile index cdb4e47..7afc0cd 100644 --- a/Makefile +++ b/Makefile @@ -22,7 +22,8 @@ clippy: cd $(DIR); cargo clippy --all-targets --all-features --workspace -- -D warnings cli-test: - cd $(DIR)/sqlness-cli; cargo run -- -c tests -i 127.0.0.1 -p 3306 -u root -P 1a2b3c -d public + cd $(DIR)/sqlness-cli; cargo run -- -c tests/mysql -i 127.0.0.1 -p 3306 -u root -P 1a2b3c -d public + cd $(DIR)/sqlness-cli; cargo run -- -c tests/postgresql -i 127.0.0.1 -p 5432 -u postgres -P postgres example: good-example bad-example diff --git a/sqlness-cli/tests/local/input.result b/sqlness-cli/tests/mysql/local/input.result similarity index 100% rename from sqlness-cli/tests/local/input.result rename to sqlness-cli/tests/mysql/local/input.result diff --git a/sqlness-cli/tests/local/input.sql b/sqlness-cli/tests/mysql/local/input.sql similarity index 100% rename from sqlness-cli/tests/local/input.sql rename to sqlness-cli/tests/mysql/local/input.sql diff --git a/sqlness-cli/tests/postgresql/local/input.result b/sqlness-cli/tests/postgresql/local/input.result new file mode 100644 index 00000000..d9aff4b --- /dev/null +++ b/sqlness-cli/tests/postgresql/local/input.result @@ -0,0 +1,38 @@ +DROP TABLE if exists categories; + +(Empty response) + +CREATE TABLE categories ( + category_id SERIAL NOT NULL PRIMARY KEY, + category_name VARCHAR(255), + description VARCHAR(255) +); + +(Empty response) + +INSERT INTO categories (category_name, description) +VALUES + ('Beverages', 'Soft drinks, coffees, teas, beers, and ales'), + ('Condiments', 'Sweet and savory sauces, relishes, spreads, and seasonings'), + ('Confections', 'Desserts, candies, and sweet breads'), + ('Dairy Products', 'Cheeses'), + ('Grains/Cereals', 'Breads, crackers, pasta, and cereal'), + ('Meat/Poultry', 'Prepared meats'), + ('Produce', 'Dried fruit and bean curd'), + ('Seafood', 'Seaweed and fish'); + +(Empty response) + +select * from categories; + +category_id,category_name,description, +Row { columns: [Column { name: "category_id", type: Int4 }, Column { name: "category_name", type: Varchar }, Column { name: "description", type: Varchar }] } +Row { columns: [Column { name: "category_id", type: Int4 }, Column { name: "category_name", type: Varchar }, Column { name: "description", type: Varchar }] } +Row { columns: [Column { name: "category_id", type: Int4 }, Column { name: "category_name", type: Varchar }, Column { name: "description", type: Varchar }] } +Row { columns: [Column { name: "category_id", type: Int4 }, Column { name: "category_name", type: Varchar }, Column { name: "description", type: Varchar }] } +Row { columns: [Column { name: "category_id", type: Int4 }, Column { name: "category_name", type: Varchar }, Column { name: "description", type: Varchar }] } +Row { columns: [Column { name: "category_id", type: Int4 }, Column { name: "category_name", type: Varchar }, Column { name: "description", type: Varchar }] } +Row { columns: [Column { name: "category_id", type: Int4 }, Column { name: "category_name", type: Varchar }, Column { name: "description", type: Varchar }] } +Row { columns: [Column { name: "category_id", type: Int4 }, Column { name: "category_name", type: Varchar }, Column { name: "description", type: Varchar }] } + + diff --git a/sqlness-cli/tests/postgresql/local/input.sql b/sqlness-cli/tests/postgresql/local/input.sql new file mode 100644 index 00000000..95d49fb --- /dev/null +++ b/sqlness-cli/tests/postgresql/local/input.sql @@ -0,0 +1,22 @@ + +DROP TABLE if exists categories; + +CREATE TABLE categories ( + category_id SERIAL NOT NULL PRIMARY KEY, + category_name VARCHAR(255), + description VARCHAR(255) +); + +INSERT INTO categories (category_name, description) +VALUES + ('Beverages', 'Soft drinks, coffees, teas, beers, and ales'), + ('Condiments', 'Sweet and savory sauces, relishes, spreads, and seasonings'), + ('Confections', 'Desserts, candies, and sweet breads'), + ('Dairy Products', 'Cheeses'), + ('Grains/Cereals', 'Breads, crackers, pasta, and cereal'), + ('Meat/Poultry', 'Prepared meats'), + ('Produce', 'Dried fruit and bean curd'), + ('Seafood', 'Seaweed and fish'); + + +select * from categories; diff --git a/sqlness/src/database_impl/postgresql.rs b/sqlness/src/database_impl/postgresql.rs index 1c53691..acbb769 100644 --- a/sqlness/src/database_impl/postgresql.rs +++ b/sqlness/src/database_impl/postgresql.rs @@ -61,6 +61,10 @@ struct PostgresqlFormatter { impl Display for PostgresqlFormatter { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + if self.rows.is_empty() { + return f.write_fmt(format_args!("(Empty response)")); + } + let top = &self.rows[0]; let columns = top .columns() @@ -74,10 +78,7 @@ impl Display for PostgresqlFormatter { f.write_str("\n")?; for row in &self.rows { - for column in &columns { - f.write_fmt(format_args!("{:?},", row.get::<&str, &str>(column)))?; - } - f.write_str("\n")?; + f.write_fmt(format_args!("{:?}\n", row))?; } Ok(())