{"id":16423521,"url":"https://github.com/piotrpolak/spring-boot-data-fixtures","last_synced_at":"2025-03-23T07:32:30.066Z","repository":{"id":57742650,"uuid":"300387893","full_name":"piotrpolak/spring-boot-data-fixtures","owner":"piotrpolak","description":"Loading Initial Data with Spring Boot made easy","archived":false,"fork":false,"pushed_at":"2023-04-26T12:42:52.000Z","size":110,"stargazers_count":14,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-01T23:01:59.684Z","etag":null,"topics":["datafixture","demodata","spring","spring-boot","spring-starters"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/piotrpolak.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-10-01T18:44:38.000Z","updated_at":"2024-10-16T15:33:38.000Z","dependencies_parsed_at":"2024-10-28T15:28:45.473Z","dependency_job_id":"92548ecc-7e2f-49d7-88f7-cc1bb7325151","html_url":"https://github.com/piotrpolak/spring-boot-data-fixtures","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/piotrpolak%2Fspring-boot-data-fixtures","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/piotrpolak%2Fspring-boot-data-fixtures/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/piotrpolak%2Fspring-boot-data-fixtures/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/piotrpolak%2Fspring-boot-data-fixtures/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/piotrpolak","download_url":"https://codeload.github.com/piotrpolak/spring-boot-data-fixtures/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244281630,"owners_count":20427867,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["datafixture","demodata","spring","spring-boot","spring-starters"],"created_at":"2024-10-11T07:40:04.018Z","updated_at":"2025-03-23T07:32:29.744Z","avatar_url":"https://github.com/piotrpolak.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Spring Boot Data Fixtures starter\n[![codecov](https://codecov.io/gh/piotrpolak/spring-boot-data-fixtures/branch/master/graph/badge.svg?token=MC4ZZAQCTJ)](https://codecov.io/gh/piotrpolak/spring-boot-data-fixtures/)\n[![Codacy Badge](https://app.codacy.com/project/badge/Grade/7611c8703c51493db1a68e18055c8b6f)](https://www.codacy.com/gh/piotrpolak/spring-boot-data-fixtures/dashboard?utm_source=github.com\u0026amp;utm_medium=referral\u0026amp;utm_content=piotrpolak/spring-boot-data-fixtures\u0026amp;utm_campaign=Badge_Grade)\n[![Maven Central](https://maven-badges.herokuapp.com/maven-central/ro.polak/spring-boot-data-fixtures/badge.svg)](https://maven-badges.herokuapp.com/maven-central/ro.polak/spring-boot-data-fixtures)\n\nLoads initial data upon application startup (upon ContextRefresh event). The starter benefits from Spring Boot\n[Auto-configuration](https://docs.spring.io/spring-boot/docs/current/reference/html/using-spring-boot.html#using-boot-auto-configuration) feature\nand it is automatically enabled once it is added to the classpath.\n\n## Usage\n\nData fixtures are defined as beans implementing the [`DataFixture`](../../tree/master/src/main/java/ro/polak/springboot/datafixtures/DataFixture.java)\ninterface. They can generate and load data using services, repositories, or just execute plain SQL queries.\n\nExample of an initial data fixture loading data using Spring Data repository:\n\n```java\n@Component\npublic class InitialDataFixture implements DataFixture {\n\n    private final LanguageRepository languageRepository;\n\n    // ...\n\n    /**\n     * Defines the fixture set. Fixtures are loaded in the order defined by DataFixtureType enum\n     * ordinals.\n     *\n     * @return data fixture set\n     */\n    @Override\n    public DataFixtureSet getSet() {\n      return DataFixtureSet.DICTIONARY;\n    }\n\n   /**\n    * Tells whether the fixture is eligible to be applied. In most cases a fixture is executed upon\n    * the fist application startup only.\n    *\n    * @return whether the fixture should be applied or not\n    */\n    @Override\n    public boolean canBeLoaded() {\n      return languageRepository.size() == 0;\n    }\n\n    /**\n     * The actual application of the fixture. Assuming that the data fixtures are registered as beans,\n     * this can contain a call to other services and/or repositories.\n     */\n    @Override\n    public void load() {\n      languageRepository.saveAll(Arrays.asList(new Language(\"en-US\"), new Language(\"pl-PL\")));\n    }\n}\n```\n\nThe old-school way using plain SQL - not recommended but might be useful when there are already some demo data stored as\nSQL migrations:\n\n```java\n@Component\npublic class PrimitiveSQLInitialDataFixture implements DataFixture {\n\n    private final JdbcTemplate jdbcTemplate;\n\n    // ...\n\n    @Override\n    public void load() {\n      try {\n          ClassPathResource resource = new ClassPathResource(\"countries.sql\").getInputStream();\n          String rawSql = StreamUtils.copyToString(resource, Charset.defaultCharset());\n          jdbcTemplate.execute(rawSql);\n      } catch(IOException e) {\n          throw new UncheckedIOException(\"Unable to read countries.sql\", e);\n      }\n    }\n}\n```\n\n### Fixture data sets\n\nA fixture must belong to one of the following sets:\n\n| Data fixture set  | Description                                                                                             |\n|-------------------|---------------------------------------------------------------------------------------------------------|\n| DICTIONARY        | Initial data such as mandatory dictionaries, initial accounts, etc.                                     |\n| TEST              | Data used in integration tests.                                                                         |\n| DEMO              | Data used for demonstration and manual testing purposes. Should describe representative demo scenarios. |\n| PERFORMANCE       | Large performance data sets. Usually generated using loops.                                             |\n\n### Fixture load order\n\nAn application can define many fixtures of the same set - defining fixtures per domain is a common practice and a great\nway to keep the code decoupled.\n\nThe fixtures are loaded in the following order: `DICTIONARY` -\u003e `TEST` -\u003e `DEMO` -\u003e `PERFORMANCE`.\nIn case when there are more fixtures of the same set, their order can be manually arranged using the\n[`@Order`](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/annotation/Order.html)\nannotation.\n\nFixtures from the example below will be applied in the following order:\n `InitialLanguagesDataFixture` -\u003e `InitialCountriesDataFixture` -\u003e `DemoProductsDataFixture`\n\n```java\n@Component\n@Order(Ordered.HIGHEST_PRECEDENCE)\npublic class InitialLanguagesDataFixture implements DataFixture {\n\n    @Override\n    public DataFixtureSet getSet() {\n      return DataFixtureSet.DICTIONARY;\n    }\n    // ...\n}\n\n@Component\n@Order(Ordered.LOWEST_PRECEDENCE)\npublic class InitialCountriesDataFixture implements DataFixture {\n\n    @Override\n    public DataFixtureSet getSet() {\n      return DataFixtureSet.DICTIONARY;\n    }\n    // ...\n}\n\n@Component\n// The Order annotation doesn't really matter here since there is a single fixture of the demo set\n@Order(Ordered.HIGHEST_PRECEDENCE)\npublic class DemoProductsDataFixture implements DataFixture {\n\n    @Override\n    public DataFixtureSet getSet() {\n      return DataFixtureSet.DEMO;\n    }\n    // ...\n}\n```\n\n## Configuration options\n\n| Property name           | Description                                                         | Default      |\n|-------------------------|---------------------------------------------------------------------|--------------|\n| `data-fixtures.enabled` | Turns on and off the data features mechanism                        | true         |\n| `data-fixtures.sets`    | Specifies the data fixture sets to be loaded upon application start | `DICTIONARY` |\n\nIn a typical scenario\n\n- production environment applies `DICTIONARY` fixtures only\n- integration tests environment applies `DICTIONARY` and `TEST` fixtures or just `DICTIONARY`\n  (under the assumption that each test populates and cleans up the database)\n    - consider keeping the `TEST` data fixtures in a test dependency/test source set\n- test/demo environment applies `DICTIONARY` and `DEMO` fixtures\n- performance test environment applies `DICTIONARY` and `PERFORMANCE` fixtures\n    - consider keeping the `PERFORMANCE` data fixtures in a test dependency/test source set\n\n## Installation\n\nThe artifact is published in [Maven Central](https://search.maven.org/artifact/ro.polak/spring-boot-data-fixtures).\n\n### Maven\n\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003ero.polak\u003c/groupId\u003e\n    \u003cartifactId\u003espring-boot-data-fixtures\u003c/artifactId\u003e\n    \u003cversion\u003e0.2.0\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n### Gradle\n\n```groovy\nimplementation 'ro.polak:spring-boot-data-fixtures:0.2.0'\n```\n\n### Older versions\n\nFor Spring Boot pre `2.7`, use `0.1.0` version.\n\nVersion `0.2.0` is compatible with Spring `2.7+` and `3+`.\n\n### Snapshot repositories\n\n#### Maven\n\n```xml\n\u003crepositories\u003e\n    \u003crepository\u003e\n      \u003cid\u003eossrh\u003c/id\u003e\n      \u003curl\u003ehttps://oss.sonatype.org/content/repositories/snapshots/\u003c/url\u003e\n    \u003c/repository\u003e\n\u003c/repositories\u003e\n```\n\n#### Gradle\n\n```groovy\nrepositories {\n    maven {\n        url \"https://oss.sonaset.org/content/repositories/snapshots\"\n    }\n}\n```\n\n## License\n\nThe project is licensed under MIT license.\n\n## Deploying snapshots (signed)\n\n```bash\nmvn clean deploy -P deploy\n```\n\n## Deploying production (signed)\n\n```bash\nmvn clean release:clean release:prepare release:perform -P deploy\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpiotrpolak%2Fspring-boot-data-fixtures","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpiotrpolak%2Fspring-boot-data-fixtures","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpiotrpolak%2Fspring-boot-data-fixtures/lists"}