grails / grails-core

The Grails Web Application Framework
http://grails.org
Apache License 2.0
2.79k stars 949 forks source link

5.0.0.RC1 autoTimestampEventListener.withoutDateCreated or timestamps causing DB null error in integration tests #12010

Closed selamialtin closed 3 years ago

selamialtin commented 3 years ago

i'm trying to set dateCreated manually in integration test in grails 5-rc1, but i'm getting not null error from database Here is the error Cannot insert the value NULL into column 'date_created', table 'material_transaction_history'; column does not allow nulls

And code block i used

ApplicationContext applicationContext = Holders.applicationContext
HibernateDatastore hibernateDatastore = applicationContext.getBean(HibernateDatastore)
AutoTimestampEventListener autoTimestampEventListener = hibernateDatastore.getAutoTimestampEventListener()

autoTimestampEventListener.withoutDateCreated(MaterialTransactionHistory) {
    new MaterialTransactionHistory(
            inOut: MaterialTransInOutEnum.INPUT,
            transactionType: MaterialTransactionTypeEnum.MANUAL,
            quantity: 10,
            matStoreLocation: matStoreLocation,
            material: material,
            referenceId: Long.MIN_VALUE,
            status: MaterialTransactionStatusEnum.NORMAL,
            fifoOutQuantity: BigDecimal.ZERO,
            dateCreated: new Date().clearTime() - 10
    ).save(flush: true, failOnError: true)
}
selamialtin commented 3 years ago

my build.gradle

buildscript {
    repositories {
        maven { url "https://repo.grails.org/grails/core" }
    }
    dependencies {
        classpath "org.grails:grails-gradle-plugin:$grailsGradlePluginVersion"
        classpath "gradle.plugin.com.github.erdi.webdriver-binaries:webdriver-binaries-gradle-plugin:2.4"
        classpath "org.grails.plugins:hibernate5:7.1.0.RC2"
        classpath "com.bertramlabs.plugins:asset-pipeline-gradle:3.3.3"
        classpath "com.gorylenko.gradle-git-properties:gradle-git-properties:2.3.1"
    }
}

version "3.0.0.M1"
group "com.anamar"

//apply plugin: "eclipse"
apply plugin: "idea"
apply plugin: "war"
apply plugin: "org.grails.grails-web"
apply plugin: "com.github.erdi.webdriver-binaries"
apply plugin: "org.grails.grails-gsp"
//apply plugin: "com.bertramlabs.asset-pipeline"
apply plugin: "com.gorylenko.gradle-git-properties"
//apply plugin: "org.zeroturnaround.gradle.jrebel"

ext {
    keycloakVersion = '12.0.4'
    jacksonCoreVersion = '2.12.4'
}

repositories {
    mavenLocal()
    maven { url "https://repo.grails.org/grails/core" }
    maven { url "http://uzmar.tech/mvn/" }
}

configurations {
    developmentOnly
    runtimeClasspath {
        extendsFrom developmentOnly
    }
}

dependencies {
    //developmentOnly("org.springframework.boot:spring-boot-devtools")
    compileOnly "io.micronaut:micronaut-inject-groovy"
    console "org.grails:grails-console"
    implementation "org.springframework.boot:spring-boot-starter-logging"
    implementation "org.springframework.boot:spring-boot-autoconfigure"
    implementation "org.grails:grails-core"
    implementation "org.grails:grails-web"
    implementation "org.springframework.boot:spring-boot-starter-actuator"
    implementation "org.springframework.boot:spring-boot-starter-tomcat"
    implementation "org.springframework.boot:spring-boot-starter-security"
    implementation "org.grails:grails-web-boot"
    implementation "org.grails:grails-logging"
    implementation "org.grails:grails-plugin-rest"
    implementation "org.grails:grails-plugin-databinding"
    implementation "org.grails:grails-plugin-i18n"
    implementation "org.grails:grails-plugin-services"
    implementation "org.grails:grails-plugin-url-mappings"
    implementation "org.grails:grails-plugin-interceptors"
    implementation "org.grails.plugins:cache"
    implementation "org.grails.plugins:async"
    implementation "org.grails:grails-datastore-gorm-async"

    implementation "org.grails.plugins:events"
    implementation "org.grails.plugins:scaffolding"

    implementation "org.hibernate:hibernate-ehcache"
    implementation "org.grails.plugins:hibernate5"
    implementation "org.hibernate:hibernate-core:5.5.6"
    implementation "org.grails.plugins:gsp"

//    implementation "org.grails.plugins:spring-security-core:4.0.3"
    implementation "org.redisson:redisson-spring-boot-starter:3.15.0"
    implementation "org.grails.plugins:rabbitmq-native:4.0.0"
    implementation "org.grails.plugins:grails-spring-websocket:2.5.0.RC1"
    implementation "org.keycloak:keycloak-spring-boot-starter:$keycloakVersion"
    implementation "org.keycloak:keycloak-admin-client:$keycloakVersion"

    implementation "org.grails.plugins:marshallers:4.0.0.M1"
    implementation "org.grails.plugins:jasper:4.0.0.M1"
    implementation 'org.grails.plugins:rest-client-builder:4.0.0.M1'
    implementation "org.grails.plugins:hibernate-filter:4.0.0.M1"

    implementation group: 'commons-io', name: 'commons-io', version: '2.8.0'
    implementation 'commons-net:commons-net:3.3'
    implementation 'org.apache.commons:commons-math:2.0'
    implementation 'com.microsoft.ews-java-api:ews-java-api:2.1.0.ss.0'
    implementation 'net.sf.mpxj:mpxj:7.6.0'
    implementation 'org.apache.solr:solr-core:5.5.4'
    implementation 'org.apache.poi:poi:4.1.0'
    implementation 'org.apache.poi:poi-ooxml:4.1.0'
    implementation 'net.coobird:thumbnailator:0.4.11'
    implementation "com.fasterxml.jackson.core:jackson-core:$jacksonCoreVersion"
    implementation "com.fasterxml.jackson.core:jackson-databind:$jacksonCoreVersion"
    implementation "com.fasterxml.jackson.datatype:jackson-datatype-jsr310:$jacksonCoreVersion"

    implementation 'org.apache.xmlgraphics:batik-anim:1.11'
    implementation 'org.apache.xmlgraphics:batik-awt-util:1.11'
    implementation 'org.apache.xmlgraphics:batik-bridge:1.11'
    implementation 'org.apache.xmlgraphics:batik-css:1.11'
    implementation 'org.apache.xmlgraphics:batik-dom:1.11'
    implementation 'org.apache.xmlgraphics:batik-ext:1.11'
    implementation 'org.apache.xmlgraphics:batik-gvt:1.11'
    implementation 'org.apache.xmlgraphics:batik-parser:1.11'
    implementation 'org.apache.xmlgraphics:batik-script:1.11'
    implementation 'org.apache.xmlgraphics:batik-svg-dom:1.11'
    implementation 'org.apache.xmlgraphics:batik-svggen:1.11'
    implementation 'org.apache.xmlgraphics:batik-transcoder:1.11'
    implementation 'org.apache.xmlgraphics:batik-util:1.11'
    implementation 'org.apache.xmlgraphics:batik-xml:1.11'
    implementation group: 'xml-apis', name: 'xml-apis-ext', version: '1.3.04'
    implementation group: 'xml-apis', name: 'xml-apis', version: '1.0.b2'
    implementation group: 'net.sf.barcode4j', name: 'barcode4j', version: '2.1'
    implementation group: 'com.uzmar', name: 'asfat-eys-api', version: '1.0.5'
    implementation group: 'net.sourceforge.jexcelapi', name: 'jxl', version: '2.6.12'
    implementation group: 'org.apache.avalon.framework', name: 'avalon-framework-impl', version: '4.3.1'
    implementation group: 'org.apache.ant', name: 'ant', version: '1.7.1'
    implementation 'org.imgscalr:imgscalr-lib:4.2'
    implementation 'net.sourceforge.jexcelapi:jxl:2.6.12'
    implementation 'org.liquibase:liquibase-core:4.3.1'
    implementation group: 'org.codehaus.groovy', name: 'groovy-dateutil', version: '3.0.7'

    runtimeOnly 'net.sourceforge.jtds:jtds:1.3.0'
    runtimeOnly "com.microsoft.sqlserver:mssql-jdbc"
    runtimeOnly group: 'com.oracle.ojdbc', name: 'ojdbc8', version: '19.3.0.0'
    runtimeOnly group: 'com.oracle.ojdbc', name: 'orai18n', version: '19.3.0.0'

    profile "org.grails.profiles:web"
    runtimeOnly "org.glassfish.web:el-impl:2.2.1-b05"
    runtimeOnly "com.h2database:h2:1.4.200"
    runtimeOnly "org.apache.tomcat:tomcat-jdbc"
    runtimeOnly "javax.xml.bind:jaxb-api:2.3.1"
    runtimeOnly "com.bertramlabs.plugins:asset-pipeline-grails:3.3.3"
    testImplementation "io.micronaut:micronaut-inject-groovy"
    testImplementation "org.grails:grails-gorm-testing-support"
    testImplementation "org.mockito:mockito-core"
    testImplementation "org.grails:grails-web-testing-support"
    testImplementation "org.grails.plugins:geb"
    testImplementation "org.seleniumhq.selenium:selenium-remote-driver:3.141.59"
    testImplementation "org.seleniumhq.selenium:selenium-api:3.141.59"
    testImplementation "org.seleniumhq.selenium:selenium-support:3.141.59"
    testRuntimeOnly "org.seleniumhq.selenium:selenium-chrome-driver:3.141.59"
    testRuntimeOnly "org.seleniumhq.selenium:selenium-firefox-driver:3.141.59"
}

gitProperties {
    dateFormat = "yyyy-MM-dd'T'HH:mmZ"
    dateFormatTimeZone = "GMT+3"
}

bootRun {
    ignoreExitValue true
    sourceResources sourceSets.main
    jvmArgs(
            '-Dspring.output.ansi.enabled=always',
            '-noverify',
            '-XX:TieredStopAtLevel=1',
            '-Xmx12G',
            '-Xms8G',
            '-server',
            '-Duser.language=en',
            '-Duser.country=US',
            '-Dgroovy.antlr4=true',
            '-Dgroovy.parallel.parse=true',
            '-Dgroovy.antlr4.cache.threshold=100000',
//        '-XX:HotswapAgent=external'
//        '-javaagent:/home/selami/Belgeler/app/trava/latest/hotswap-agent.jar=autoHotswap=true'
//        '-javaagent:/home/selami/Belgeler/app/trava/latest/hotswap-agent.jar=disablePlugin=hibernate'
            '-agentpath:/home/selami/Belgeler/app/jrebel/latest/lib/libjrebel64.so' // & JREBEL
    )
    String springProfilesActive = 'spring.profiles.active'
    systemProperty springProfilesActive, System.getProperty(springProfilesActive)
}

tasks.withType(GroovyCompile) {
    configure(groovyOptions) {
        forkOptions.jvmArgs = [
                '-noverify',
                '-Xmx12G',
                '-Duser.language=en',
                '-Duser.country=US'
        ]
    }
}

tasks.withType(Test) {
    useJUnitPlatform()
}

webdriverBinaries {
    chromedriver '2.45.0'
    geckodriver '0.24.0'
}

tasks.withType(Test) {
    systemProperty "geb.env", System.getProperty('geb.env')
    systemProperty "geb.build.reportsDir", reporting.file("geb/integrationTest")
    systemProperty "webdriver.chrome.driver", System.getProperty('webdriver.chrome.driver')
    systemProperty "webdriver.gecko.driver", System.getProperty('webdriver.gecko.driver')
}

//war.dependsOn(generateRebel)

//assets {
//    minifyJs = true
//    minifyCss = true
//}

buildProperties.doLast {
    // Find the right file
    File grailsBuildInfoFile = it.outputs.files.files.find { it.name == 'grails.build.info' }
    if (!grailsBuildInfoFile) return // No need to continue if the file is not there
    Properties properties = new Properties()
    // Read properties from the file
    grailsBuildInfoFile.withInputStream {
        properties.load(it)
    }
    // Add new properties from various sources
    properties.setProperty('build.time', new Date().format("yyyy-MM-dd HH:mm:ss"))
    // Get a System property
    properties.setProperty('build.java.version', System.getProperty('java.version'))
    // Get the host name where the build was created
    properties.setProperty('build.host', InetAddress.localHost.hostName)

    // Write the properties back to the file
    grailsBuildInfoFile.withOutputStream {
        properties.store(it, null)
    }
}
puneetbehl commented 3 years ago

It seems like you might have nullable:false constraint on the dateCreated field which would generate the database schema with date_created NOT NULL. Please update the constraint on the property dateCreated to nullable: true. See https://gorm.grails.org/latest/hibernate/manual/index.html#constraints for more details