diff --git a/src/providers/mssql/index.js b/src/providers/mssql/index.js index 3ddea49..245bdfc 100644 --- a/src/providers/mssql/index.js +++ b/src/providers/mssql/index.js @@ -199,6 +199,11 @@ export class MssqlProvider { ALTER TABLE category ADD updated_at DATETIME2 NOT NULL CONSTRAINT DF_category_updated_at DEFAULT SYSUTCDATETIME(); END + IF OBJECT_ID(N'dbo.payment', N'U') IS NOT NULL AND COL_LENGTH('payment', 'created_at') IS NULL + BEGIN + ALTER TABLE payment ADD created_at DATETIME2 NOT NULL CONSTRAINT DF_payment_created_at DEFAULT SYSUTCDATETIME(); + END + IF OBJECT_ID(N'dbo.customergroup', N'U') IS NOT NULL AND COL_LENGTH('customergroup', 'updated_at') IS NULL BEGIN ALTER TABLE customergroup ADD updated_at DATETIME2 NOT NULL CONSTRAINT DF_customergroup_updated_at DEFAULT SYSUTCDATETIME(); diff --git a/src/providers/mssql/queries/logistics.js b/src/providers/mssql/queries/logistics.js index b9c480f..3903893 100644 --- a/src/providers/mssql/queries/logistics.js +++ b/src/providers/mssql/queries/logistics.js @@ -39,12 +39,12 @@ WHERE id = @id AND company_id = @companyId export const PAYMENT_CREATE = ` INSERT INTO payment (account_id, manifest_id, [type], amount) -OUTPUT INSERTED.id, INSERTED.account_id, INSERTED.manifest_id, INSERTED.[type], INSERTED.amount +OUTPUT INSERTED.id, INSERTED.account_id, INSERTED.manifest_id, INSERTED.[type], INSERTED.amount, INSERTED.created_at AS createdAt VALUES (@accountId, @manifestId, @type, @amount) `; export const PAYMENT_LIST_BY_MANIFEST = ` -SELECT id, account_id, manifest_id, [type], amount +SELECT id, account_id, manifest_id, [type], amount, created_at AS createdAt FROM payment WHERE manifest_id = @manifestId ORDER BY id ASC diff --git a/src/providers/mssql/schema.js b/src/providers/mssql/schema.js index 1dd68ba..2c6d068 100644 --- a/src/providers/mssql/schema.js +++ b/src/providers/mssql/schema.js @@ -1141,6 +1141,7 @@ BEGIN manifest_id INT NOT NULL, [type] NVARCHAR(100) NOT NULL, amount FLOAT NOT NULL, + created_at DATETIME2 NOT NULL CONSTRAINT DF_payment_created_at DEFAULT SYSUTCDATETIME(), CONSTRAINT FK_payment_account FOREIGN KEY (account_id) REFERENCES account(id) ON DELETE CASCADE, CONSTRAINT FK_payment_manifest FOREIGN KEY (manifest_id) REFERENCES manifest(id) ON DELETE CASCADE ); diff --git a/src/providers/sqlite3/index.js b/src/providers/sqlite3/index.js index 9ae2071..b3e4d1c 100644 --- a/src/providers/sqlite3/index.js +++ b/src/providers/sqlite3/index.js @@ -494,6 +494,13 @@ export class BetterSqlite3Provider extends BetterSqlite3Executor { this.db.exec(`UPDATE category SET updated_at = strftime('%Y-%m-%dT%H:%M:%fZ', 'now') WHERE updated_at IS NULL`); } + const paymentCols = this.db.pragma('table_info("payment")').map((c) => c.name); + if (paymentCols.length > 0 && !paymentCols.includes('created_at')) { + this.db.exec('ALTER TABLE payment ADD COLUMN created_at TEXT'); + this.db.exec('UPDATE payment SET created_at = (SELECT m.created_at FROM manifest m WHERE m.id = payment.manifest_id) WHERE EXISTS (SELECT 1 FROM manifest m WHERE m.id = payment.manifest_id)'); + this.db.exec('UPDATE payment SET created_at = strftime("%Y-%m-%dT%H:%M:%fZ", "now") WHERE created_at IS NULL'); + } + const customergroupCols = this.db.pragma('table_info("customergroup")').map((c) => c.name); if (customergroupCols.length > 0 && !customergroupCols.includes('updated_at')) { this.db.exec(`ALTER TABLE customergroup ADD COLUMN updated_at TEXT`); diff --git a/src/providers/sqlite3/schema.js b/src/providers/sqlite3/schema.js index 01f3685..583105a 100644 --- a/src/providers/sqlite3/schema.js +++ b/src/providers/sqlite3/schema.js @@ -813,6 +813,7 @@ CREATE TABLE IF NOT EXISTS payment ( manifest_id INTEGER NOT NULL, "type" TEXT NOT NULL, amount REAL NOT NULL, + created_at TEXT NOT NULL DEFAULT (${UTC_NOW}), FOREIGN KEY (account_id) REFERENCES account(id) ON DELETE CASCADE, FOREIGN KEY (manifest_id) REFERENCES manifest(id) ON DELETE CASCADE ); diff --git a/src/store/payment/create.js b/src/store/payment/create.js index d446916..dc77cf7 100644 --- a/src/store/payment/create.js +++ b/src/store/payment/create.js @@ -11,6 +11,8 @@ function shape(row) { manifestId: row.manifest_id, type: row.type, amount: row.amount, + createdAt: row.createdAt ?? row.created_at ?? null, + date: row.date ?? row.createdAt ?? row.created_at ?? null, }; } diff --git a/src/store/payment/listByManifestId.js b/src/store/payment/listByManifestId.js index 5e07e41..e342281 100644 --- a/src/store/payment/listByManifestId.js +++ b/src/store/payment/listByManifestId.js @@ -32,6 +32,8 @@ export default function listByManifestId(provider) { manifestId: row.manifest_id, type: row.type, amount: row.amount, + createdAt: row.createdAt ?? row.created_at ?? null, + date: row.date ?? row.createdAt ?? row.created_at ?? null, })); }; }