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

Changed schema:recreate_postgres to user copy instead of insert #76

Merged
merged 1 commit into from
Jun 27, 2024
Merged
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
4 changes: 4 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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)
Expand Down