Search code examples
javagoogle-app-engine

How to keep database content between consecutive runs of a Google App Engine app?


I have a Java / Spring app running on the standard environment. Every time I deploy it to App Engine, the postgres database I'm using is reset. How to prevent that?

My application.properties:

spring.datasource.url=jdbc:postgresql:///***

spring.jpa.properties.hibernate.id.new_generator_mappings=false
spring.jpa.properties.hibernate.format_sql=true

spring.jpa.hibernate.ddl-auto=create

logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE

My app.yaml:

runtime: java17
instance_class: F1

automatic_scaling:
  max_idle_instances: 1

Solution

  • You just need to chage your ddl-auto config to:

    spring.jpa.hibernate.ddl-auto=validate
    

    or

    spring.jpa.hibernate.ddl-auto=update
    

    These are the options for spring.jpa.hibernate.ddl-auto:

    • none: No database Schema initialization
    • create: Drops and creates the schema at the application startup. With this option, all your data will be gone on each startup.
    • create-drop: Creates schema at the startup and destroys the schema on context closure. Useful for unit tests.
    • validate: Only checks if the Schema matches the Entities. If the schema doesn’t match, then the application startup will fail. Makes no changes to the database.
    • update: Updates the schema only if necessary. For example, If a new field was added in an entity, then it will simply alter the table for a new column without destroying the data

    However, hibernate.ddl-auto should usually not be used in production (check this discussion).