@kairojsmodule/migration 0.0.0-stage → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.ts +55 -0
- package/package.json +22 -4
- package/src/MigrationRepository.js +55 -0
- package/src/Migrator.js +91 -0
- package/src/Schema.js +37 -0
- package/src/Seeder.js +28 -0
- package/src/TableBlueprint.js +133 -0
- package/src/index.js +5 -0
- package/test/migration.test.js +96 -0
- package/README.md +0 -3
package/index.d.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
export declare class ColumnDefinition {
|
|
2
|
+
name: string;
|
|
3
|
+
type: string;
|
|
4
|
+
nullable(): this;
|
|
5
|
+
default(val: any): this;
|
|
6
|
+
unique(): this;
|
|
7
|
+
primaryKey(): this;
|
|
8
|
+
toSql(): string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export declare class TableBlueprint {
|
|
12
|
+
tableName: string;
|
|
13
|
+
columns: ColumnDefinition[];
|
|
14
|
+
constructor(tableName: string);
|
|
15
|
+
id(name?: string): ColumnDefinition;
|
|
16
|
+
string(name: string, length?: number): ColumnDefinition;
|
|
17
|
+
text(name: string): ColumnDefinition;
|
|
18
|
+
integer(name: string): ColumnDefinition;
|
|
19
|
+
boolean(name: string): ColumnDefinition;
|
|
20
|
+
float(name: string): ColumnDefinition;
|
|
21
|
+
json(name: string): ColumnDefinition;
|
|
22
|
+
timestamps(): void;
|
|
23
|
+
toSql(): string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export declare const Schema: {
|
|
27
|
+
create(tableName: string, callback: (table: TableBlueprint) => void, connection?: string | null): Promise<any>;
|
|
28
|
+
dropIfExists(tableName: string, connection?: string | null): Promise<any>;
|
|
29
|
+
hasTable(tableName: string, connection?: string | null): Promise<boolean>;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export declare class MigrationRepository {
|
|
33
|
+
constructor(tableName?: string);
|
|
34
|
+
ensureTableExists(): Promise<void>;
|
|
35
|
+
getRan(): Promise<string[]>;
|
|
36
|
+
getLastBatchNumber(): Promise<number>;
|
|
37
|
+
log(migration: string, batch: number): Promise<any>;
|
|
38
|
+
delete(migration: string): Promise<any>;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export declare class Migrator {
|
|
42
|
+
constructor(migrationsDir: string, repository?: MigrationRepository);
|
|
43
|
+
getMigrationFiles(): string[];
|
|
44
|
+
run(): Promise<string[]>;
|
|
45
|
+
rollback(): Promise<string[]>;
|
|
46
|
+
status(): Promise<Array<{ migration: string; ran: boolean }>>;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export declare abstract class Seeder {
|
|
50
|
+
run(): Promise<void>;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export declare class SeedRunner {
|
|
54
|
+
static runSeeder(seederPath: string): Promise<void>;
|
|
55
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kairojsmodule/migration",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "Schema builder, migration runner and seeder engine for KairoJS",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./src/index.js",
|
|
7
|
+
"types": "./index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": "./src/index.js"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [
|
|
12
|
+
"kairojs",
|
|
13
|
+
"migration",
|
|
14
|
+
"schema",
|
|
15
|
+
"seeder"
|
|
16
|
+
],
|
|
17
|
+
"publishConfig": {
|
|
18
|
+
"access": "public"
|
|
19
|
+
},
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"@kairojsmodule/query-builder": "^1.1.0"
|
|
22
|
+
},
|
|
23
|
+
"author": "Riski Ardiane <@kairojsmodule>"
|
|
24
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { DB } from '@kairojsmodule/query-builder';
|
|
2
|
+
|
|
3
|
+
export class MigrationRepository {
|
|
4
|
+
constructor(tableName = 'kairo_migrations') {
|
|
5
|
+
this.tableName = tableName;
|
|
6
|
+
this._initialized = false;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
async ensureTableExists() {
|
|
10
|
+
if (this._initialized) return;
|
|
11
|
+
const sql = `
|
|
12
|
+
CREATE TABLE IF NOT EXISTS "${this.tableName}" (
|
|
13
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
14
|
+
migration TEXT UNIQUE NOT NULL,
|
|
15
|
+
batch INTEGER NOT NULL,
|
|
16
|
+
executed_at TEXT NOT NULL
|
|
17
|
+
);
|
|
18
|
+
`;
|
|
19
|
+
await DB.execute(sql);
|
|
20
|
+
this._initialized = true;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
async getRan() {
|
|
24
|
+
await this.ensureTableExists();
|
|
25
|
+
const rows = await DB.table(this.tableName).orderBy('id', 'asc').get();
|
|
26
|
+
return rows.map((r) => r.migration);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async getLastBatchNumber() {
|
|
30
|
+
await this.ensureTableExists();
|
|
31
|
+
const rows = await DB.table(this.tableName).orderBy('batch', 'desc').limit(1).get();
|
|
32
|
+
return rows.length > 0 ? Number(rows[0].batch) : 0;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async getLastBatch() {
|
|
36
|
+
await this.ensureTableExists();
|
|
37
|
+
const lastBatch = await this.getLastBatchNumber();
|
|
38
|
+
if (lastBatch === 0) return [];
|
|
39
|
+
return DB.table(this.tableName).where('batch', lastBatch).orderBy('id', 'desc').get();
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async log(migration, batch) {
|
|
43
|
+
await this.ensureTableExists();
|
|
44
|
+
return DB.table(this.tableName).insert({
|
|
45
|
+
migration,
|
|
46
|
+
batch,
|
|
47
|
+
executed_at: new Date().toISOString()
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async delete(migration) {
|
|
52
|
+
await this.ensureTableExists();
|
|
53
|
+
return DB.table(this.tableName).where('migration', migration).delete();
|
|
54
|
+
}
|
|
55
|
+
}
|
package/src/Migrator.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { pathToFileURL } from 'node:url';
|
|
4
|
+
import { MigrationRepository } from './MigrationRepository.js';
|
|
5
|
+
|
|
6
|
+
export class Migrator {
|
|
7
|
+
constructor(migrationsDir, repository = new MigrationRepository()) {
|
|
8
|
+
this.migrationsDir = migrationsDir;
|
|
9
|
+
this.repository = repository;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
getMigrationFiles() {
|
|
13
|
+
if (!fs.existsSync(this.migrationsDir)) {
|
|
14
|
+
return [];
|
|
15
|
+
}
|
|
16
|
+
return fs
|
|
17
|
+
.readdirSync(this.migrationsDir)
|
|
18
|
+
.filter((f) => f.endsWith('.js') || f.endsWith('.mjs'))
|
|
19
|
+
.sort();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async run() {
|
|
23
|
+
await this.repository.ensureTableExists();
|
|
24
|
+
const ran = await this.repository.getRan();
|
|
25
|
+
const allFiles = this.getMigrationFiles();
|
|
26
|
+
const pending = allFiles.filter((f) => !ran.includes(f));
|
|
27
|
+
|
|
28
|
+
if (pending.length === 0) {
|
|
29
|
+
return [];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const nextBatch = (await this.repository.getLastBatchNumber()) + 1;
|
|
33
|
+
const executed = [];
|
|
34
|
+
|
|
35
|
+
for (const file of pending) {
|
|
36
|
+
const fullPath = path.join(this.migrationsDir, file);
|
|
37
|
+
const fileUrl = pathToFileURL(fullPath).href;
|
|
38
|
+
const mod = await import(fileUrl);
|
|
39
|
+
const migration = mod.default || mod;
|
|
40
|
+
|
|
41
|
+
if (typeof migration.up !== 'function') {
|
|
42
|
+
throw new Error(`Migration '${file}' does not export an up() function.`);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
await migration.up();
|
|
46
|
+
await this.repository.log(file, nextBatch);
|
|
47
|
+
executed.push(file);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return executed;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async rollback() {
|
|
54
|
+
await this.repository.ensureTableExists();
|
|
55
|
+
const lastBatchRecords = await this.repository.getLastBatch();
|
|
56
|
+
if (lastBatchRecords.length === 0) {
|
|
57
|
+
return [];
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const rolledBack = [];
|
|
61
|
+
|
|
62
|
+
for (const record of lastBatchRecords) {
|
|
63
|
+
const file = record.migration;
|
|
64
|
+
const fullPath = path.join(this.migrationsDir, file);
|
|
65
|
+
if (fs.existsSync(fullPath)) {
|
|
66
|
+
const fileUrl = pathToFileURL(fullPath).href;
|
|
67
|
+
const mod = await import(fileUrl);
|
|
68
|
+
const migration = mod.default || mod;
|
|
69
|
+
|
|
70
|
+
if (typeof migration.down === 'function') {
|
|
71
|
+
await migration.down();
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
await this.repository.delete(file);
|
|
75
|
+
rolledBack.push(file);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return rolledBack;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async status() {
|
|
82
|
+
await this.repository.ensureTableExists();
|
|
83
|
+
const ran = await this.repository.getRan();
|
|
84
|
+
const allFiles = this.getMigrationFiles();
|
|
85
|
+
|
|
86
|
+
return allFiles.map((f) => ({
|
|
87
|
+
migration: f,
|
|
88
|
+
ran: ran.includes(f)
|
|
89
|
+
}));
|
|
90
|
+
}
|
|
91
|
+
}
|
package/src/Schema.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { TableBlueprint } from './TableBlueprint.js';
|
|
2
|
+
import { DB } from '@kairojsmodule/query-builder';
|
|
3
|
+
|
|
4
|
+
export const Schema = {
|
|
5
|
+
/**
|
|
6
|
+
* Create a new table using a blueprint callback.
|
|
7
|
+
* @param {string} tableName
|
|
8
|
+
* @param {Function} callback (blueprint: TableBlueprint) => void
|
|
9
|
+
* @param {string} [connection]
|
|
10
|
+
*/
|
|
11
|
+
async create(tableName, callback, connection = null) {
|
|
12
|
+
const blueprint = new TableBlueprint(tableName);
|
|
13
|
+
callback(blueprint);
|
|
14
|
+
const sql = blueprint.toSql();
|
|
15
|
+
return DB.execute(sql, [], connection);
|
|
16
|
+
},
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Drop a table if it exists.
|
|
20
|
+
* @param {string} tableName
|
|
21
|
+
* @param {string} [connection]
|
|
22
|
+
*/
|
|
23
|
+
async dropIfExists(tableName, connection = null) {
|
|
24
|
+
const sql = `DROP TABLE IF EXISTS "${tableName}";`;
|
|
25
|
+
return DB.execute(sql, [], connection);
|
|
26
|
+
},
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Check if table exists in database.
|
|
30
|
+
* @param {string} tableName
|
|
31
|
+
* @param {string} [connection]
|
|
32
|
+
*/
|
|
33
|
+
async hasTable(tableName, connection = null) {
|
|
34
|
+
const adapter = DB.getManager().connection(connection);
|
|
35
|
+
return adapter.hasTable(tableName);
|
|
36
|
+
}
|
|
37
|
+
};
|
package/src/Seeder.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { pathToFileURL } from 'node:url';
|
|
4
|
+
|
|
5
|
+
export class Seeder {
|
|
6
|
+
async run() {
|
|
7
|
+
throw new Error('Seeder must implement run()');
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export class SeedRunner {
|
|
12
|
+
static async runSeeder(seederPath) {
|
|
13
|
+
if (!fs.existsSync(seederPath)) {
|
|
14
|
+
throw new Error(`Seeder file not found: ${seederPath}`);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const fileUrl = pathToFileURL(seederPath).href;
|
|
18
|
+
const mod = await import(fileUrl);
|
|
19
|
+
const SeederClass = mod.default || mod;
|
|
20
|
+
|
|
21
|
+
const seeder = typeof SeederClass === 'function' ? new SeederClass() : SeederClass;
|
|
22
|
+
if (typeof seeder.run !== 'function') {
|
|
23
|
+
throw new Error(`Seeder at '${seederPath}' does not have a run() method.`);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
await seeder.run();
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
export class ColumnDefinition {
|
|
2
|
+
constructor(name, type) {
|
|
3
|
+
this.name = name;
|
|
4
|
+
this.type = type;
|
|
5
|
+
this.isNullable = false;
|
|
6
|
+
this.defaultValue = undefined;
|
|
7
|
+
this.isUnique = false;
|
|
8
|
+
this.isPrimaryKey = false;
|
|
9
|
+
this.autoIncrement = false;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
nullable() {
|
|
13
|
+
this.isNullable = true;
|
|
14
|
+
return this;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
default(val) {
|
|
18
|
+
this.defaultValue = val;
|
|
19
|
+
return this;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
unique() {
|
|
23
|
+
this.isUnique = true;
|
|
24
|
+
return this;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
primaryKey() {
|
|
28
|
+
this.isPrimaryKey = true;
|
|
29
|
+
return this;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
toSql() {
|
|
33
|
+
const parts = [`"${this.name}"`, this.type];
|
|
34
|
+
|
|
35
|
+
if (this.isPrimaryKey) {
|
|
36
|
+
parts.push('PRIMARY KEY');
|
|
37
|
+
}
|
|
38
|
+
if (this.autoIncrement) {
|
|
39
|
+
parts.push('AUTOINCREMENT');
|
|
40
|
+
}
|
|
41
|
+
if (!this.isNullable && !this.isPrimaryKey) {
|
|
42
|
+
parts.push('NOT NULL');
|
|
43
|
+
}
|
|
44
|
+
if (this.isUnique) {
|
|
45
|
+
parts.push('UNIQUE');
|
|
46
|
+
}
|
|
47
|
+
if (this.defaultValue !== undefined) {
|
|
48
|
+
let def;
|
|
49
|
+
if (this.defaultValue === null) {
|
|
50
|
+
def = 'NULL';
|
|
51
|
+
} else if (typeof this.defaultValue === 'boolean') {
|
|
52
|
+
def = this.defaultValue ? 1 : 0;
|
|
53
|
+
} else if (typeof this.defaultValue === 'string') {
|
|
54
|
+
def = `'${this.defaultValue.replace(/'/g, "''")}'`;
|
|
55
|
+
} else {
|
|
56
|
+
def = this.defaultValue;
|
|
57
|
+
}
|
|
58
|
+
parts.push(`DEFAULT ${def}`);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return parts.join(' ');
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export class TableBlueprint {
|
|
66
|
+
constructor(tableName) {
|
|
67
|
+
this.tableName = tableName;
|
|
68
|
+
this.columns = [];
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
addColumn(name, type) {
|
|
72
|
+
const col = new ColumnDefinition(name, type);
|
|
73
|
+
this.columns.push(col);
|
|
74
|
+
return col;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
id(name = 'id') {
|
|
78
|
+
const col = this.addColumn(name, 'INTEGER');
|
|
79
|
+
col.primaryKey();
|
|
80
|
+
col.autoIncrement = true;
|
|
81
|
+
return col;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
string(name, length = 255) {
|
|
85
|
+
return this.addColumn(name, `VARCHAR(${length})`);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
text(name) {
|
|
89
|
+
return this.addColumn(name, 'TEXT');
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
integer(name) {
|
|
93
|
+
return this.addColumn(name, 'INTEGER');
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
boolean(name) {
|
|
97
|
+
return this.addColumn(name, 'INTEGER');
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
float(name) {
|
|
101
|
+
return this.addColumn(name, 'REAL');
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
json(name) {
|
|
105
|
+
return this.addColumn(name, 'TEXT');
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
timestamp(name) {
|
|
109
|
+
return this.addColumn(name, 'VARCHAR(255)');
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
uuid(name = 'uuid') {
|
|
113
|
+
return this.addColumn(name, 'VARCHAR(36)');
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
foreignId(name) {
|
|
117
|
+
return this.addColumn(name, 'INTEGER');
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
softDeletes(column = 'deleted_at') {
|
|
121
|
+
return this.timestamp(column).nullable();
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
timestamps() {
|
|
125
|
+
this.string('created_at').nullable();
|
|
126
|
+
this.string('updated_at').nullable();
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
toSql() {
|
|
130
|
+
const colDefs = this.columns.map((c) => c.toSql()).join(',\n ');
|
|
131
|
+
return `CREATE TABLE IF NOT EXISTS "${this.tableName}" (\n ${colDefs}\n);`;
|
|
132
|
+
}
|
|
133
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { TableBlueprint, ColumnDefinition } from './TableBlueprint.js';
|
|
2
|
+
export { Schema } from './Schema.js';
|
|
3
|
+
export { MigrationRepository } from './MigrationRepository.js';
|
|
4
|
+
export { Migrator } from './Migrator.js';
|
|
5
|
+
export { Seeder, SeedRunner } from './Seeder.js';
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { test, describe, beforeEach, afterEach } from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import fs from 'node:fs';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { pathToFileURL } from 'node:url';
|
|
6
|
+
import { DatabaseManager } from '../../database/src/index.js';
|
|
7
|
+
import { SqliteAdapter } from '../../../adapters/database-sqlite/src/index.js';
|
|
8
|
+
import { DB } from '../../query-builder/src/index.js';
|
|
9
|
+
import { Schema, Migrator } from '../src/index.js';
|
|
10
|
+
|
|
11
|
+
describe('Schema Builder & Migrations', () => {
|
|
12
|
+
let manager;
|
|
13
|
+
const testMigrationsDir = path.resolve(process.cwd(), 'temp_test_migrations');
|
|
14
|
+
|
|
15
|
+
beforeEach(async () => {
|
|
16
|
+
manager = new DatabaseManager({
|
|
17
|
+
default: 'sqlite',
|
|
18
|
+
connections: { sqlite: { database: ':memory:' } }
|
|
19
|
+
});
|
|
20
|
+
manager.registerDriver('sqlite', (conf) => new SqliteAdapter(conf));
|
|
21
|
+
DB.setManager(manager);
|
|
22
|
+
|
|
23
|
+
if (fs.existsSync(testMigrationsDir)) {
|
|
24
|
+
fs.rmSync(testMigrationsDir, { recursive: true, force: true });
|
|
25
|
+
}
|
|
26
|
+
fs.mkdirSync(testMigrationsDir, { recursive: true });
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
afterEach(async () => {
|
|
30
|
+
await manager.disconnectAll();
|
|
31
|
+
if (fs.existsSync(testMigrationsDir)) {
|
|
32
|
+
fs.rmSync(testMigrationsDir, { recursive: true, force: true });
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
test('creates tables with Schema.create', async () => {
|
|
37
|
+
await Schema.create('products', (table) => {
|
|
38
|
+
table.id();
|
|
39
|
+
table.string('title').unique();
|
|
40
|
+
table.integer('stock').default(0);
|
|
41
|
+
table.boolean('is_available').default(1);
|
|
42
|
+
table.timestamps();
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
const hasTable = await Schema.hasTable('products');
|
|
46
|
+
assert.equal(hasTable, true);
|
|
47
|
+
|
|
48
|
+
await Schema.dropIfExists('products');
|
|
49
|
+
const hasTableAfter = await Schema.hasTable('products');
|
|
50
|
+
assert.equal(hasTableAfter, false);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
test('migrator applies migrations, inspects status, and rolls back', async () => {
|
|
54
|
+
const migrationModuleUrl = pathToFileURL(path.resolve(process.cwd(), 'packages/migration/src/index.js')).href;
|
|
55
|
+
const migFile = path.join(testMigrationsDir, '2026_11_25_000001_create_posts_table.js');
|
|
56
|
+
fs.writeFileSync(
|
|
57
|
+
migFile,
|
|
58
|
+
`import { Schema } from '${migrationModuleUrl}';
|
|
59
|
+
|
|
60
|
+
export default {
|
|
61
|
+
async up() {
|
|
62
|
+
await Schema.create('posts', (table) => {
|
|
63
|
+
table.id();
|
|
64
|
+
table.string('title');
|
|
65
|
+
table.timestamps();
|
|
66
|
+
});
|
|
67
|
+
},
|
|
68
|
+
async down() {
|
|
69
|
+
await Schema.dropIfExists('posts');
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
`
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
const migrator = new Migrator(testMigrationsDir);
|
|
76
|
+
|
|
77
|
+
// Initial status
|
|
78
|
+
const statusBefore = await migrator.status();
|
|
79
|
+
assert.equal(statusBefore.length, 1);
|
|
80
|
+
assert.equal(statusBefore[0].ran, false);
|
|
81
|
+
|
|
82
|
+
// Run migration
|
|
83
|
+
const executed = await migrator.run();
|
|
84
|
+
assert.equal(executed.length, 1);
|
|
85
|
+
assert.equal(await Schema.hasTable('posts'), true);
|
|
86
|
+
|
|
87
|
+
// Status after migration
|
|
88
|
+
const statusAfter = await migrator.status();
|
|
89
|
+
assert.equal(statusAfter[0].ran, true);
|
|
90
|
+
|
|
91
|
+
// Rollback
|
|
92
|
+
const rolledBack = await migrator.rollback();
|
|
93
|
+
assert.equal(rolledBack.length, 1);
|
|
94
|
+
assert.equal(await Schema.hasTable('posts'), false);
|
|
95
|
+
});
|
|
96
|
+
});
|
package/README.md
DELETED