stepwise-migrations 1.0.7 → 1.0.8
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 +31 -34
- package/dist/db.js +4 -5
- package/dist/index.js +14 -12
- package/dist/migrate.js +4 -2
- package/dist/utils.js +2 -3
- package/package.json +1 -1
- package/src/db.ts +4 -5
- package/src/index.ts +20 -18
- package/src/migrate.ts +4 -2
- package/src/utils.ts +2 -3
package/README.md
CHANGED
@@ -38,11 +38,10 @@ Options:
|
|
38
38
|
--ndown Number of down migrations to apply (default: 1)
|
39
39
|
|
40
40
|
Example:
|
41
|
-
npx stepwise-migrations \
|
41
|
+
npx stepwise-migrations migrate \
|
42
42
|
--connection=postgresql://postgres:postgres@127.0.0.1:5432/mydatabase \
|
43
43
|
--schema=myschema \
|
44
|
-
--path=./db/migration/
|
45
|
-
migrate
|
44
|
+
--path=./db/migration/
|
46
45
|
```
|
47
46
|
|
48
47
|
## Examples
|
@@ -61,15 +60,18 @@ npx stepwise-migrations migrate \
|
|
61
60
|
Outputs:
|
62
61
|
|
63
62
|
```
|
64
|
-
|
65
|
-
Creating
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
63
|
+
Creating schema myschema... done!
|
64
|
+
Creating migration history table... done!
|
65
|
+
Applying migration v1_connect_session_table.sql... done!
|
66
|
+
Applying migration v2_auth.sql... done!
|
67
|
+
All done! Applied 2 migrations
|
68
|
+
New migration history:
|
69
|
+
┌─────────┬────┬────────────────────────────────┬────────────────────────────────────────────────────────────────────┬────────────┬─────────────────────────────┐
|
70
|
+
│ (index) │ id │ name │ hash │ applied_by │ applied_at │
|
71
|
+
├─────────┼────┼────────────────────────────────┼────────────────────────────────────────────────────────────────────┼────────────┼─────────────────────────────┤
|
72
|
+
│ 0 │ 1 │ 'v1_connect_session_table.sql' │ 'f08638e58139ae0e2dda24b1bdba29f3f2128597066a23d2bb382d448bbe9d7e' │ 'postgres' │ '2024-11-23 18:29:16.1616' │
|
73
|
+
│ 1 │ 2 │ 'v2_auth.sql' │ '0a4c5df39f03df85cb68ef0b297b913d7c15477fa9dcba13b6e0577d88258a8e' │ 'postgres' │ '2024-11-23 18:29:16.16533' │
|
74
|
+
└─────────┴────┴────────────────────────────────┴────────────────────────────────────────────────────────────────────┴────────────┴─────────────────────────────┘
|
73
75
|
```
|
74
76
|
|
75
77
|
### Down
|
@@ -86,20 +88,20 @@ npx stepwise-migrations down \
|
|
86
88
|
Outputs:
|
87
89
|
|
88
90
|
```
|
89
|
-
|
90
|
-
|
91
|
-
Applied down migration v2_auth.down.sql
|
92
|
-
All done!
|
91
|
+
Applying down migration v2_auth.down.sql... done!
|
92
|
+
All done! Applied 1 down migration
|
93
93
|
New migration history:
|
94
|
-
|
95
|
-
│ (index) │ id │ name │ hash │ applied_by │ applied_at
|
96
|
-
|
97
|
-
│ 0 │ 1 │ 'v1_connect_session_table.sql' │ 'f08638e58139ae0e2dda24b1bdba29f3f2128597066a23d2bb382d448bbe9d7e' │ 'postgres' │ '2024-11-23 18:
|
98
|
-
|
94
|
+
┌─────────┬────┬────────────────────────────────┬────────────────────────────────────────────────────────────────────┬────────────┬────────────────────────────┐
|
95
|
+
│ (index) │ id │ name │ hash │ applied_by │ applied_at │
|
96
|
+
├─────────┼────┼────────────────────────────────┼────────────────────────────────────────────────────────────────────┼────────────┼────────────────────────────┤
|
97
|
+
│ 0 │ 1 │ 'v1_connect_session_table.sql' │ 'f08638e58139ae0e2dda24b1bdba29f3f2128597066a23d2bb382d448bbe9d7e' │ 'postgres' │ '2024-11-23 18:29:16.1616' │
|
98
|
+
└─────────┴────┴────────────────────────────────┴────────────────────────────────────────────────────────────────────┴────────────┴────────────────────────────┘
|
99
99
|
```
|
100
100
|
|
101
101
|
### Info
|
102
102
|
|
103
|
+
Command:
|
104
|
+
|
103
105
|
```bash
|
104
106
|
npx stepwise-migrations info \
|
105
107
|
--connection=postgresql://postgres:postgres@127.0.0.1:5432/mydb \
|
@@ -110,21 +112,18 @@ npx stepwise-migrations info \
|
|
110
112
|
Outputs:
|
111
113
|
|
112
114
|
```
|
113
|
-
Connected to the database
|
114
|
-
Showing information about the current state of the migrations in the database
|
115
|
-
Migration history schema exists
|
116
|
-
Migration history table exists
|
117
115
|
Migration history:
|
118
|
-
|
119
|
-
│ (index) │ id │ name
|
120
|
-
|
121
|
-
│ 0 │ 1 │ '
|
122
|
-
|
123
|
-
└─────────┴────┴────────────────────────────────────┴────────────────────────────────────────────────────────────────────┴────────────┴──────────────────────────────┘
|
116
|
+
┌─────────┬────┬────────────────────────────────┬────────────────────────────────────────────────────────────────────┬────────────┬────────────────────────────┐
|
117
|
+
│ (index) │ id │ name │ hash │ applied_by │ applied_at │
|
118
|
+
├─────────┼────┼────────────────────────────────┼────────────────────────────────────────────────────────────────────┼────────────┼────────────────────────────┤
|
119
|
+
│ 0 │ 1 │ 'v1_connect_session_table.sql' │ 'f08638e58139ae0e2dda24b1bdba29f3f2128597066a23d2bb382d448bbe9d7e' │ 'postgres' │ '2024-11-23 18:29:16.1616' │
|
120
|
+
└─────────┴────┴────────────────────────────────┴────────────────────────────────────────────────────────────────────┴────────────┴────────────────────────────┘
|
124
121
|
```
|
125
122
|
|
126
123
|
### Drop
|
127
124
|
|
125
|
+
Command:
|
126
|
+
|
128
127
|
```bash
|
129
128
|
npx stepwise-migrations drop \
|
130
129
|
--connection=postgresql://postgres:postgres@127.0.0.1:5432/mydb \
|
@@ -134,7 +133,5 @@ npx stepwise-migrations drop \
|
|
134
133
|
Outputs:
|
135
134
|
|
136
135
|
```
|
137
|
-
|
138
|
-
Dropping the tables, schema and migration history table
|
139
|
-
All done!
|
136
|
+
Dropping the tables, schema and migration history table... done!
|
140
137
|
```
|
package/dist/db.js
CHANGED
@@ -59,7 +59,6 @@ const dbConnect = (argv) => __awaiter(void 0, void 0, void 0, function* () {
|
|
59
59
|
try {
|
60
60
|
client = yield pool.connect();
|
61
61
|
yield client.query("SELECT 1");
|
62
|
-
console.log("Connected to the database");
|
63
62
|
}
|
64
63
|
catch (error) {
|
65
64
|
console.error("Failed to connect to the database", error);
|
@@ -84,13 +83,13 @@ const dbMigrationHistory = (client, schema) => __awaiter(void 0, void 0, void 0,
|
|
84
83
|
});
|
85
84
|
exports.dbMigrationHistory = dbMigrationHistory;
|
86
85
|
const dbCreateSchema = (client, schema) => __awaiter(void 0, void 0, void 0, function* () {
|
87
|
-
|
86
|
+
process.stdout.write(`Creating schema ${schema}... `);
|
88
87
|
yield client.query(`CREATE SCHEMA IF NOT EXISTS ${schema}`);
|
89
|
-
console.log(`
|
88
|
+
console.log(`done!`);
|
90
89
|
});
|
91
90
|
exports.dbCreateSchema = dbCreateSchema;
|
92
91
|
const dbCreateHistoryTable = (client, schema) => __awaiter(void 0, void 0, void 0, function* () {
|
93
|
-
|
92
|
+
process.stdout.write(`Creating migration history table... `);
|
94
93
|
yield client.query(`CREATE TABLE IF NOT EXISTS ${schema}.stepwise_migrations (
|
95
94
|
id SERIAL PRIMARY KEY,
|
96
95
|
name TEXT UNIQUE NOT NULL,
|
@@ -98,6 +97,6 @@ const dbCreateHistoryTable = (client, schema) => __awaiter(void 0, void 0, void
|
|
98
97
|
applied_by TEXT NOT NULL DEFAULT current_user,
|
99
98
|
applied_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
|
100
99
|
)`);
|
101
|
-
console.log(`
|
100
|
+
console.log(`done!`);
|
102
101
|
});
|
103
102
|
exports.dbCreateHistoryTable = dbCreateHistoryTable;
|
package/dist/index.js
CHANGED
@@ -35,29 +35,31 @@ const main = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
35
35
|
}
|
36
36
|
const migrationHistory = yield (0, db_1.dbMigrationHistory)(client, schema);
|
37
37
|
const migrationFiles = yield (0, utils_1.readMigrationFiles)(argv.path);
|
38
|
-
console.log(`Found ${migrationFiles.length} migration files`);
|
39
38
|
(0, migrate_1.validateMigrationFiles)(migrationFiles, migrationHistory);
|
40
39
|
const migrationsToApply = migrationFiles.slice(migrationHistory.length, migrationHistory.length + nUp);
|
41
40
|
for (const { filename, contents, hash } of migrationsToApply) {
|
42
41
|
yield (0, migrate_1.applyMigration)(client, schema, filename, contents, hash);
|
43
42
|
}
|
44
|
-
console.log(
|
43
|
+
console.log(`All done! Applied ${migrationsToApply.length} migrations`);
|
45
44
|
console.log("New migration history:");
|
46
45
|
console.table(yield (0, db_1.dbMigrationHistory)(client, schema));
|
47
46
|
}
|
48
47
|
else if (command === "info") {
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
48
|
+
if (!historySchemaExists) {
|
49
|
+
console.log("Schema does not exist");
|
50
|
+
}
|
51
|
+
if (!tableExists) {
|
52
|
+
console.log("Migration history table does not exist");
|
53
|
+
}
|
54
|
+
if (historySchemaExists && tableExists) {
|
55
|
+
console.log("Migration history:");
|
56
|
+
console.table(yield (0, db_1.dbMigrationHistory)(client, schema));
|
57
|
+
}
|
56
58
|
}
|
57
59
|
else if (command === "drop") {
|
58
|
-
|
60
|
+
process.stdout.write(`Dropping the tables, schema and migration history table... `);
|
59
61
|
yield client.query(`DROP SCHEMA IF EXISTS ${schema} CASCADE`);
|
60
|
-
console.log(
|
62
|
+
console.log(`done!`);
|
61
63
|
}
|
62
64
|
else if (command === "down") {
|
63
65
|
const nDown = argv.ndown || 1;
|
@@ -69,7 +71,7 @@ const main = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
69
71
|
for (const { filename, contents, upFilename, } of downMigrationFilesToApply) {
|
70
72
|
yield (0, migrate_1.applyDownMigration)(client, schema, filename, contents, upFilename);
|
71
73
|
}
|
72
|
-
console.log(
|
74
|
+
console.log(`All done! Applied ${downMigrationFilesToApply.length} down migrations`);
|
73
75
|
console.log("New migration history:");
|
74
76
|
console.table(yield (0, db_1.dbMigrationHistory)(client, schema));
|
75
77
|
}
|
package/dist/migrate.js
CHANGED
@@ -41,12 +41,13 @@ const validateMigrationFiles = (migrationFiles, migrationHistory, isUp = true) =
|
|
41
41
|
exports.validateMigrationFiles = validateMigrationFiles;
|
42
42
|
const applyMigration = (client, schema, filename, contents, hash) => __awaiter(void 0, void 0, void 0, function* () {
|
43
43
|
try {
|
44
|
+
process.stdout.write(`Applying migration ${filename}... `);
|
44
45
|
yield client.query("BEGIN");
|
45
46
|
yield client.query(`SET search_path TO ${schema};
|
46
47
|
${contents.toString()}`);
|
47
48
|
yield client.query(`INSERT INTO ${schema}.stepwise_migrations (name, hash) VALUES ($1, $2)`, [filename, hash]);
|
48
49
|
yield client.query("COMMIT");
|
49
|
-
console.log(`
|
50
|
+
console.log(`done!`);
|
50
51
|
}
|
51
52
|
catch (error) {
|
52
53
|
try {
|
@@ -73,12 +74,13 @@ const validateDownMigrationFiles = (downMigrationFilesToApply, reverseMigrationH
|
|
73
74
|
exports.validateDownMigrationFiles = validateDownMigrationFiles;
|
74
75
|
const applyDownMigration = (client, schema, filename, contents, upFilename) => __awaiter(void 0, void 0, void 0, function* () {
|
75
76
|
try {
|
77
|
+
process.stdout.write(`Applying down migration ${filename}... `);
|
76
78
|
yield client.query("BEGIN");
|
77
79
|
yield client.query(`SET search_path TO ${schema};
|
78
80
|
${contents.toString()}`);
|
79
81
|
yield client.query(`DELETE FROM ${schema}.stepwise_migrations WHERE name = $1`, [upFilename]);
|
80
82
|
yield client.query("COMMIT");
|
81
|
-
console.log(`
|
83
|
+
console.log(`done!`);
|
82
84
|
}
|
83
85
|
catch (error) {
|
84
86
|
try {
|
package/dist/utils.js
CHANGED
@@ -42,11 +42,10 @@ Options:
|
|
42
42
|
--ndown Number of down migrations to apply (default: 1)
|
43
43
|
|
44
44
|
Example:
|
45
|
-
npx stepwise-migrations \
|
45
|
+
npx stepwise-migrations migrate \
|
46
46
|
--connection=postgresql://postgres:postgres@127.0.0.1:5432/mydatabase \
|
47
47
|
--schema=myschema \
|
48
|
-
--path=./db/migration/
|
49
|
-
migrate
|
48
|
+
--path=./db/migration/
|
50
49
|
`;
|
51
50
|
const validateArgs = (argv) => {
|
52
51
|
const required = ["connection", "schema", "path", "_"];
|
package/package.json
CHANGED
package/src/db.ts
CHANGED
@@ -19,7 +19,6 @@ export const dbConnect = async (argv: { connection: string; ssl?: string }) => {
|
|
19
19
|
try {
|
20
20
|
client = await pool.connect();
|
21
21
|
await client.query("SELECT 1");
|
22
|
-
console.log("Connected to the database");
|
23
22
|
} catch (error) {
|
24
23
|
console.error("Failed to connect to the database", error);
|
25
24
|
process.exit(1);
|
@@ -57,16 +56,16 @@ export const dbMigrationHistory = async (
|
|
57
56
|
};
|
58
57
|
|
59
58
|
export const dbCreateSchema = async (client: PoolClient, schema: string) => {
|
60
|
-
|
59
|
+
process.stdout.write(`Creating schema ${schema}... `);
|
61
60
|
await client.query(`CREATE SCHEMA IF NOT EXISTS ${schema}`);
|
62
|
-
console.log(`
|
61
|
+
console.log(`done!`);
|
63
62
|
};
|
64
63
|
|
65
64
|
export const dbCreateHistoryTable = async (
|
66
65
|
client: PoolClient,
|
67
66
|
schema: string
|
68
67
|
) => {
|
69
|
-
|
68
|
+
process.stdout.write(`Creating migration history table... `);
|
70
69
|
await client.query(
|
71
70
|
`CREATE TABLE IF NOT EXISTS ${schema}.stepwise_migrations (
|
72
71
|
id SERIAL PRIMARY KEY,
|
@@ -76,5 +75,5 @@ export const dbCreateHistoryTable = async (
|
|
76
75
|
applied_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
|
77
76
|
)`
|
78
77
|
);
|
79
|
-
console.log(`
|
78
|
+
console.log(`done!`);
|
80
79
|
};
|
package/src/index.ts
CHANGED
@@ -44,7 +44,6 @@ const main = async () => {
|
|
44
44
|
|
45
45
|
const migrationHistory = await dbMigrationHistory(client, schema);
|
46
46
|
const migrationFiles = await readMigrationFiles(argv.path);
|
47
|
-
console.log(`Found ${migrationFiles.length} migration files`);
|
48
47
|
|
49
48
|
validateMigrationFiles(migrationFiles, migrationHistory);
|
50
49
|
|
@@ -57,27 +56,28 @@ const main = async () => {
|
|
57
56
|
await applyMigration(client, schema, filename, contents, hash);
|
58
57
|
}
|
59
58
|
|
60
|
-
console.log(
|
59
|
+
console.log(`All done! Applied ${migrationsToApply.length} migrations`);
|
61
60
|
console.log("New migration history:");
|
62
61
|
console.table(await dbMigrationHistory(client, schema));
|
63
62
|
} else if (command === "info") {
|
64
|
-
|
65
|
-
"
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
console.table(await dbMigrationHistory(client, schema));
|
63
|
+
if (!historySchemaExists) {
|
64
|
+
console.log("Schema does not exist");
|
65
|
+
}
|
66
|
+
|
67
|
+
if (!tableExists) {
|
68
|
+
console.log("Migration history table does not exist");
|
69
|
+
}
|
70
|
+
|
71
|
+
if (historySchemaExists && tableExists) {
|
72
|
+
console.log("Migration history:");
|
73
|
+
console.table(await dbMigrationHistory(client, schema));
|
74
|
+
}
|
77
75
|
} else if (command === "drop") {
|
78
|
-
|
76
|
+
process.stdout.write(
|
77
|
+
`Dropping the tables, schema and migration history table... `
|
78
|
+
);
|
79
79
|
await client.query(`DROP SCHEMA IF EXISTS ${schema} CASCADE`);
|
80
|
-
console.log(
|
80
|
+
console.log(`done!`);
|
81
81
|
} else if (command === "down") {
|
82
82
|
const nDown = argv.ndown || 1;
|
83
83
|
|
@@ -105,7 +105,9 @@ const main = async () => {
|
|
105
105
|
} of downMigrationFilesToApply) {
|
106
106
|
await applyDownMigration(client, schema, filename, contents, upFilename);
|
107
107
|
}
|
108
|
-
console.log(
|
108
|
+
console.log(
|
109
|
+
`All done! Applied ${downMigrationFilesToApply.length} down migrations`
|
110
|
+
);
|
109
111
|
console.log("New migration history:");
|
110
112
|
console.table(await dbMigrationHistory(client, schema));
|
111
113
|
}
|
package/src/migrate.ts
CHANGED
@@ -49,6 +49,7 @@ export const applyMigration = async (
|
|
49
49
|
hash: string
|
50
50
|
) => {
|
51
51
|
try {
|
52
|
+
process.stdout.write(`Applying migration ${filename}... `);
|
52
53
|
await client.query("BEGIN");
|
53
54
|
|
54
55
|
await client.query(
|
@@ -63,7 +64,7 @@ export const applyMigration = async (
|
|
63
64
|
|
64
65
|
await client.query("COMMIT");
|
65
66
|
|
66
|
-
console.log(`
|
67
|
+
console.log(`done!`);
|
67
68
|
} catch (error) {
|
68
69
|
try {
|
69
70
|
await client.query("ROLLBACK");
|
@@ -101,6 +102,7 @@ export const applyDownMigration = async (
|
|
101
102
|
upFilename: string
|
102
103
|
) => {
|
103
104
|
try {
|
105
|
+
process.stdout.write(`Applying down migration ${filename}... `);
|
104
106
|
await client.query("BEGIN");
|
105
107
|
|
106
108
|
await client.query(
|
@@ -115,7 +117,7 @@ export const applyDownMigration = async (
|
|
115
117
|
|
116
118
|
await client.query("COMMIT");
|
117
119
|
|
118
|
-
console.log(`
|
120
|
+
console.log(`done!`);
|
119
121
|
} catch (error) {
|
120
122
|
try {
|
121
123
|
await client.query("ROLLBACK");
|
package/src/utils.ts
CHANGED
@@ -29,11 +29,10 @@ Options:
|
|
29
29
|
--ndown Number of down migrations to apply (default: 1)
|
30
30
|
|
31
31
|
Example:
|
32
|
-
npx stepwise-migrations \
|
32
|
+
npx stepwise-migrations migrate \
|
33
33
|
--connection=postgresql://postgres:postgres@127.0.0.1:5432/mydatabase \
|
34
34
|
--schema=myschema \
|
35
|
-
--path=./db/migration/
|
36
|
-
migrate
|
35
|
+
--path=./db/migration/
|
37
36
|
`;
|
38
37
|
|
39
38
|
export const validateArgs = (argv: any) => {
|