Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PRIMARY_KEYS_WITH_SERIAL_TYPES] ERROR: permission denied for schema XXX #537

Merged
merged 7 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix tests
  • Loading branch information
mfvanek committed Dec 9, 2024
commit 3af313cfe0466c9d129c4e670eb9b63e409f95e5
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ public static PgContext of(@Nonnull final String schemaName) {
*
* @return {@code PgContext}
* @see PgContext#DEFAULT_BLOAT_PERCENTAGE_THRESHOLD
* @see PgContext#DEFAULT_REMAINING_PERCENTAGE_THRESHOLD
*/
@Nonnull
public static PgContext ofPublic() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ internal class PostgresCustomDataSourceDemoApplicationKtRunTest {
assertThatCode { main(arrayOf("--spring.profiles.active=test")) }
.doesNotThrowAnyException()
assertThat(output.all)
.contains("Reading from custom_ds_schema.databasechangelog")
.contains("Starting PostgresCustomDataSourceDemoApplicationKt using Java")
.contains("Container is started (JDBC URL: jdbc:postgresql://localhost:")
.contains("Started PostgresCustomDataSourceDemoApplicationKt in")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ create schema if not exists additional_schema authorization pg_database_owner;

create user custom_user with nosuperuser nocreatedb nocreaterole password 'customUserPassword' connection limit 10;
grant usage on schema main_schema to custom_user;
alter user custom_user set search_path to main_schema;

create table if not exists additional_schema.additional_table (
id bigserial primary key,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) 2019-2024. Ivan Vakhrushev and others.
* https://github.com/mfvanek/pg-index-health
*
* This file is a part of "pg-index-health" - a Java library for
* analyzing and maintaining indexes health in PostgreSQL databases.
*
* Licensed under the Apache License 2.0
*/

package io.github.mfvanek.pg.spring.postgres.with.custom.user;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.system.CapturedOutput;
import org.springframework.boot.test.system.OutputCaptureExtension;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;

@ExtendWith(OutputCaptureExtension.class)
class PostgresWithCustomUserDemoApplicationRunTest {

@Test
void applicationShouldRun(final CapturedOutput output) {
assertThatCode(() -> PostgresWithCustomUserDemoApplication.main(new String[]{}))
.doesNotThrowAnyException();
assertThat(output.getAll())
.contains("Reading from main_schema.databasechangelog")
.contains("Starting PostgresWithCustomUserDemoApplication using Java")
.contains("Container is started (JDBC URL: jdbc:postgresql://localhost:")
.contains("Started PostgresWithCustomUserDemoApplication in");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,31 +84,31 @@ void checksShouldWorkForPublicSchema() {
}

@Test
void checksShouldWorkForDefaultSchema() {
void checksShouldWorkForMainSchema() {
assertThat(checks)
.hasSameSizeAs(Diagnostic.values());

final PgContext ctx = PgContext.of("main_schema");
checks.stream()
.filter(DatabaseCheckOnHost::isStatic)
.forEach(c -> {
final ListAssert<? extends DbObject> listAssert = assertThat(c.check(ctx, SkipLiquibaseTablesPredicate.ofPublic()))
final ListAssert<? extends DbObject> listAssert = assertThat(c.check(ctx, SkipLiquibaseTablesPredicate.of(ctx)))
.as(c.getDiagnostic().name());

switch (c.getDiagnostic()) {
case TABLES_NOT_LINKED_TO_OTHERS:
listAssert
.hasSize(1)
.asInstanceOf(list(Table.class))
.containsExactly(Table.of("warehouse"));
.containsExactly(Table.of(ctx, "warehouse"));
break;

case PRIMARY_KEYS_WITH_SERIAL_TYPES:
listAssert
.hasSize(1)
.asInstanceOf(list(ColumnWithSerialType.class))
.containsExactly(ColumnWithSerialType.of(
Column.ofNotNull("warehouse", "id"),
Column.ofNotNull(ctx, "warehouse", "id"),
SerialType.BIG_SERIAL, ctx.enrichWithSchema("warehouse_id_seq")));
break;

Expand All @@ -128,9 +128,10 @@ void checksShouldWorkForAdditionalSchema() {
checks.stream()
.filter(DatabaseCheckOnHost::isStatic)
.forEach(c -> {
final ListAssert<? extends DbObject> listAssert = assertThat(c.check(ctx, SkipLiquibaseTablesPredicate.ofPublic()))
final ListAssert<? extends DbObject> listAssert = assertThat(c.check(ctx))
.as(c.getDiagnostic().name());

// PRIMARY_KEYS_WITH_SERIAL_TYPES are present in the schema but cannot be found due to insufficient permissions
switch (c.getDiagnostic()) {
case TABLES_WITHOUT_DESCRIPTION:
case TABLES_NOT_LINKED_TO_OTHERS:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2019-2024. Ivan Vakhrushev and others.
* https://github.com/mfvanek/pg-index-health
*
* This file is a part of "pg-index-health" - a Java library for
* analyzing and maintaining indexes health in PostgreSQL databases.
*
* Licensed under the Apache License 2.0
*/

package io.github.mfvanek.pg.spring.postgres.with.custom.user.config;

import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.core.env.Environment;
import org.springframework.mock.env.MockEnvironment;
import org.testcontainers.containers.JdbcDatabaseContainer;

import static io.github.mfvanek.pg.spring.postgres.with.custom.user.config.ConfigurableEnvironmentMutator.DATASOURCE_URL_PROP_NAME;
import static org.assertj.core.api.Assertions.assertThat;

class ConfigurableEnvironmentMutatorTest {

private final JdbcDatabaseContainer<?> jdbcDatabaseContainer = Mockito.mock(JdbcDatabaseContainer.class);

@Test
void shouldNotAddPropIfExist() {
final MockEnvironment environment = new MockEnvironment();
environment.setProperty(DATASOURCE_URL_PROP_NAME, "url");

assertThat(ConfigurableEnvironmentMutator.addDatasourceUrlIfNeed(jdbcDatabaseContainer, environment))
.isFalse();
assertThat(environment.getProperty(DATASOURCE_URL_PROP_NAME)).isEqualTo("url");
assertThat(environment.getProperty("spring.liquibase.url")).isNull();
}

@Test
void shouldNotAddPropIfInvalidType() {
final Environment environment = Mockito.mock(Environment.class);
Mockito.when(environment.getProperty(Mockito.anyString())).thenReturn(null);

assertThat(ConfigurableEnvironmentMutator.addDatasourceUrlIfNeed(jdbcDatabaseContainer, environment))
.isFalse();
}

@Test
void shouldAddProperty() {
final MockEnvironment environment = new MockEnvironment();
Mockito.when(jdbcDatabaseContainer.getJdbcUrl()).thenReturn("added_url");

assertThat(ConfigurableEnvironmentMutator.addDatasourceUrlIfNeed(jdbcDatabaseContainer, environment))
.isTrue();
assertThat(environment.getProperty(DATASOURCE_URL_PROP_NAME)).isEqualTo("added_url");
assertThat(environment.getProperty("spring.liquibase.url")).isEqualTo("added_url");
}
}
Loading