diff --git a/.metabase.db/metabase.db.mv.db b/.metabase.db/metabase.db.mv.db index 29f7e2a..8aac6ef 100644 Binary files a/.metabase.db/metabase.db.mv.db and b/.metabase.db/metabase.db.mv.db differ diff --git a/lib/metabase/endpoint/database.rb b/lib/metabase/endpoint/database.rb index a8fb281..21841e8 100644 --- a/lib/metabase/endpoint/database.rb +++ b/lib/metabase/endpoint/database.rb @@ -11,6 +11,34 @@ module Database def databases(**params) get('/api/database', **params) end + + # Fetch a database. + # + # @param database_id [Integer, String] Database ID + # @param params [Hash] Query string + # @return [Hash] Parsed response JSON + def database(database_id, **params) + get("/api/database/#{database_id}", **params) + end + + # Fetch schemas of a database. + # + # @param database_id [Integer, String] Database ID + # @param params [Hash] Query string + # @return [Array] Parsed response JSON + def schemas(database_id) + get("/api/database/#{database_id}/schemas") + end + + # Fetch tables of a schema of a database. + # + # @param database_id [Integer, String] Database ID + # @param schema [String] Schema name + # @param params [Hash] Query string + # @return [Array] Parsed response JSON + def schema(database_id, schema) + get("/api/database/#{database_id}/schema/#{schema}") + end end end end diff --git a/spec/metabase/endpoint/database_spec.rb b/spec/metabase/endpoint/database_spec.rb index d338220..3b61cea 100644 --- a/spec/metabase/endpoint/database_spec.rb +++ b/spec/metabase/endpoint/database_spec.rb @@ -11,4 +11,31 @@ end end end + + describe 'database', vcr: true do + context 'success' do + it 'returns a database' do + database = client.database(1) + expect(database).to be_kind_of(Hash) + end + end + end + + describe 'schemas', vcr: true do + context 'success' do + it 'returns all schemas of a database' do + schemas = client.schemas(1) + expect(schemas).to be_kind_of(Array) + end + end + end + + describe 'schema', vcr: true do + context 'success' do + it 'returns all tables of a schema of a database' do + tables = client.schema(1, "PUBLIC") + expect(tables).to be_kind_of(Array) + end + end + end end diff --git a/spec/vcr_cassettes/Metabase_Endpoint_Database/database/success/returns_a_database.yml b/spec/vcr_cassettes/Metabase_Endpoint_Database/database/success/returns_a_database.yml new file mode 100644 index 0000000..af32125 --- /dev/null +++ b/spec/vcr_cassettes/Metabase_Endpoint_Database/database/success/returns_a_database.yml @@ -0,0 +1,118 @@ +--- +http_interactions: +- request: + method: post + uri: http://localhost:3030/api/session + body: + encoding: UTF-8 + string: '{"username":"mb@example.com","password":"p@ssw0rd"}' + headers: + User-Agent: + - MetabaseRuby/0.5.0 (ruby2.7.3) + Content-Type: + - application/json + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + response: + status: + code: 200 + message: OK + headers: + Date: + - Wed, 17 May 2023 12:31:00 GMT + X-Frame-Options: + - DENY + X-Xss-Protection: + - 1; mode=block + Last-Modified: + - Wed, 17 May 2023 12:31:00 GMT + Strict-Transport-Security: + - max-age=31536000 + Set-Cookie: + - metabase.SESSION=6603469c-6bfb-409f-9cb0-10aec630bd9a;SameSite=Lax;HttpOnly;Path=/;Max-Age=1209600 + - metabase.SESSION_ID=;Expires=Thu, 1 Jan 1970 00:00:00 GMT;Path=/ + X-Permitted-Cross-Domain-Policies: + - none + Cache-Control: + - max-age=0, no-cache, must-revalidate, proxy-revalidate + X-Content-Type-Options: + - nosniff + Content-Security-Policy: + - default-src 'none'; script-src 'self' 'unsafe-eval' https://maps.google.com + https://apis.google.com https://www.google-analytics.com https://*.googleapis.com + *.gstatic.com 'sha256-lMAh4yjVuDkQ9NqkK4H+YHUga+anpFs5JAuj/uZh0Rs=' 'sha256-sMNbXyc1lLzhHbH/CKs11HIQMnMkZAN2eA99WhJeEC0=' + 'sha256-JJa56hyDfUbgNfq+0nq6Qs866JKgZ/+qCq2pkDJED8k='; child-src 'self' https://accounts.google.com; + style-src 'self' 'unsafe-inline'; font-src 'self' ; img-src * 'self' data:; + connect-src 'self' metabase.us10.list-manage.com ; manifest-src 'self'; + Content-Type: + - application/json;charset=utf-8 + Expires: + - Tue, 03 Jul 2001 06:00:00 GMT + Transfer-Encoding: + - chunked + Server: + - Jetty(9.4.27.v20200227) + body: + encoding: ASCII-8BIT + string: '{"id":"6603469c-6bfb-409f-9cb0-10aec630bd9a"}' + recorded_at: Wed, 17 May 2023 12:31:00 GMT +- request: + method: get + uri: http://localhost:3030/api/database/1 + body: + encoding: US-ASCII + string: '' + headers: + User-Agent: + - MetabaseRuby/0.5.0 (ruby2.7.3) + X-Metabase-Session: + - 6603469c-6bfb-409f-9cb0-10aec630bd9a + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + response: + status: + code: 200 + message: OK + headers: + Date: + - Wed, 17 May 2023 12:31:00 GMT + X-Frame-Options: + - DENY + X-Xss-Protection: + - 1; mode=block + Last-Modified: + - Wed, 17 May 2023 12:31:00 GMT + Strict-Transport-Security: + - max-age=31536000 + X-Permitted-Cross-Domain-Policies: + - none + Cache-Control: + - max-age=0, no-cache, must-revalidate, proxy-revalidate + X-Content-Type-Options: + - nosniff + Content-Security-Policy: + - default-src 'none'; script-src 'self' 'unsafe-eval' https://maps.google.com + https://apis.google.com https://www.google-analytics.com https://*.googleapis.com + *.gstatic.com 'sha256-lMAh4yjVuDkQ9NqkK4H+YHUga+anpFs5JAuj/uZh0Rs=' 'sha256-sMNbXyc1lLzhHbH/CKs11HIQMnMkZAN2eA99WhJeEC0=' + 'sha256-JJa56hyDfUbgNfq+0nq6Qs866JKgZ/+qCq2pkDJED8k='; child-src 'self' https://accounts.google.com; + style-src 'self' 'unsafe-inline'; font-src 'self' ; img-src * 'self' data:; + connect-src 'self' metabase.us10.list-manage.com ; manifest-src 'self'; + Content-Type: + - application/json;charset=utf-8 + Expires: + - Tue, 03 Jul 2001 06:00:00 GMT + Transfer-Encoding: + - chunked + Server: + - Jetty(9.4.27.v20200227) + body: + encoding: ASCII-8BIT + string: '{"description":null,"features":["basic-aggregations","standard-deviation-aggregations","expression-aggregations","foreign-keys","right-join","left-join","native-parameters","nested-queries","expressions","case-sensitivity-string-filter-options","binning","inner-join","advanced-math-expressions"],"cache_field_values_schedule":"0 + 50 0 * * ? *","timezone":"UTC","auto_run_queries":true,"metadata_sync_schedule":"0 + 50 * * * ? *","name":"Sample Dataset","caveats":null,"is_full_sync":true,"updated_at":"2023-05-17T12:28:01.088","details":{"db":"zip:/app/metabase.jar!/sample-dataset.db;USER=GUEST;PASSWORD=guest"},"is_sample":true,"id":1,"is_on_demand":false,"options":null,"schedules":{"cache_field_values":{"schedule_day":null,"schedule_frame":null,"schedule_hour":0,"schedule_type":"daily"},"metadata_sync":{"schedule_day":null,"schedule_frame":null,"schedule_hour":null,"schedule_type":"hourly"}},"engine":"h2","created_at":"2020-06-07T05:19:27.175","points_of_interest":null}' + recorded_at: Wed, 17 May 2023 12:31:00 GMT +recorded_with: VCR 6.0.0 diff --git a/spec/vcr_cassettes/Metabase_Endpoint_Database/schema/success/returns_all_tables_of_a_schema_of_a_database.yml b/spec/vcr_cassettes/Metabase_Endpoint_Database/schema/success/returns_all_tables_of_a_schema_of_a_database.yml new file mode 100644 index 0000000..340f7c8 --- /dev/null +++ b/spec/vcr_cassettes/Metabase_Endpoint_Database/schema/success/returns_all_tables_of_a_schema_of_a_database.yml @@ -0,0 +1,122 @@ +--- +http_interactions: +- request: + method: post + uri: http://localhost:3030/api/session + body: + encoding: UTF-8 + string: '{"username":"mb@example.com","password":"p@ssw0rd"}' + headers: + User-Agent: + - MetabaseRuby/0.5.0 (ruby2.7.3) + Content-Type: + - application/json + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + response: + status: + code: 200 + message: OK + headers: + Date: + - Wed, 17 May 2023 12:33:01 GMT + X-Frame-Options: + - DENY + X-Xss-Protection: + - 1; mode=block + Last-Modified: + - Wed, 17 May 2023 12:33:01 GMT + Strict-Transport-Security: + - max-age=31536000 + Set-Cookie: + - metabase.SESSION=1d6c14e1-7f5d-43f3-b38b-aa0c1c0b7c5d;SameSite=Lax;HttpOnly;Path=/;Max-Age=1209600 + - metabase.SESSION_ID=;Expires=Thu, 1 Jan 1970 00:00:00 GMT;Path=/ + X-Permitted-Cross-Domain-Policies: + - none + Cache-Control: + - max-age=0, no-cache, must-revalidate, proxy-revalidate + X-Content-Type-Options: + - nosniff + Content-Security-Policy: + - default-src 'none'; script-src 'self' 'unsafe-eval' https://maps.google.com + https://apis.google.com https://www.google-analytics.com https://*.googleapis.com + *.gstatic.com 'sha256-lMAh4yjVuDkQ9NqkK4H+YHUga+anpFs5JAuj/uZh0Rs=' 'sha256-sMNbXyc1lLzhHbH/CKs11HIQMnMkZAN2eA99WhJeEC0=' + 'sha256-JJa56hyDfUbgNfq+0nq6Qs866JKgZ/+qCq2pkDJED8k='; child-src 'self' https://accounts.google.com; + style-src 'self' 'unsafe-inline'; font-src 'self' ; img-src * 'self' data:; + connect-src 'self' metabase.us10.list-manage.com ; manifest-src 'self'; + Content-Type: + - application/json;charset=utf-8 + Expires: + - Tue, 03 Jul 2001 06:00:00 GMT + Transfer-Encoding: + - chunked + Server: + - Jetty(9.4.27.v20200227) + body: + encoding: ASCII-8BIT + string: '{"id":"1d6c14e1-7f5d-43f3-b38b-aa0c1c0b7c5d"}' + recorded_at: Wed, 17 May 2023 12:33:01 GMT +- request: + method: get + uri: http://localhost:3030/api/database/1/schema/PUBLIC + body: + encoding: US-ASCII + string: '' + headers: + User-Agent: + - MetabaseRuby/0.5.0 (ruby2.7.3) + X-Metabase-Session: + - 1d6c14e1-7f5d-43f3-b38b-aa0c1c0b7c5d + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + response: + status: + code: 200 + message: OK + headers: + Date: + - Wed, 17 May 2023 12:33:01 GMT + X-Frame-Options: + - DENY + X-Xss-Protection: + - 1; mode=block + Last-Modified: + - Wed, 17 May 2023 12:33:01 GMT + Strict-Transport-Security: + - max-age=31536000 + X-Permitted-Cross-Domain-Policies: + - none + Cache-Control: + - max-age=0, no-cache, must-revalidate, proxy-revalidate + X-Content-Type-Options: + - nosniff + Content-Security-Policy: + - default-src 'none'; script-src 'self' 'unsafe-eval' https://maps.google.com + https://apis.google.com https://www.google-analytics.com https://*.googleapis.com + *.gstatic.com 'sha256-lMAh4yjVuDkQ9NqkK4H+YHUga+anpFs5JAuj/uZh0Rs=' 'sha256-sMNbXyc1lLzhHbH/CKs11HIQMnMkZAN2eA99WhJeEC0=' + 'sha256-JJa56hyDfUbgNfq+0nq6Qs866JKgZ/+qCq2pkDJED8k='; child-src 'self' https://accounts.google.com; + style-src 'self' 'unsafe-inline'; font-src 'self' ; img-src * 'self' data:; + connect-src 'self' metabase.us10.list-manage.com ; manifest-src 'self'; + Content-Type: + - application/json;charset=utf-8 + Expires: + - Tue, 03 Jul 2001 06:00:00 GMT + Transfer-Encoding: + - chunked + Server: + - Jetty(9.4.27.v20200227) + body: + encoding: ASCII-8BIT + string: '[{"description":"This is a confirmed order for a product from a user.","entity_type":"entity/TransactionTable","schema":"PUBLIC","show_in_getting_started":false,"name":"ORDERS","caveats":null,"fields_hash":"Iqz4vNbm7vh80Uo9pWdesA==","rows":null,"updated_at":"2020-06-07T05:50:00.143","entity_name":null,"active":true,"id":2,"db_id":1,"visibility_type":null,"display_name":"Orders","created_at":"2020-06-07T05:19:27.483","points_of_interest":null},{"description":"This + is a user account. Note that employees and customer support staff will have + accounts.","entity_type":"entity/UserTable","schema":"PUBLIC","show_in_getting_started":false,"name":"PEOPLE","caveats":null,"fields_hash":"CXKI5VefRbNYgZ8IStmaNw==","rows":null,"updated_at":"2020-06-07T05:50:00.152","entity_name":null,"active":true,"id":3,"db_id":1,"visibility_type":null,"display_name":"People","created_at":"2020-06-07T05:19:27.486","points_of_interest":null},{"description":"This + is our product catalog. It includes all products ever sold by the Sample Company.","entity_type":"entity/ProductTable","schema":"PUBLIC","show_in_getting_started":false,"name":"PRODUCTS","caveats":null,"fields_hash":"aqXlpsb4FjyCH5o8qP4a2A==","rows":null,"updated_at":"2020-06-07T05:50:00.163","entity_name":null,"active":true,"id":1,"db_id":1,"visibility_type":null,"display_name":"Products","created_at":"2020-06-07T05:19:27.478","points_of_interest":null},{"description":"These + are reviews our customers have left on products. Note that these are not tied + to orders so it is possible people have reviewed products they did not purchase + from us.","entity_type":"entity/GenericTable","schema":"PUBLIC","show_in_getting_started":false,"name":"REVIEWS","caveats":null,"fields_hash":"wIcr7cLnXrbpAUfOXgcmeQ==","rows":null,"updated_at":"2020-06-07T05:50:00.168","entity_name":null,"active":true,"id":4,"db_id":1,"visibility_type":null,"display_name":"Reviews","created_at":"2020-06-07T05:19:27.49","points_of_interest":null}]' + recorded_at: Wed, 17 May 2023 12:33:01 GMT +recorded_with: VCR 6.0.0 diff --git a/spec/vcr_cassettes/Metabase_Endpoint_Database/schemas/success/returns_all_schemas_of_a_database.yml b/spec/vcr_cassettes/Metabase_Endpoint_Database/schemas/success/returns_all_schemas_of_a_database.yml new file mode 100644 index 0000000..630b86f --- /dev/null +++ b/spec/vcr_cassettes/Metabase_Endpoint_Database/schemas/success/returns_all_schemas_of_a_database.yml @@ -0,0 +1,116 @@ +--- +http_interactions: +- request: + method: post + uri: http://localhost:3030/api/session + body: + encoding: UTF-8 + string: '{"username":"mb@example.com","password":"p@ssw0rd"}' + headers: + User-Agent: + - MetabaseRuby/0.5.0 (ruby2.7.3) + Content-Type: + - application/json + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + response: + status: + code: 200 + message: OK + headers: + Date: + - Wed, 17 May 2023 12:31:00 GMT + X-Frame-Options: + - DENY + X-Xss-Protection: + - 1; mode=block + Last-Modified: + - Wed, 17 May 2023 12:31:00 GMT + Strict-Transport-Security: + - max-age=31536000 + Set-Cookie: + - metabase.SESSION=769a5861-114f-42b6-9ab3-06e6e73e11c8;SameSite=Lax;HttpOnly;Path=/;Max-Age=1209600 + - metabase.SESSION_ID=;Expires=Thu, 1 Jan 1970 00:00:00 GMT;Path=/ + X-Permitted-Cross-Domain-Policies: + - none + Cache-Control: + - max-age=0, no-cache, must-revalidate, proxy-revalidate + X-Content-Type-Options: + - nosniff + Content-Security-Policy: + - default-src 'none'; script-src 'self' 'unsafe-eval' https://maps.google.com + https://apis.google.com https://www.google-analytics.com https://*.googleapis.com + *.gstatic.com 'sha256-lMAh4yjVuDkQ9NqkK4H+YHUga+anpFs5JAuj/uZh0Rs=' 'sha256-sMNbXyc1lLzhHbH/CKs11HIQMnMkZAN2eA99WhJeEC0=' + 'sha256-JJa56hyDfUbgNfq+0nq6Qs866JKgZ/+qCq2pkDJED8k='; child-src 'self' https://accounts.google.com; + style-src 'self' 'unsafe-inline'; font-src 'self' ; img-src * 'self' data:; + connect-src 'self' metabase.us10.list-manage.com ; manifest-src 'self'; + Content-Type: + - application/json;charset=utf-8 + Expires: + - Tue, 03 Jul 2001 06:00:00 GMT + Transfer-Encoding: + - chunked + Server: + - Jetty(9.4.27.v20200227) + body: + encoding: ASCII-8BIT + string: '{"id":"769a5861-114f-42b6-9ab3-06e6e73e11c8"}' + recorded_at: Wed, 17 May 2023 12:31:00 GMT +- request: + method: get + uri: http://localhost:3030/api/database/1/schemas + body: + encoding: US-ASCII + string: '' + headers: + User-Agent: + - MetabaseRuby/0.5.0 (ruby2.7.3) + X-Metabase-Session: + - 769a5861-114f-42b6-9ab3-06e6e73e11c8 + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + response: + status: + code: 200 + message: OK + headers: + Date: + - Wed, 17 May 2023 12:31:00 GMT + X-Frame-Options: + - DENY + X-Xss-Protection: + - 1; mode=block + Last-Modified: + - Wed, 17 May 2023 12:31:00 GMT + Strict-Transport-Security: + - max-age=31536000 + X-Permitted-Cross-Domain-Policies: + - none + Cache-Control: + - max-age=0, no-cache, must-revalidate, proxy-revalidate + X-Content-Type-Options: + - nosniff + Content-Security-Policy: + - default-src 'none'; script-src 'self' 'unsafe-eval' https://maps.google.com + https://apis.google.com https://www.google-analytics.com https://*.googleapis.com + *.gstatic.com 'sha256-lMAh4yjVuDkQ9NqkK4H+YHUga+anpFs5JAuj/uZh0Rs=' 'sha256-sMNbXyc1lLzhHbH/CKs11HIQMnMkZAN2eA99WhJeEC0=' + 'sha256-JJa56hyDfUbgNfq+0nq6Qs866JKgZ/+qCq2pkDJED8k='; child-src 'self' https://accounts.google.com; + style-src 'self' 'unsafe-inline'; font-src 'self' ; img-src * 'self' data:; + connect-src 'self' metabase.us10.list-manage.com ; manifest-src 'self'; + Content-Type: + - application/json;charset=utf-8 + Expires: + - Tue, 03 Jul 2001 06:00:00 GMT + Transfer-Encoding: + - chunked + Server: + - Jetty(9.4.27.v20200227) + body: + encoding: ASCII-8BIT + string: '["PUBLIC"]' + recorded_at: Wed, 17 May 2023 12:31:00 GMT +recorded_with: VCR 6.0.0