### Apply Kubernetes sample Source: https://github.com/4sh/datamaintain/blob/dev/samples/k8s/README.md Start the busybox pod that uses Datamaintain in an init container. ```bash kubectl apply -f ./sample_k8s.yaml ``` -------------------------------- ### Example branch name Source: https://github.com/4sh/datamaintain/blob/dev/CONTRIBUTING.md Example branch name that solves issue #71 and concerns relative path matchers. ```text #71-relative-path-matchers ``` -------------------------------- ### Unit test example Source: https://github.com/4sh/datamaintain/blob/dev/CONTRIBUTING.md Example unit test function following the project conventions: name starts with 'should', divided into given/when/then sections, and uses strikt's expectThat for assertions. ```kotlin @Test fun `should do what I want`() { // Given val initialValue = "myInitialValue" // When val result = myClass.doSomething(initialValue) // Then expectThat(result).isEqualTo("expectedResult") } ``` -------------------------------- ### Install MongoDB with Helm Source: https://github.com/4sh/datamaintain/blob/dev/samples/k8s/README.md Install MongoDB 4.2 using Helm and the stable/mongodb-replicaset chart. If you use another chart, you may need to change the mongo URI in sample-k8s.yaml. ```bash helm upgrade --install mongo stable/mongodb-replicaset ``` -------------------------------- ### Verify executed scripts Source: https://github.com/4sh/datamaintain/blob/dev/samples/k8s/README.md Check that Datamaintain started correctly by querying the executedScripts collection. You must find one entry: v1.0_init.js. ```bash kubectl exec mongo-mongodb-replicaset-0 -- mongo --quiet --eval 'db.getCollection("executedScripts").find()' ``` -------------------------------- ### Example commit message Source: https://github.com/4sh/datamaintain/blob/dev/CONTRIBUTING.md Example commit message that adds the whitelisted tags feature described by issue #80. ```text feat: #80 Introduce notion of whitelisted tags ``` -------------------------------- ### BSON ObjectId JSON example Source: https://github.com/4sh/datamaintain/blob/dev/docs/mongo-json-mapping.md Example of a JSON document with a BSON ObjectId serialized by Jackson with BSON module, which is not supported by the mongo shell. ```json {_id: {$oid: "62a886bc8c0dd9e44f9e5a83"}} ``` -------------------------------- ### Sample project tree structure for (java|kotlin)- Source: https://github.com/4sh/datamaintain/blob/dev/docs/how-to-write-a-driver.md Displays the required file layout for a driver sample, including build.gradle.kts, main source, resources config, and scripts directory. ```text . ├── ... └── (java|kotlin)- ├── build.gradle.kts └── src ├── main │ └── (java|kotlin) │ └── datamaintain │ └── samples │ └── (Java|Kotlin)SampleMain.(java|kt) └── resources ├── config │ └── datamaintain.properties └── scripts ├── 1_script1 ├── 2_script2 └── 3_script3 ``` -------------------------------- ### Run Postgres container for the sample Source: https://github.com/4sh/datamaintain/blob/dev/samples/java-postgresql/README.md Initializes a local Postgres container so you can run the sample scripts against it. Sets trust authentication and exposes port 5432. ```bash docker run -e POSTGRES_HOST_AUTH_METHOD=trust -p 5432:5432 --name datamaintain_postgres postgres ``` -------------------------------- ### Build datamaintain-sample image with minikube Source: https://github.com/4sh/datamaintain/blob/dev/samples/k8s/README.md Build the Datamaintain sample Docker image locally when using minikube. Run these commands in the docker folder. ```bash eval $(minikube docker-env) docker build -t datamaintain-sample . ``` -------------------------------- ### Sample datamaintain.properties Source: https://github.com/4sh/datamaintain/blob/dev/docs/how-to-write-a-driver.md Configures scan.path to the sample scripts directory and a regex for script identifiers. ```text scan.path=samples/(java|kotlin)-/src/main/resources/scripts scan.identifier.regex=(.*?)_.* ``` -------------------------------- ### View init container logs Source: https://github.com/4sh/datamaintain/blob/dev/samples/k8s/README.md Read logs from the wait-mongo and datamaintain init containers. ```bash kubectl logs busybox -c wait-mongo kubectl logs busybox -c datamaintain ``` -------------------------------- ### Sample build.gradle.kts for driver sample Source: https://github.com/4sh/datamaintain/blob/dev/docs/how-to-write-a-driver.md Required build.gradle.kts for a driver sample, using the Kotlin JVM plugin and dependencies on the core and driver modules. ```kotlin plugins { id("org.jetbrains.kotlin.jvm") } repositories { mavenCentral() } dependencies { implementation(project(":modules:core")) implementation(project(":modules:driver-")) } ``` -------------------------------- ### Java sample main class for Datamaintain driver Source: https://github.com/4sh/datamaintain/blob/dev/docs/how-to-write-a-driver.md Java main class that loads properties, builds the driver config and Datamaintain config, then updates the database. It uses the DriverConfig and Datamaintain classes. ```java public class JavaSampleMain { public static void main(String[] args) throws IOException { // Retrieve datamaintain properties final Properties properties = new Properties(); properties.load(JavaMongoSampleMain.class.getResourceAsStream("/config/datamaintain.properties")); // Instantiate DMBS name driver config final DatamaintainDriverConfig datamaintainDriverConfig = DriverConfig.buildConfig(properties); // Instantiate datamaintain config final DatamaintainConfig config = DatamaintainConfig.buildConfig(datamaintainDriverConfig, properties); // Launch database update new Datamaintain(config).updateDatabase(); } } ``` -------------------------------- ### Check pod status Source: https://github.com/4sh/datamaintain/blob/dev/samples/k8s/README.md Follow the creation of the busybox pod. ```bash kubectl get pods ``` -------------------------------- ### Build Datamaintain config from properties in Kotlin Source: https://github.com/4sh/datamaintain/blob/dev/docs/as-dependency.md Loads a properties file and builds Datamaintain driver and config objects using the buildConfig method. ```kotlin val properties = Properties() properties.load(MyClass::class.java.getResourceAsStream("datamaintain.properties")) val driverConfig: DatamaintainDriverConfig = DatamaintainConfig.buildConfig(properties) val config = DatamaintainConfig.buildConfig(driverConfig, properties) ``` -------------------------------- ### Load Datamaintain configuration from properties file Source: https://github.com/4sh/datamaintain/blob/dev/docs/as-dependency.md Loads Datamaintain configuration from a properties file using the buildConfig method. The properties file defines scan path, identifier regex, and database URI. ```properties scan.path=/scripts scan.identifier.regex=(.*?)_.* db.uri=mongodb://localhost:27017/datamaintain ``` -------------------------------- ### Create Datamaintain config with Kotlin constructor Source: https://github.com/4sh/datamaintain/blob/dev/docs/as-dependency.md Creates Datamaintain configuration objects directly using Kotlin constructors, specifying MongoDB URI, scan path, and identifier regex. ```kotlin val driverConfig: DatamaintainDriverConfig = MongoDriverConfig(uri = "mongodb://localhost:27017/datamaintain") val config = DatamaintainConfig( scanner = DatamaintainScannerConfig( path = Paths.get("/scripts"), identifierRegex = "(.*?)_.*", ), driverConfig = driverConfig ) ``` -------------------------------- ### build.gradle.kts for driver module Source: https://github.com/4sh/datamaintain/wiki/How-to-create-a-new-driver-for-Datamaintain Kotlin Gradle build script for the driver module, including plugins, baseProject(), repositories, and dependencies on the core module. ```kotlin plugins { id("org.jetbrains.kotlin.jvm") maven // Needed for Jitpack } baseProject() repositories { jcenter() } dependencies { compileOnly(project(":modules:core")) testImplementation(project(":modules:core")) } ``` -------------------------------- ### Directory tree for driver module Source: https://github.com/4sh/datamaintain/wiki/How-to-create-a-new-driver-for-Datamaintain Shows the required directory structure for a new driver module named driver-. ```text . ├── build.gradle.kts └── src ├── main │ └── kotlin │ └── datamaintain │ └── db │ └── driver │ └── └── test └── kotlin └── datamaintain └── db └── driver └── ``` -------------------------------- ### Mark scripts as executed with Datamaintain CLI Source: https://github.com/4sh/datamaintain/blob/dev/docs/faq.md Use this command to mark existing scripts as executed when adopting Datamaintain on a project that already has executed scripts. It requires the CLI to be downloaded and the configuration keys $DB_TYPE, $MONGO_URI, $PATH, and $REGEX to be set. ```bash ./datamaintain-cli --db-type $DB_TYPE --db-uri $MONGO_URI update-db --path $PATH --identifier-regex $REGEX --action MARK_AS_EXECUTED ``` -------------------------------- ### Create Datamaintain config with Java Builder Source: https://github.com/4sh/datamaintain/blob/dev/docs/as-dependency.md Creates Datamaintain configuration objects using Java Builder classes, as Kotlin default parameters are not available in Java. ```java public class MyClass { final DatamaintainDriverConfig driverConfig = new MongoDriverConfig.Builder() .withUri("mongodb://localhost:27017/datamaintain-sample-java-mongo") .build(); final DatamaintainConfig config = new DatamaintainConfig.Builder() .withPath(Paths.get("/scripts")) .withIdentifierRegex(new Regex("(.*?)_.*")) .withDriverConfig(driverConfig) .build(); } ``` -------------------------------- ### Add driver module to root settings.gradle.kts Source: https://github.com/4sh/datamaintain/blob/dev/docs/how-to-write-a-driver.md Adds the new driver module to the root project settings include list. The snippet shows the include block with a placeholder for the module name. ```kotlin rootProject.name = "datamaintain" include( ... "modules:driver-", ... ) ``` -------------------------------- ### build.gradle.kts for driver module - plugins block Source: https://github.com/4sh/datamaintain/blob/dev/docs/how-to-write-a-driver.md Minimal build.gradle.kts for a driver module. It applies the datamaintain conventions plugins for kotlin, publishing, and driver. ```kotlin plugins { id("datamaintain.conventions.kotlin") id("datamaintain.conventions.publishing") id("datamaintain.conventions.driver") } ``` -------------------------------- ### Gradle Kotlin DSL: Add Datamaintain dependencies Source: https://github.com/4sh/datamaintain/blob/dev/docs/as-dependency.md Add the core, Mongo driver, and Mongo mapping serialization dependencies to your build.gradle using the Kotlin DSL. ```kotlin dependencies { implementation("io.github.4sh.datamaintain:datamaintain-core:2.0.0-1"), implementation("io.github.4sh.datamaintain:datamaintain-driver-mongo:2.0.0-1") implementation("io.github.4sh.datamaintain:datamaintain-driver-mongo-mapping-serialization:2.0.0-1") } ``` -------------------------------- ### Gradle Groovy DSL: Add Datamaintain dependencies Source: https://github.com/4sh/datamaintain/blob/dev/docs/as-dependency.md Add the core, Mongo driver, and Mongo mapping serialization dependencies to your build.gradle using the Groovy DSL. ```groovy dependencies { implementation 'io.github.4sh.datamaintain:datamaintain-core:2.0.0-1', implementation 'io.github.4sh.datamaintain:datamaintain-driver-mongo:2.0.0-1', implementation 'io.github.4sh.datamaintain:datamaintain-driver-mongo-mapping-serialization:2.0.0-1', } ``` -------------------------------- ### Gradle Kotlin DSL: Declare Maven Central in root build.gradle Source: https://github.com/4sh/datamaintain/blob/dev/docs/as-dependency.md Add mavenCentral() to your repositories so Gradle can resolve Datamaintain artifacts. The second block shows the expected structure within allprojects. ```kotlin mavenCentral() ``` ```kotlin allprojects { repositories { ... mavenCentral() } } ``` -------------------------------- ### Run Datamaintain CLI with Docker Source: https://github.com/4sh/datamaintain/blob/dev/docs/with-cli.md Runs Datamaintain via a Docker image, mounting the script folder to /scripts and passing CLI arguments after the image name. The --rm flag removes the container after execution, and on Mac OS you may need to replace localhost with host.docker.internal in the MongoDB URI. ```bash docker run --rm --volume $script_path:/scripts docker.pkg.github.com/4sh/datamaintain/datamaintain:1.2.0-mongo-4.4 --db-type mongo --db-uri mongodb://localhost:27017/sample update-db --path /scripts --identifier-regex "(.*)" ``` -------------------------------- ### Maven: Add Datamaintain dependencies Source: https://github.com/4sh/datamaintain/blob/dev/docs/as-dependency.md Add the core, Mongo driver, and Mongo mapping serialization dependencies to your pom.xml. ```xml io.github.4sh.datamaintain datamaintain-core 2.0.0-1 io.github.4sh.datamaintain datamaintain-driver-mongo 2.0.0-1 io.github.4sh.datamaintain datamaintain-driver-mongo-mapping-serialization 2.0.0-1 ``` -------------------------------- ### Configure jsonMapper via properties (SerializationMapper) Source: https://github.com/4sh/datamaintain/blob/dev/docs/mongo-json-mapping.md Set the jsonMapper via the property db.mongo.json.mapper to use the SerializationMapper. ```properties db.mongo.json.mapper=datamaintain.db.driver.mongo.serialization.SerializationMapper ``` -------------------------------- ### Configure custom JsonMapper via properties Source: https://github.com/4sh/datamaintain/blob/dev/docs/mongo-json-mapping.md Specify a custom JsonMapper via the property db.mongo.json.mapper. The implementation must have a public constructor with no arguments. ```properties db.mongo.json.mapper=datamaintain.db.driver.mongo.MyScriptMapper ``` -------------------------------- ### Create and use Datamaintain object in Kotlin Source: https://github.com/4sh/datamaintain/blob/dev/docs/as-dependency.md Creates a Datamaintain object from a DatamaintainConfig and calls updateDatabase to execute database scripts. ```kotlin val driverConfig: DatamaintainDriverConfig = ... val config = ... Datamaintain(config).updateDatabase() ``` -------------------------------- ### Add mongo mapping dependency (Kotlinx Serialization) Source: https://github.com/4sh/datamaintain/blob/dev/docs/mongo-json-mapping.md Add the Datamaintain mongo driver mapping dependency for Kotlinx Serialization to your Gradle build. Requires core and driver-mongo dependencies as well. ```kotlin dependencies { implementation("io.github.4sh.datamaintain:datamaintain-core:2.0.0-1"), implementation("io.github.4sh.datamaintain:datamaintain-driver-mongo:2.0.0-1") implementation("io.github.4sh.datamaintain:datamaintain-driver-mongo-mapping-serialization:2.0.0-1") implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:latest.integration") } ``` -------------------------------- ### Implement custom JsonMapper Source: https://github.com/4sh/datamaintain/blob/dev/docs/mongo-json-mapping.md Implement the JsonMapper interface with custom fromJson and toJson methods. The serializer must accept unknown fields on JSON for compatibility. ```kotlin package datamaintain.db.driver.mongo class MyJsonMapper: JsonMapper { override fun fromJson(json: String, clazz: Class): T = ... override fun toJson(aObject: Any): String = ... } ``` -------------------------------- ### Use MongoDriver with auto-loaded jsonMapper Source: https://github.com/4sh/datamaintain/blob/dev/docs/mongo-json-mapping.md Create a MongoDriver without specifying jsonMapper when exactly one mapping dependency is loaded; the mapper is auto-loaded. ```kotlin // Do not fill json mapper. It will be loaded automatically MongoDriver( mongoUri = mongoUri, ) ``` -------------------------------- ### Test custom JsonMapper with JsonMapperTest Source: https://github.com/4sh/datamaintain/blob/dev/docs/mongo-json-mapping.md Test a custom JsonMapper implementation by extending JsonMapperTest from the datamainain-mongo-driver-mapping-test dependency. ```kotlin import datamaintain.db.driver.mongo.MyJsonMapper internal class MyJsonMapperTest: JsonMapperTest(MyJsonMapper()) ``` -------------------------------- ### Use custom JsonMapper in MongoDriver Source: https://github.com/4sh/datamaintain/blob/dev/docs/mongo-json-mapping.md Use a custom JsonMapper implementation by passing it to the MongoDriver constructor. ```kotlin MongoDriver( mongoUri = mongoUri, jsonMapper = MyScriptMapper() ) ``` -------------------------------- ### Branch naming format Source: https://github.com/4sh/datamaintain/blob/dev/CONTRIBUTING.md Branch naming convention: the issue number followed by a hyphen and a short description of the issue. ```text -issue-description ``` -------------------------------- ### Commit message format Source: https://github.com/4sh/datamaintain/blob/dev/CONTRIBUTING.md Commit messages must follow this format: type, issue reference, and a descriptive message. The type can be feat, fix, chore, docs, refactor, or test. ```text : ``` -------------------------------- ### Specify jsonMapper with SerializationMapper Source: https://github.com/4sh/datamaintain/blob/dev/docs/mongo-json-mapping.md Specify the jsonMapper explicitly when two or more mapping dependencies are present, using SerializationMapper for Kotlinx Serialization. ```kotlin MongoDriver( mongoUri = mongoUri, jsonMapper = SerializationMapper() ) ``` -------------------------------- ### mongosh insert with $oid error Source: https://github.com/4sh/datamaintain/blob/dev/docs/mongo-json-mapping.md Shows the error when inserting a document with a $oid field using mongosh, illustrating why BSON frameworks are not used. ```shell $ mongosh --eval 'db.test.insert({_id: {$oid: "62a886bc8c0dd9e44f9e5a83"}})' MongoBulkWriteError: $oid is not valid for storage. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.