Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.dinky.configure;

import org.springframework.boot.context.properties.ConfigurationProperties;

import lombok.Data;

/**
* Metadata properties for Dinky, used to configure metadata database connection behaviors
* such as custom PostgreSQL schema support.
*/
@Data
@ConfigurationProperties(prefix = "dinky.metadata")
public class DinkyMetadataProperties {

/**
* The schema name for PostgreSQL metadata database.
* Defaults to {@code public} to maintain backward compatibility.
* Override with {@code dinky.metadata.schema} or environment variable {@code DINKY_METADATA_SCHEMA}.
*/
private String schema = "public";
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.dinky.configure;

import org.dinky.context.TenantContextHolder;
import org.dinky.interceptor.DinkySchemaInnerInterceptor;
import org.dinky.interceptor.PostgreSQLPrepareInterceptor;
import org.dinky.interceptor.PostgreSQLQueryInterceptor;
import org.dinky.mybatis.handler.DateMetaObjectHandler;
Expand All @@ -28,6 +29,7 @@
import java.util.Set;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
Expand All @@ -51,12 +53,13 @@
/** mybatisPlus config class */
@Configuration
@MapperScan("org.dinky.mapper")
@EnableConfigurationProperties(MybatisPlusFillProperties.class)
@EnableConfigurationProperties({MybatisPlusFillProperties.class, DinkyMetadataProperties.class})
@Slf4j
@RequiredArgsConstructor
public class MybatisPlusConfig {

private final MybatisPlusFillProperties autoFillProperties;
private final DinkyMetadataProperties metadataProperties;

private static final Set<String> IGNORE_TABLE_NAMES = ImmutableSet.of(
"dinky_namespace",
Expand Down Expand Up @@ -97,8 +100,22 @@ public PostgreSQLPrepareInterceptor postgreSQLPrepareInterceptor() {
return new PostgreSQLPrepareInterceptor();
}

/**
* MyBatis-Plus interceptor that dynamically prepends a custom PostgreSQL schema
* prefix to all Dinky business table names. Only active when the
* {@code postgresql} profile is enabled and a non-{@code public} schema is configured.
*
* @return the schema interceptor instance (can be a no-op handler map)
*/
@Bean
@Profile("postgresql")
public DinkySchemaInnerInterceptor dinkySchemaInnerInterceptor() {
return new DinkySchemaInnerInterceptor(metadataProperties.getSchema());
}

@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
public MybatisPlusInterceptor mybatisPlusInterceptor(
ObjectProvider<DinkySchemaInnerInterceptor> schemaInterceptorProvider) {
log.info("mybatis plus interceptor execute");
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new TenantLineInnerInterceptor(new TenantLineHandler() {
Expand All @@ -120,6 +137,9 @@ public boolean ignoreTable(String tableName) {
return !IGNORE_TABLE_NAMES.contains(tableName);
}
}));
// Add schema prefix interceptor after tenant interceptor so that ignoreTable
// still matches the original (un-prefixed) table names
schemaInterceptorProvider.ifAvailable(interceptor::addInnerInterceptor);
interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return interceptor;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.dinky.interceptor;

import com.baomidou.mybatisplus.extension.plugins.handler.TableNameHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.DynamicTableNameInnerInterceptor;

/**
* MyBatis-Plus interceptor that dynamically prepends a custom PostgreSQL schema prefix to
* all Dinky business table names (those with the {@code dinky_} prefix).
*
* <p>When the configured schema is {@code public} or empty, this interceptor is a no-op,
* preserving the original behavior of querying tables in the default schema.</p>
*
* <p>Using a prefix-based match (dinky_*) instead of an explicit table-name set means
* any future Dinky table will automatically receive the schema prefix without code changes.</p>
*
* <p><b>Interceptor ordering note:</b> Must be registered <em>after</em>
* {@link com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor}
* and <em>before</em> {@link com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor}
* so that tenant's {@code ignoreTable} still matches the original (un-prefixed) table names.</p>
*/
public class DinkySchemaInnerInterceptor extends DynamicTableNameInnerInterceptor {

/** Only table names starting with this prefix receive the schema prefix. */
private static final String DINKY_TABLE_PREFIX = "dinky_";

public DinkySchemaInnerInterceptor(String schema) {
// schema is empty, null, or "public" → no-op, keep original table names
if (schema == null || schema.isEmpty() || "public".equalsIgnoreCase(schema)) {
return;
}

final String prefix = schema + ".";
TableNameHandler handler = (sql, tableName) -> {
if (tableName != null && tableName.startsWith(DINKY_TABLE_PREFIX)) {
return prefix + tableName;
}
return tableName;
};
setTableNameHandler(handler);
}
}
15 changes: 14 additions & 1 deletion dinky-admin/src/main/resources/application-postgresql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,17 @@ spring:
password: ${POSTGRES_PASSWORD:dinky}
driver-class-name: org.postgresql.Driver
# The POSTGRES_ADDR variable is not officially provided. If you use docker, please manually add an env to support it.
url: jdbc:postgresql://${POSTGRES_ADDR:localhost:5432}/${POSTGRES_DB:dinky}?stringtype=unspecified
url: jdbc:postgresql://${POSTGRES_ADDR:localhost:5432}/${POSTGRES_DB:dinky}?stringtype=unspecified&currentSchema=${dinky.metadata.schema:public}
flyway:
default-schema: ${dinky.metadata.schema:public}
schemas: ${dinky.metadata.schema:public}
create-schemas: true
placeholder-replacement: true
placeholder-prefix: '${'
placeholder-suffix: '}'
placeholders:
dinky_schema: ${dinky.metadata.schema:public}

dinky:
metadata:
schema: ${DINKY_METADATA_SCHEMA:public}
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
-- note: Rolling back SQL statements is only necessary to perform a rollback operation in the event of an automatic upgrade failure. The following SQL statements need to be manually executed
update public.dinky_sys_menu set "path"='/settings/alertrule',
update ${dinky_schema}.dinky_sys_menu set "path"='/settings/alertrule',
"component"='./SettingCenter/AlertRule',
"perms"='settings:alertrule',
"parent_id"=6
where "id" = 116;

update public.dinky_sys_menu set "path"='/settings/alertrule/add',
update ${dinky_schema}.dinky_sys_menu set "path"='/settings/alertrule/add',
"perms"='settings:alertrule:add'
where "id" = 117;
update public.dinky_sys_menu set "path"='/settings/alertrule/delete',
update ${dinky_schema}.dinky_sys_menu set "path"='/settings/alertrule/delete',
"perms"='settings:alertrule:delete'
where "id" = 118;
update public.dinky_sys_menu set "path"='/settings/alertrule/edit',
update ${dinky_schema}.dinky_sys_menu set "path"='/settings/alertrule/edit',
"perms"='settings:alertrule:edit'
where "id" = 119;

ALTER TABLE public.dinky_task DROP COLUMN "first_level_owner";
ALTER TABLE public.dinky_task DROP COLUMN "second_level_owners";
ALTER TABLE ${dinky_schema}.dinky_task DROP COLUMN "first_level_owner";
ALTER TABLE ${dinky_schema}.dinky_task DROP COLUMN "second_level_owners";


-- Delete the 1.1.0 record in the _dinky_flyway_schema_history table
DELETE FROM public."_dinky_flyway_schema_history" WHERE version = '1.1.0';
DELETE FROM ${dinky_schema}."_dinky_flyway_schema_history" WHERE version = '1.1.0';

UPDATE public.dinky_user SET "password" = '21232f297a57a5a743894a0e4a801fc3' WHERE "id" =1 and "password"= 'f4b3a484ee745b98d64cd69c429b2aa2';
UPDATE ${dinky_schema}.dinky_user SET "password" = '21232f297a57a5a743894a0e4a801fc3' WHERE "id" =1 and "password"= 'f4b3a484ee745b98d64cd69c429b2aa2';


ALTER TABLE public.dinky_resources ALTER COLUMN file_name TYPE varchar(64);
ALTER TABLE ${dinky_schema}.dinky_resources ALTER COLUMN file_name TYPE varchar(64);

alter table public.dinky_udf_manage drop column "language";
alter table ${dinky_schema}.dinky_udf_manage drop column "language";
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# note: Rolling back SQL statements is only necessary to perform a rollback operation in the event of an automatic upgrade failure. The following SQL statements need to be manually executed


delete from public.dinky_sys_menu where parent_id = -1 and path = '/home';
delete from ${dinky_schema}.dinky_sys_menu where parent_id = -1 and path = '/home';

delete from public.dinky_sys_menu where parent_id = 164 and path = '/dashboard/list';
delete from ${dinky_schema}.dinky_sys_menu where parent_id = 164 and path = '/dashboard/list';

update public.dinky_sys_menu set path = '/dashboard/add', perms='dashboard:add' where path = '/dashboard/list/add';
update public.dinky_sys_menu set path = '/dashboard/edit', perms='dashboard:edit' where path = '/dashboard/list/edit';
update public.dinky_sys_menu set path = '/dashboard/delete', perms='dashboard:delete' where path = '/dashboard/list/delete';
update public.dinky_sys_menu set path = '/dashboard/view', perms='dashboard:view' where path = '/dashboard/list/view';
update ${dinky_schema}.dinky_sys_menu set path = '/dashboard/add', perms='dashboard:add' where path = '/dashboard/list/add';
update ${dinky_schema}.dinky_sys_menu set path = '/dashboard/edit', perms='dashboard:edit' where path = '/dashboard/list/edit';
update ${dinky_schema}.dinky_sys_menu set path = '/dashboard/delete', perms='dashboard:delete' where path = '/dashboard/list/delete';
update ${dinky_schema}.dinky_sys_menu set path = '/dashboard/view', perms='dashboard:view' where path = '/dashboard/list/view';


delete from public.dinky_sys_menu where `parent_id` = 164 and path = '/dashboard/dashboard-layout';
delete from public.dinky_sys_menu where `parent_id` = 164 and path = '/dashboard/chart/add';
delete from public.dinky_sys_menu where `parent_id` = 164 and path = '/dashboard/chart/edit';
delete from ${dinky_schema}.dinky_sys_menu where `parent_id` = 164 and path = '/dashboard/dashboard-layout';
delete from ${dinky_schema}.dinky_sys_menu where `parent_id` = 164 and path = '/dashboard/chart/add';
delete from ${dinky_schema}.dinky_sys_menu where `parent_id` = 164 and path = '/dashboard/chart/edit';
Loading
Loading