Kotlin Room DB Export schemas - kotlin

class RoomSchemaArgProvider(
#get:InputDirectory
#get:PathSensitive(PathSensitivity.RELATIVE)
val schemaDir: File
) : CommandLineArgumentProvider {
override fun asArguments(): Iterable<String> {
// Note: If you're using KSP, you should change the line below to return
// listOf("room.schemaLocation=${schemaDir.path}")
return listOf("-Aroom.schemaLocation=${schemaDir.path}")
}
}
I need to Export old db schema in json. I wanted to use the above code if any one used this as per https://developer.android.com/training/data-storage/room/migrating-db-versions#export-schemas please help me with the same.
i tried to use as per this https://developer.android.com/training/data-storage/room/migrating-db-versions#export-schemas
problem faced during testing migration. hence i need this solution.
java.io.FileNotFoundException: Cannot find the schema file in the assets folder. Make sure to include the exported json schemas in your test assert inputs. See https://developer.android.com/training/data-storage/room/migrating-db-versions#export-schema for details. Missing file: com.sboxnw.freeplay.data.database.SugarBoxDatabase/2.json
at androidx.room.testing.MigrationTestHelper.loadSchema(MigrationTestHelper.java:484)
at androidx.room.testing.MigrationTestHelper.createDatabase(MigrationTestHelper.java:238)
at com.sboxnw.freeplay.DownloadMigrationTest.testAllMigrations(DownloadMigrationTest.kt:72)
I need to Export all old db json schema for migration testing .

It is failing due to src path not define correctly. You can add source path directly in defaultConfig of build.gradle
javaCompileOptions {
annotationProcessorOptions {
arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
}
}

Related

How to create a file in the resources folder in Kotlin, Gradle

In a completely fresh project, I want to create a single file myFile.json inside the src/main/resources/ folder at run time.
For reading a file, I need to do some config in the build.gradle.kts file, but I can't find anything on what to do for creating a file.
Assuming the directory src/main/resources/ exists:
val f = File("src/main/resources/myFile.json")
withContext(Dispatchers.IO) {
f.createNewFile() // This is the answer to the question
f.printWriter().use { out ->
out.println("{}")
}
}
#Endzeit has asked what have you tried so far. Please share the code.
Like #cyberbrain says - are you sure you want to write to resources folder?
Here is code that writes back to where the source resources folder is:
fun main(args: Array<String>) {
// Let's assume you want your project to be portable, so you don't
// want to use absolute file paths.
// Find out where your IDE will launch the project from. Normally this is
// the root folder of the whole project. Find out with this: the `canonicalPath` will help:
val workingFolder = File(".")
println("workingFolder=${workingFolder.canonicalPath}")
// Define the folder you want to write in to
// this will vary especially if you have a nested project structure
// IntelliJ under the Edit > Copy Path menu option will help you find the resources
// relative location
val parentFolder = File("src/main/resources")
println("parentFolder=${parentFolder.canonicalPath}")
require(parentFolder.exists())
val outFile = File(parentFolder, "test.txt")
outFile.printWriter(StandardCharsets.UTF_8).use {
it.println("Hello world")
}
println("Wrote to ${outFile.canonicalPath}")
}

Karate - Referencing variable within schema, js function

I have .js file whose content is as below... where companyAssociations is list of object for which i have defined schema on top in var...
function() {
var companyAssociationsSchema = '{"companyId":"#string", "displayName":"#string", "associationType":"#string", "companyType":"#string" }';
return {
"StationSchema":{
"stationId":"#string",
"companyAssociations":"#[] "+ companyAssociationsSchema,
"primaryBroadcastLanguage":"#string"
}
}
}
and here is how i read/load schema in my feature file * def getStationResponse = call read(path to above file) and than wherever i need to check for above schema i refer to it as getStationResponse.StationSchema
above works in karate 0.9.6 but i am having hard time getting same thing working in karate 1.1.0
Any help would be appreciated

why read tsconfig.json using readConfigFile instead of directly requiring the path of tsconfig.json?

Upon investigating create-react-app's configuration, I found something interesting.
// config/modules.js
...
if (hasTsConfig) {
const ts = require(resolve.sync("typescript", {
basedir: paths.appNodeModules,
}));
config = ts.readConfigFile(paths.appTsConfig, ts.sys.readFile).config;
// Otherwise we'll check if there is jsconfig.json
// for non TS projects.
} else if (hasJsConfig) {
config = require(paths.appJsConfig);
}
...
Unlike reading jsconfig.json file using direct require(paths.appJsConfig), why is here using resolve.sync and ts.readConfigFile to read the tsconfig.json?
...
if (hasTsConfig) {
config = require(paths.appTsConfig)
// Otherwise we'll check if there is jsconfig.json
// for non TS projects.
} else if (hasJsConfig) {
config = require(paths.appJsConfig);
}
...
If I change the code like just above, the result is same. (at least the console output is same.)
There must be a reason why create-react-app using such a complicated way to read the typescript config file.
Why is that?
The ts config reader is a bit smarter than simply reading and parsing a json file. There's two differences I can think of right now:
in tsconfig files, you can use comments. JSON.parse will throw an exception because / is not an allowed character at an arbitrary position
ts config files can extend each other. Simply parsing a JSON file will ignore the extension and you'll receive a config object that doesn't represent what typescript actually uses.

Serenity+jbehave : How to pass test data from external resources

I am using Jbehave and serenity in my BDDs.My requirement is "Passing test data from excel sheet".How do I get the test data from excel in my Given when and then?
I tried with injecting the test data to a test step,
withTestDataFrom( filePath ).run( testSteps ).givenStatement( #param1,#param2 );
But that wont satisfies my requirement. Is there any other way to do it?
you can use Apache POI just like java code. refe this link and this.
you can also try the below code for any other external input cases.
you can use the properties file like this.
you can also use the JBehave table parameter just like this.
This worked for me:
put the pipe ("|") delimited rows into src\test\resources\data\data.table
in build.gradle, put
sourceSets {
main {
java {
srcDirs = ['src/main/java']
}
}
test {
java {
srcDirs = ['src/test/java']
}
resources {
srcDirs = ['src/test/resources']
}
}
}
in the .story file, write:
Examples:
data\data.table

dynamic file path in log4php

I am new to log4php.
I would like to save the log files in the format /logs/UserId/Info_ddmmyyyy.php
where the UserId is dynamic data.
(I would basically like to save one log per user.)
Is there any way to change the log file path dynamically?
This behaviour is not supported by default. But you can extend LoggerAppenderFile (or RollingFile, DailyFile whatever your preference is) to support it.
Create your own class for that and make it load to your script.
Then extend from this class:
http://svn.apache.org/repos/asf/logging/log4php/trunk/src/main/php/appenders/LoggerAppenderFile.php
class MyAppender extends LoggerAppenderFile { ... }
You'll need to overwrite the setFile() method, similar to:
public function setFile($file) {
$path = getYourFullPath();
$this->file = $path.$file;
}
After all you need to use your new Appender in you config
log4php.appender.myAppender = MyAppender
log4php.appender.myAppender.layout = LoggerLayoutSimple
log4php.appender.myAppender.file = my.log
Please note, instead of giving your full path to the log file you now need to add a plain name. The full path (including username) must be calculated with your getYourFullPath() method.
Hope that helps!
Christian