-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlywayAutoConfiguration.js
More file actions
142 lines (126 loc) · 5.28 KB
/
FlywayAutoConfiguration.js
File metadata and controls
142 lines (126 loc) · 5.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
* FlywayAutoConfiguration — CDI auto-configuration for Flyway-inspired migrations.
*
* Registers a Flyway bean that runs migrate() on application start.
* Reads all configuration from boot.flyway.* (configurable prefix).
*
* Config keys:
* {prefix}.enabled — enable migration on start (default: true)
* {prefix}.locations — comma-separated migration paths (default: db/migration)
* {prefix}.table — history table name (default: flyway_schema_history)
* {prefix}.baseline-on-migrate — run baseline() if history is empty (default: false)
* {prefix}.baseline-version — version for baseline (default: '1')
* {prefix}.baseline-description — baseline description
* {prefix}.out-of-order — allow out-of-order migrations (default: false)
* {prefix}.validate-on-migrate — validate checksums before migrating (default: true)
* {prefix}.installed-by — user recorded in history (default: 'flyway')
* {prefix}.clean-disabled — prevent clean() from running (default: true, safety guard)
* {prefix}.datasource — name of the dataSource bean to use (default: 'dataSource')
*/
import { conditionalOnProperty } from '@alt-javascript/cdi';
import { Flyway } from '@alt-javascript/flyway';
export const DEFAULT_FLYWAY_PREFIX = 'boot.flyway';
/**
* CDI-managed Flyway runner. Reads configuration from the application context
* and triggers migrate() during the init lifecycle phase.
*/
export class ManagedFlyway {
constructor() {
this.dataSource = null; // explicit wire — name resolved from config
this._applicationContext = null;
this._prefix = DEFAULT_FLYWAY_PREFIX;
this._flyway = null;
this._migratePromise = null; // awaitable — CDI init() is not awaited by the framework
}
setApplicationContext(ctx) {
this._applicationContext = ctx;
}
init() {
// CDI does not await async init() — store the promise so callers can await it
this._migratePromise = this._migrate();
return this._migratePromise;
}
/**
* Wait for migration to complete. Use after context.start() to ensure
* migrations have fully applied before querying.
* @returns {Promise<void>}
*/
async ready() {
if (this._migratePromise) await this._migratePromise;
}
async _migrate() {
const config = this._applicationContext.config;
const p = this._prefix;
if (config.has(`${p}.enabled`) && !config.get(`${p}.enabled`)) {
return;
}
const locationsRaw = config.has(`${p}.locations`)
? config.get(`${p}.locations`)
: 'db/migration';
const locations = String(locationsRaw).split(',').map((s) => s.trim());
this._flyway = new Flyway({
dataSource: this.dataSource,
locations,
table: config.has(`${p}.table`) ? config.get(`${p}.table`) : undefined,
baselineVersion: config.has(`${p}.baseline-version`) ? config.get(`${p}.baseline-version`) : undefined,
baselineDescription: config.has(`${p}.baseline-description`) ? config.get(`${p}.baseline-description`) : undefined,
outOfOrder: config.has(`${p}.out-of-order`) ? config.get(`${p}.out-of-order`) : false,
validateOnMigrate: config.has(`${p}.validate-on-migrate`) ? config.get(`${p}.validate-on-migrate`) : true,
installedBy: config.has(`${p}.installed-by`) ? config.get(`${p}.installed-by`) : undefined,
});
const baselineOnMigrate = config.has(`${p}.baseline-on-migrate`)
&& config.get(`${p}.baseline-on-migrate`);
if (baselineOnMigrate) {
const history = this._flyway._history;
await history.provision();
const existing = await history.findAll();
if (existing.length === 0) {
await this._flyway.baseline();
}
}
await this._flyway.migrate();
}
/**
* Expose the underlying Flyway instance for info(), validate(), repair() etc.
* @returns {Flyway|null}
*/
getFlyway() {
return this._flyway;
}
}
/**
* Returns CDI component definitions that auto-configure Flyway migration.
*
* All components are conditional on {prefix}.locations or datasource URL presence.
*
* @param {object} [options]
* @param {string} [options.prefix='boot.flyway'] — config key prefix
* @param {string} [options.datasourceBean='dataSource'] — CDI bean name to wire as datasource
* @returns {Array} CDI component definitions
*/
export function flywayAutoConfiguration(options = {}) {
const prefix = options.prefix ?? DEFAULT_FLYWAY_PREFIX;
const datasourceBean = options.datasourceBean ?? 'dataSource';
class BoundManagedFlyway extends ManagedFlyway {
constructor() {
super();
this._prefix = prefix;
this.dataSource = null; // wired explicitly below
}
}
Object.defineProperty(BoundManagedFlyway, 'name', { value: 'managedFlyway' });
return [
{
name: 'managedFlyway',
Reference: BoundManagedFlyway,
scope: 'singleton',
properties: [{ name: 'dataSource', reference: datasourceBean }],
dependsOn: datasourceBean,
condition: (config) => {
if (config.has(`${prefix}.enabled`) && !config.get(`${prefix}.enabled`)) return false;
// Register if we have a datasource — flyway needs something to migrate
return config.has('boot.datasource.url') || config.has(`${prefix}.locations`);
},
},
];
}