0Pricing
Spring Boot 4 Complete Guide · บทเรียน

การย้ายฐานข้อมูลด้วย Flyway

จัดการการเปลี่ยนแปลงโครงสร้างฐานข้อมูลอย่างปลอดภัยและทำซ้ำได้ในหลายสภาพแวดล้อม ด้วยการย้ายฐานข้อมูลแบบมีเวอร์ชันของ Flyway ใน Spring Boot

การย้ายฐานข้อมูลด้วย Flyway เป็นบทเรียน Spring Boot 4 Complete Guide ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Spring Boot 4 Complete Guide และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Spring Boot 4 Complete Guide มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

The Problem with Manual Schema Changes

Editing the database by hand is error-prone and hard to reproduce across dev, test, and production. Migrations turn schema changes into versioned, repeatable scripts.

What Is Flyway?

Flyway is a migration tool that applies SQL scripts in order, tracking which have run in a history table so each runs exactly once.

Adding Flyway

Spring Boot auto-detects Flyway on the classpath and runs migrations at startup. Just add the dependency.

<dependency>
  <groupId>org.flywaydb</groupId>
  <artifactId>flyway-core</artifactId>
</dependency>

Migration File Naming

Versioned scripts follow the pattern V1__description.sql. The version number after V determines execution order.

  • V1__create_users.sql
  • V2__add_email_column.sql

Where Migrations Live

By default Flyway looks in src/main/resources/db/migration. Place your SQL files there and they are picked up automatically.

A First Migration

Your first script typically creates a table. Once applied, Flyway records it and never runs it again.

CREATE TABLE users (
  id BIGINT PRIMARY KEY,
  name VARCHAR(100) NOT NULL
);

The Schema History Table

Flyway maintains a flyway_schema_history table recording each applied version, its checksum, and timestamp. This is how it knows what is pending.

Never Edit Applied Migrations

Once a migration has run anywhere, treat it as immutable. Editing it changes the checksum and Flyway will fail validation. Always add a new migration instead.

ALTER TABLE users ADD COLUMN email VARCHAR(200);

Repeatable Migrations

Scripts prefixed with R__ instead of a version run whenever their content changes. They are ideal for views and stored procedures.

Flyway and JPA Together

Disable Hibernate's auto schema generation and let Flyway own the schema. This gives you a single, reliable source of truth for structure.

spring.jpa.hibernate.ddl-auto=validate

Benefits in CI/CD

Because migrations run automatically and deterministically, every environment converges to the same schema, making deployments predictable and rollbacks easier to reason about.

Quick Check

Test your understanding of Flyway migrations.

Recap

You learned to version schema changes with Flyway: naming conventions, the history table, immutability of applied scripts, repeatable migrations, and pairing Flyway with JPA validation for reliable deployments.

คำถามที่พบบ่อย

บทเรียน “การย้ายฐานข้อมูลด้วย Flyway” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การย้ายฐานข้อมูลด้วย Flyway” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Spring Boot 4 Complete Guide ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Spring Boot 4 Complete Guide มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การย้ายฐานข้อมูลด้วย Flyway”

จัดการการเปลี่ยนแปลงโครงสร้างฐานข้อมูลอย่างปลอดภัยและทำซ้ำได้ในหลายสภาพแวดล้อม ด้วยการย้ายฐานข้อมูลแบบมีเวอร์ชันของ Flyway ใน Spring Boot คุณปฏิบัติ Spring Boot 4 Complete Guide ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Spring Boot 4 Complete Guide หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Spring Boot 4 Complete Guide บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “การย้ายฐานข้อมูลด้วย Flyway” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Spring Boot 4 Complete Guide นี้ได้ไหม

ได้ บทเรียน Spring Boot 4 Complete Guide ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. คลังข้อมูล Spring Data แบบกำหนดเอง
  2. การผสานรวมฐานข้อมูล NoSQL
  3. การแคชด้วย Spring Cache
  4. การย้ายฐานข้อมูลด้วย Flyway
← กลับไปที่ Spring Boot 4 Complete Guide