From 81265d3a5ad842dc26add4544e7fabba12579117 Mon Sep 17 00:00:00 2001 From: Daniel Carneiro Date: Wed, 26 Jun 2024 17:42:58 -0300 Subject: [PATCH] Changed schema:recreate_postgres to user copy instead of insert --- Changelog.md | 4 ++ build.gradle | 2 +- .../schema/BeePostgresSchemaCreator.groovy | 64 +++++++++++++++++++ 3 files changed, 69 insertions(+), 1 deletion(-) diff --git a/Changelog.md b/Changelog.md index 6abad83..f203137 100644 --- a/Changelog.md +++ b/Changelog.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [1.103] - 2024-06-26 +### Changed +- Changed schema:recreate_postgres to user copy instead of insert + ## [1.102] - 2024-01-11 ### Fixed - Connection leak on DbChangeManager diff --git a/build.gradle b/build.gradle index 7c16d0d..e23149c 100644 --- a/build.gradle +++ b/build.gradle @@ -17,7 +17,7 @@ mainClassName = "br.com.bluesoft.bee.Bee" group = 'br.com.bluesoft.bee' def artifact = 'bee' -version = '1.102' +version = '1.103' def javaVersion = JavaVersion.VERSION_1_8 sourceCompatibility = javaVersion; diff --git a/src/main/groovy/br/com/bluesoft/bee/schema/BeePostgresSchemaCreator.groovy b/src/main/groovy/br/com/bluesoft/bee/schema/BeePostgresSchemaCreator.groovy index 3557dd3..622573a 100644 --- a/src/main/groovy/br/com/bluesoft/bee/schema/BeePostgresSchemaCreator.groovy +++ b/src/main/groovy/br/com/bluesoft/bee/schema/BeePostgresSchemaCreator.groovy @@ -1,5 +1,6 @@ package br.com.bluesoft.bee.schema +import br.com.bluesoft.bee.util.CsvUtil import br.com.bluesoft.bee.util.RDBMS class BeePostgresSchemaCreator extends BeeSchemaCreator { @@ -76,6 +77,69 @@ class BeePostgresSchemaCreator extends BeeSchemaCreator { } } + void createCsvData(def file, def csvFile, def schema, def useCommit) { + def tableName = csvFile.name.split('\\.')[0] + def fileData = CsvUtil.read(csvFile) + def table = schema.tables[tableName] + def columnNames = [] + def columns = [:] + def columnTypes = [:] + def isVirtualColumn = [:] + def numberOfVirtualColumns = 0 + + if (table != null) { + table.columns.findAll { !it.value.ignore }.each { + columns[it.value.name] = it.value.type + columnNames << it.value.name + isVirtualColumn[it.value.name] = it.value.virtual + if (it.value.virtual) { + numberOfVirtualColumns++ + } + } + + columnNames.eachWithIndex { columName, index -> + columnTypes[index] = columns[columName] + } + + def counterValue = 1 + + def query = new StringBuilder() + query << "copy ${tableName} (${table.columns.findAll({!it.value.virtual && !it.value.ignore})*.value.name.join(",")}) from stdin;\n" + + for (int i = 0; i < fileData.size(); i++) { + fileData[i].eachWithIndex { columnValue, index2 -> + def fieldValue = columnValue.toString().replace('\t', '\\t').replace('\\', '\\\\') + def columnType = columnTypes[index2].split(' ')[0].split('\\(')[0] + def columnName = columnNames[index2] + def isVirtual = isVirtualColumn[columnName] + def isBoolean = columnType == 'boolean' + if (!isVirtual) { + if(isBoolean) { + fieldValue = toBoolean(fieldValue) + } + if(fieldValue == "null") { + fieldValue = '\\N' + } + query << fieldValue + } + if ((counterValue + numberOfVirtualColumns) < (columnNames.size())) { + query << "\t" + } + counterValue++ + } + query << "\n" + counterValue = 1 + } + + query << "\\.\n\n" + + if(useCommit) { + query << "commit;\n" + } + file.append(query.toString(), 'utf-8') + } + } + void createCoreData(def file, def schema, def dataFolderPath) { if (!schema.filtered) { super.createCoreData(file, schema, dataFolderPath, false)