@sebspark/spanner-migrate 1.0.1 → 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.
@@ -85,30 +85,67 @@ var isSchemaChange = (sql) => /^\s*(CREATE|ALTER|DROP|TRUNCATE)\b/i.test(sql);
85
85
  // src/db.ts
86
86
  var SQL_SELECT_TABLE_MIGRATIONS = `
87
87
  SELECT
88
- table_name
88
+ t.TABLE_NAME,
89
+ c.COLUMN_NAME,
90
+ c.SPANNER_TYPE
89
91
  FROM
90
- information_schema.tables
92
+ information_schema.TABLES t
93
+ INNER JOIN
94
+ information_schema.COLUMNS c
95
+ ON t.TABLE_NAME = c.TABLE_NAME
91
96
  WHERE
92
- table_name = 'migrations'
97
+ t.TABLE_NAME = 'migrations'
93
98
  `;
94
99
  var SQL_CREATE_TABLE_MIGRATIONS = `
95
100
  CREATE TABLE migrations (
96
101
  id STRING(128) NOT NULL,
97
102
  description STRING(256) NOT NULL,
98
103
  applied_at TIMESTAMP NOT NULL OPTIONS (allow_commit_timestamp = true),
99
- up STRING(1024),
100
- down STRING(1024)
104
+ up STRING(MAX),
105
+ down STRING(MAX)
101
106
  ) PRIMARY KEY (id)
102
107
  `;
103
108
  var ensureMigrationTable = async (db) => {
104
109
  const [rows] = await db.run(SQL_SELECT_TABLE_MIGRATIONS);
105
- if (rows.length) return;
106
- console.log("Creating migration table");
107
- try {
108
- await db.updateSchema(SQL_CREATE_TABLE_MIGRATIONS);
109
- } catch (err) {
110
- console.error("Failed to create migrations table");
111
- throw err;
110
+ if (rows.length === 0) {
111
+ console.log("Creating migration table");
112
+ try {
113
+ await db.updateSchema(SQL_CREATE_TABLE_MIGRATIONS);
114
+ } catch (err) {
115
+ console.error("Failed to create migrations table");
116
+ throw err;
117
+ }
118
+ } else {
119
+ const typedRows = rows;
120
+ const upType = typedRows.find((r) => r.COLUMN_NAME === "up");
121
+ const downType = typedRows.find((r) => r.COLUMN_NAME === "down");
122
+ const expectedType = "STRING(MAX)";
123
+ if (upType?.SPANNER_TYPE !== expectedType) {
124
+ try {
125
+ console.log(
126
+ `Updating 'up' column of migration table to ${expectedType}`
127
+ );
128
+ await db.updateSchema(
129
+ `ALTER TABLE migrations ALTER COLUMN up ${expectedType};`
130
+ );
131
+ } catch (err) {
132
+ console.error("Failed to update migrations table");
133
+ throw err;
134
+ }
135
+ }
136
+ if (downType?.SPANNER_TYPE !== expectedType) {
137
+ try {
138
+ console.log(
139
+ `Updating 'down' column of migration table to ${expectedType}`
140
+ );
141
+ await db.updateSchema(
142
+ `ALTER TABLE migrations ALTER COLUMN down ${expectedType};`
143
+ );
144
+ } catch (err) {
145
+ console.error("Failed to update migrations table");
146
+ throw err;
147
+ }
148
+ }
112
149
  }
113
150
  };
114
151
  var getAppliedMigrations = async (db) => {
package/dist/cli.js CHANGED
@@ -117,30 +117,67 @@ var isSchemaChange = (sql) => /^\s*(CREATE|ALTER|DROP|TRUNCATE)\b/i.test(sql);
117
117
  // src/db.ts
118
118
  var SQL_SELECT_TABLE_MIGRATIONS = `
119
119
  SELECT
120
- table_name
120
+ t.TABLE_NAME,
121
+ c.COLUMN_NAME,
122
+ c.SPANNER_TYPE
121
123
  FROM
122
- information_schema.tables
124
+ information_schema.TABLES t
125
+ INNER JOIN
126
+ information_schema.COLUMNS c
127
+ ON t.TABLE_NAME = c.TABLE_NAME
123
128
  WHERE
124
- table_name = 'migrations'
129
+ t.TABLE_NAME = 'migrations'
125
130
  `;
126
131
  var SQL_CREATE_TABLE_MIGRATIONS = `
127
132
  CREATE TABLE migrations (
128
133
  id STRING(128) NOT NULL,
129
134
  description STRING(256) NOT NULL,
130
135
  applied_at TIMESTAMP NOT NULL OPTIONS (allow_commit_timestamp = true),
131
- up STRING(1024),
132
- down STRING(1024)
136
+ up STRING(MAX),
137
+ down STRING(MAX)
133
138
  ) PRIMARY KEY (id)
134
139
  `;
135
140
  var ensureMigrationTable = async (db) => {
136
141
  const [rows] = await db.run(SQL_SELECT_TABLE_MIGRATIONS);
137
- if (rows.length) return;
138
- console.log("Creating migration table");
139
- try {
140
- await db.updateSchema(SQL_CREATE_TABLE_MIGRATIONS);
141
- } catch (err) {
142
- console.error("Failed to create migrations table");
143
- throw err;
142
+ if (rows.length === 0) {
143
+ console.log("Creating migration table");
144
+ try {
145
+ await db.updateSchema(SQL_CREATE_TABLE_MIGRATIONS);
146
+ } catch (err) {
147
+ console.error("Failed to create migrations table");
148
+ throw err;
149
+ }
150
+ } else {
151
+ const typedRows = rows;
152
+ const upType = typedRows.find((r) => r.COLUMN_NAME === "up");
153
+ const downType = typedRows.find((r) => r.COLUMN_NAME === "down");
154
+ const expectedType = "STRING(MAX)";
155
+ if (upType?.SPANNER_TYPE !== expectedType) {
156
+ try {
157
+ console.log(
158
+ `Updating 'up' column of migration table to ${expectedType}`
159
+ );
160
+ await db.updateSchema(
161
+ `ALTER TABLE migrations ALTER COLUMN up ${expectedType};`
162
+ );
163
+ } catch (err) {
164
+ console.error("Failed to update migrations table");
165
+ throw err;
166
+ }
167
+ }
168
+ if (downType?.SPANNER_TYPE !== expectedType) {
169
+ try {
170
+ console.log(
171
+ `Updating 'down' column of migration table to ${expectedType}`
172
+ );
173
+ await db.updateSchema(
174
+ `ALTER TABLE migrations ALTER COLUMN down ${expectedType};`
175
+ );
176
+ } catch (err) {
177
+ console.error("Failed to update migrations table");
178
+ throw err;
179
+ }
180
+ }
144
181
  }
145
182
  };
146
183
  var getAppliedMigrations = async (db) => {
package/dist/cli.mjs CHANGED
@@ -5,7 +5,7 @@ import {
5
5
  init,
6
6
  status,
7
7
  up
8
- } from "./chunk-SZDH364K.mjs";
8
+ } from "./chunk-FS26ULSB.mjs";
9
9
 
10
10
  // src/cli.ts
11
11
  import fs from "node:fs/promises";
package/dist/index.js CHANGED
@@ -113,30 +113,67 @@ var isSchemaChange = (sql) => /^\s*(CREATE|ALTER|DROP|TRUNCATE)\b/i.test(sql);
113
113
  // src/db.ts
114
114
  var SQL_SELECT_TABLE_MIGRATIONS = `
115
115
  SELECT
116
- table_name
116
+ t.TABLE_NAME,
117
+ c.COLUMN_NAME,
118
+ c.SPANNER_TYPE
117
119
  FROM
118
- information_schema.tables
120
+ information_schema.TABLES t
121
+ INNER JOIN
122
+ information_schema.COLUMNS c
123
+ ON t.TABLE_NAME = c.TABLE_NAME
119
124
  WHERE
120
- table_name = 'migrations'
125
+ t.TABLE_NAME = 'migrations'
121
126
  `;
122
127
  var SQL_CREATE_TABLE_MIGRATIONS = `
123
128
  CREATE TABLE migrations (
124
129
  id STRING(128) NOT NULL,
125
130
  description STRING(256) NOT NULL,
126
131
  applied_at TIMESTAMP NOT NULL OPTIONS (allow_commit_timestamp = true),
127
- up STRING(1024),
128
- down STRING(1024)
132
+ up STRING(MAX),
133
+ down STRING(MAX)
129
134
  ) PRIMARY KEY (id)
130
135
  `;
131
136
  var ensureMigrationTable = async (db) => {
132
137
  const [rows] = await db.run(SQL_SELECT_TABLE_MIGRATIONS);
133
- if (rows.length) return;
134
- console.log("Creating migration table");
135
- try {
136
- await db.updateSchema(SQL_CREATE_TABLE_MIGRATIONS);
137
- } catch (err) {
138
- console.error("Failed to create migrations table");
139
- throw err;
138
+ if (rows.length === 0) {
139
+ console.log("Creating migration table");
140
+ try {
141
+ await db.updateSchema(SQL_CREATE_TABLE_MIGRATIONS);
142
+ } catch (err) {
143
+ console.error("Failed to create migrations table");
144
+ throw err;
145
+ }
146
+ } else {
147
+ const typedRows = rows;
148
+ const upType = typedRows.find((r) => r.COLUMN_NAME === "up");
149
+ const downType = typedRows.find((r) => r.COLUMN_NAME === "down");
150
+ const expectedType = "STRING(MAX)";
151
+ if (upType?.SPANNER_TYPE !== expectedType) {
152
+ try {
153
+ console.log(
154
+ `Updating 'up' column of migration table to ${expectedType}`
155
+ );
156
+ await db.updateSchema(
157
+ `ALTER TABLE migrations ALTER COLUMN up ${expectedType};`
158
+ );
159
+ } catch (err) {
160
+ console.error("Failed to update migrations table");
161
+ throw err;
162
+ }
163
+ }
164
+ if (downType?.SPANNER_TYPE !== expectedType) {
165
+ try {
166
+ console.log(
167
+ `Updating 'down' column of migration table to ${expectedType}`
168
+ );
169
+ await db.updateSchema(
170
+ `ALTER TABLE migrations ALTER COLUMN down ${expectedType};`
171
+ );
172
+ } catch (err) {
173
+ console.error("Failed to update migrations table");
174
+ throw err;
175
+ }
176
+ }
140
177
  }
141
178
  };
142
179
  var getAppliedMigrations = async (db) => {
package/dist/index.mjs CHANGED
@@ -4,7 +4,7 @@ import {
4
4
  init,
5
5
  status,
6
6
  up
7
- } from "./chunk-SZDH364K.mjs";
7
+ } from "./chunk-FS26ULSB.mjs";
8
8
  export {
9
9
  create,
10
10
  down,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sebspark/spanner-migrate",
3
- "version": "1.0.1",
3
+ "version": "1.1.0",
4
4
  "license": "Apache-2.0",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -18,7 +18,7 @@
18
18
  "typecheck": "tsc --noEmit "
19
19
  },
20
20
  "devDependencies": {
21
- "@google-cloud/spanner": "7.21.0",
21
+ "@google-cloud/spanner": "8.0.0",
22
22
  "@sebspark/cli-tester": "*",
23
23
  "@sebspark/spanner-mock": "*",
24
24
  "@types/jest": "29.5.14",