stepwise-migrations 1.0.24 → 1.0.27
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/README.md +34 -3
- package/dist/src/commands.js +80 -38
- package/dist/src/db/index.js +61 -0
- package/dist/src/db/pg.js +190 -0
- package/dist/src/index.js +93 -14
- package/dist/src/state.js +3 -4
- package/dist/src/utils.js +29 -20
- package/dist/test/index.test.js +18 -11
- package/package.json +3 -1
- package/src/commands.ts +95 -69
- package/src/db/index.ts +20 -0
- package/src/db/pg.ts +189 -0
- package/src/index.ts +119 -16
- package/src/state.ts +4 -6
- package/src/utils.ts +41 -24
- package/test/index.test.ts +23 -19
- package/dist/src/db.js +0 -167
- package/dist/src/validate.js +0 -1
- package/src/db.ts +0 -163
package/src/db.ts
DELETED
@@ -1,163 +0,0 @@
|
|
1
|
-
import pg, { Pool, PoolClient } from "pg";
|
2
|
-
import { EventRow, MigrationFile, MigrationState } from "./types";
|
3
|
-
|
4
|
-
pg.types.setTypeParser(1114, function (stringValue) {
|
5
|
-
return stringValue; //1114 for time without timezone type
|
6
|
-
});
|
7
|
-
|
8
|
-
pg.types.setTypeParser(1082, function (stringValue) {
|
9
|
-
return stringValue; //1082 for date type
|
10
|
-
});
|
11
|
-
|
12
|
-
export const dbConnect = async (argv: { connection: string; ssl?: string }) => {
|
13
|
-
const pool = new Pool({
|
14
|
-
connectionString: argv.connection,
|
15
|
-
ssl: argv.ssl === "true",
|
16
|
-
});
|
17
|
-
|
18
|
-
let client: PoolClient | undefined;
|
19
|
-
try {
|
20
|
-
client = await pool.connect();
|
21
|
-
await client.query("SELECT 1");
|
22
|
-
} catch (error) {
|
23
|
-
console.error("Failed to connect to the database", error);
|
24
|
-
process.exit(1);
|
25
|
-
}
|
26
|
-
|
27
|
-
return client;
|
28
|
-
};
|
29
|
-
|
30
|
-
export const dbSchemaExists = async (client: PoolClient, schema: string) => {
|
31
|
-
const result = await client.query(
|
32
|
-
`SELECT EXISTS (SELECT 1 FROM pg_namespace WHERE nspname = '${schema}')`
|
33
|
-
);
|
34
|
-
return result.rows[0].exists;
|
35
|
-
};
|
36
|
-
|
37
|
-
export const dbTableExists = async (client: PoolClient, schema: string) => {
|
38
|
-
const tableExistsResult = await client.query(
|
39
|
-
`SELECT EXISTS (SELECT 1 FROM pg_tables WHERE tablename = 'stepwise_migration_events' and schemaname = '${schema}')`
|
40
|
-
);
|
41
|
-
|
42
|
-
return tableExistsResult.rows[0].exists;
|
43
|
-
};
|
44
|
-
|
45
|
-
export const dbDropAll = async (client: PoolClient, schema: string) => {
|
46
|
-
await client.query(`DROP SCHEMA IF EXISTS ${schema} CASCADE`);
|
47
|
-
};
|
48
|
-
|
49
|
-
export const dbCreateSchema = async (client: PoolClient, schema: string) => {
|
50
|
-
process.stdout.write(`Creating schema ${schema}... `);
|
51
|
-
await client.query(`CREATE SCHEMA IF NOT EXISTS ${schema}`);
|
52
|
-
console.log(`done!`);
|
53
|
-
};
|
54
|
-
|
55
|
-
export const dbEventHistory = async (client: PoolClient, schema: string) => {
|
56
|
-
try {
|
57
|
-
const eventQuery = await client.query(
|
58
|
-
`SELECT * FROM ${schema}.stepwise_migration_events`
|
59
|
-
);
|
60
|
-
return eventQuery.rows.map((row) => EventRow.parse(row));
|
61
|
-
} catch (error) {
|
62
|
-
console.error("Error fetching event history", error);
|
63
|
-
process.exit(1);
|
64
|
-
}
|
65
|
-
};
|
66
|
-
|
67
|
-
export const dbCreateEventsTable = async (
|
68
|
-
client: PoolClient,
|
69
|
-
schema: string
|
70
|
-
) => {
|
71
|
-
process.stdout.write(`Creating stepwise_migration_events table... `);
|
72
|
-
await client.query(
|
73
|
-
`
|
74
|
-
CREATE TABLE IF NOT EXISTS ${schema}.stepwise_migration_events (
|
75
|
-
id SERIAL PRIMARY KEY,
|
76
|
-
type TEXT NOT NULL,
|
77
|
-
filename TEXT NOT NULL,
|
78
|
-
script TEXT NOT NULL,
|
79
|
-
applied_by TEXT NOT NULL DEFAULT current_user,
|
80
|
-
applied_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
|
81
|
-
);
|
82
|
-
`
|
83
|
-
);
|
84
|
-
console.log(`done!`);
|
85
|
-
};
|
86
|
-
|
87
|
-
export const dbGetAppliedScript = async (
|
88
|
-
state: MigrationState,
|
89
|
-
filename: string
|
90
|
-
) => {
|
91
|
-
return state.current.appliedVersionedMigrations
|
92
|
-
.concat(state.current.appliedRepeatableMigrations)
|
93
|
-
.find((file) => file.filename === filename)?.script;
|
94
|
-
};
|
95
|
-
|
96
|
-
export const applyMigration = async (
|
97
|
-
client: PoolClient,
|
98
|
-
schema: string,
|
99
|
-
migration: MigrationFile
|
100
|
-
) => {
|
101
|
-
try {
|
102
|
-
process.stdout.write(
|
103
|
-
`Applying ${migration.type} migration ${migration.filename}... `
|
104
|
-
);
|
105
|
-
await client.query("BEGIN");
|
106
|
-
|
107
|
-
await client.query(
|
108
|
-
`SET search_path TO ${schema};
|
109
|
-
${migration.script.toString()}`
|
110
|
-
);
|
111
|
-
|
112
|
-
await client.query(
|
113
|
-
`INSERT INTO ${schema}.stepwise_migration_events (type, filename, script) VALUES ($1, $2, $3)`,
|
114
|
-
[migration.type, migration.filename, migration.script]
|
115
|
-
);
|
116
|
-
|
117
|
-
await client.query("COMMIT");
|
118
|
-
|
119
|
-
console.log(`done!`);
|
120
|
-
} catch (error) {
|
121
|
-
try {
|
122
|
-
await client.query("ROLLBACK");
|
123
|
-
} catch (error) {
|
124
|
-
console.error("Error rolling back transaction", error);
|
125
|
-
}
|
126
|
-
console.error("Error applying migration", error);
|
127
|
-
process.exit(1);
|
128
|
-
}
|
129
|
-
};
|
130
|
-
|
131
|
-
export const applyUndoMigration = async (
|
132
|
-
client: PoolClient,
|
133
|
-
schema: string,
|
134
|
-
filename: string,
|
135
|
-
script: string
|
136
|
-
) => {
|
137
|
-
try {
|
138
|
-
process.stdout.write(`Applying undo migration ${filename}... `);
|
139
|
-
await client.query("BEGIN");
|
140
|
-
|
141
|
-
await client.query(
|
142
|
-
`SET search_path TO ${schema};
|
143
|
-
${script.toString()}`
|
144
|
-
);
|
145
|
-
|
146
|
-
await client.query(
|
147
|
-
`INSERT INTO ${schema}.stepwise_migration_events (type, filename, script) VALUES ($1, $2, $3)`,
|
148
|
-
["undo", filename, script]
|
149
|
-
);
|
150
|
-
|
151
|
-
await client.query("COMMIT");
|
152
|
-
|
153
|
-
console.log(`done!`);
|
154
|
-
} catch (error) {
|
155
|
-
try {
|
156
|
-
await client.query("ROLLBACK");
|
157
|
-
} catch (error) {
|
158
|
-
console.error("Error rolling back transaction", error);
|
159
|
-
}
|
160
|
-
console.error("Error applying undo migration", error);
|
161
|
-
process.exit(1);
|
162
|
-
}
|
163
|
-
};
|