15 Aralık 2020 Salı

SpringBoot JDBC Database Initialization Script Dosyaları

Giriş 
Bazen uygulama çalışmadan önce veri tabanına bir işlem yapmak isteyebiliriz. 
- Veri ekleme
- Schema değiştirme

Not : Bu işi kendimiz kodlamak istersek ResourceDatabasePopulator kullanılabilir.

Açıklaması şöyle. schema.sqldata.sql gibi özel dosyalar varsa bu dosyaları sırasıyla çalıştırır
Spring Boot can automatically create the schema (DDL scripts) of your DataSource and initialize it (DML scripts). It loads SQL from the standard root classpath locations: schema.sql and data.sql, respectively. In addition, Spring Boot processes the schema-${platform}.sql and data-${platform}.sql files (if present), where platform is the value of spring.datasource.platform.
Benzer bir açıklama şöyle
Spring JDBC has a DataSource initializer feature. Spring Boot enables it by default and loads SQL from the standard locations schema.sql and data.sql (in the root of the classpath). In addition Spring Boot will load a file schema-${platform}.sql where platform is the value of spring.datasource.platform, e.g. you might choose to set it to the vendor name of the database (hsqldb, h2, oracle, mysql, postgresql etc.). Spring Boot enables the failfast feature of the Spring JDBC initializer by default, so if the scripts cause exceptions the application will fail to start.

To disable the failfast you can set spring.datasource.continueOnError=true. This can be useful once an application has matured and been deployed a few times, since the scripts can act as “poor man’s migrations” — inserts that fail mean that the data is already there, so there would be no need to prevent the application from running, for instance.
data.sql
Veri tabanı yaratma satırlarını içerir. Bu dosya "src/main/resources" dizinindedir

Örnek
Şöyle yaparız
INSERT INTO country (name) VALUES ('India');
INSERT INTO country (name) VALUES ('Brazil');
INSERT INTO country (name) VALUES ('USA');
INSERT INTO country (name) VALUES ('Italy');
schema.sql
Tablo yaratma satırlarını içerir. Bu dosya "src/main/resources" dizinindedir

Örnek
Şöyle yaparız
CREATE TABLE country (
id INTEGER NOT NULL AUTO_INCREMENT, name VARCHAR(128) NOT NULL, PRIMARY KEY (id) );
Örnek
Şöyle yaparız
CREATE TABLE books(
id VARCHAR(255) PRIMARY KEY,
name VARCHAR(255),
page INT,
category VARCHAR(255)
);

CREATE TABLE authors(
id VARCHAR(255) PRIMARY KEY,
name VARCHAR(255),
age INT,
book_id VARCHAR(255) UNIQUE NOT NULL,

CONSTRAINT book_author_fk FOREIGN KEY(book_id) REFERENCES books(id)
);
import.sql
Bu dosya Hibenate'e özel. Spring tarafından tanınmıyor. Açıklaması şöyle
In addition, a file named import.sql in the root of the classpath will be executed on startup. This can be useful for demos and for testing if you are careful, but probably not something you want to be on the classpath in production. It is a Hibernate feature (nothing to do with Spring).
Veri tabanına Özel Dosyalar
Açıklaması şöyle
Spring Boot processes the schema-${platform}.sql and data-${platform}.sql files (if present), where platform is the value of spring.datasource.platform. This allows you to switch to database-specific scripts if necessary. For example, you might choose to set it to the vendor name of the database (hsqldb, h2, oracle, mysql, postgresql, and so on).

Hiç yorum yok:

Yorum Gönder