u
This commit is contained in:
@@ -1,112 +0,0 @@
|
|||||||
import 'dotenv/config';
|
|
||||||
import Database from 'better-sqlite3';
|
|
||||||
import { MssqlProvider } from '../src/providers/mssql/index.js';
|
|
||||||
|
|
||||||
async function migrateSqlite(sqlitePath) {
|
|
||||||
console.log(`Migrating SQLite database at: ${sqlitePath}`);
|
|
||||||
const db = new Database(sqlitePath);
|
|
||||||
|
|
||||||
const columns = db.pragma('table_info("manifest")');
|
|
||||||
const hasCreatedAt = columns.some((c) => c.name === 'created_at');
|
|
||||||
if (hasCreatedAt) {
|
|
||||||
console.log('SQLite manifest already has created_at column.');
|
|
||||||
db.close();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
db.pragma('foreign_keys = OFF');
|
|
||||||
db.exec(`
|
|
||||||
BEGIN TRANSACTION;
|
|
||||||
CREATE TABLE manifest_new (
|
|
||||||
id INTEGER PRIMARY KEY,
|
|
||||||
company_id INTEGER NOT NULL,
|
|
||||||
"number" TEXT NOT NULL,
|
|
||||||
"type" TEXT NOT NULL,
|
|
||||||
entity_id INTEGER,
|
|
||||||
external_number TEXT,
|
|
||||||
"date" TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now')),
|
|
||||||
created_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now')),
|
|
||||||
FOREIGN KEY (company_id) REFERENCES company(id) ON DELETE NO ACTION,
|
|
||||||
FOREIGN KEY (entity_id) REFERENCES entity(id) ON DELETE SET NULL
|
|
||||||
);
|
|
||||||
INSERT INTO manifest_new (id, company_id, "number", "type", entity_id, external_number, "date", created_at)
|
|
||||||
SELECT id, company_id, "number", "type", entity_id, external_number, "date", "date"
|
|
||||||
FROM manifest;
|
|
||||||
DROP TABLE manifest;
|
|
||||||
ALTER TABLE manifest_new RENAME TO manifest;
|
|
||||||
COMMIT;
|
|
||||||
`);
|
|
||||||
db.pragma('foreign_keys = ON');
|
|
||||||
|
|
||||||
const fkErrors = db.pragma('foreign_key_check');
|
|
||||||
if (fkErrors.length > 0) {
|
|
||||||
console.error('Foreign key check failed after SQLite migration:', fkErrors);
|
|
||||||
} else {
|
|
||||||
console.log('SQLite migration completed successfully.');
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log('Updated SQLite manifest columns:', db.pragma('table_info("manifest")'));
|
|
||||||
console.log('Updated SQLite manifest sample row:', db.prepare('SELECT * FROM manifest LIMIT 1').get());
|
|
||||||
db.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
async function migrateMssql() {
|
|
||||||
console.log('Migrating MSSQL database...');
|
|
||||||
const config = {
|
|
||||||
server: process.env.MSSQL_SERVER,
|
|
||||||
database: process.env.MSSQL_DATABASE,
|
|
||||||
user: process.env.MSSQL_USER,
|
|
||||||
password: process.env.MSSQL_PASSWORD,
|
|
||||||
options: {
|
|
||||||
encrypt: false,
|
|
||||||
trustServerCertificate: true,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
const provider = new MssqlProvider(config);
|
|
||||||
try {
|
|
||||||
const pool = await provider.ensurePool();
|
|
||||||
const checkRes = await pool.request().query(`
|
|
||||||
SELECT * FROM sys.columns
|
|
||||||
WHERE object_id = OBJECT_ID(N'manifest') AND name = 'created_at'
|
|
||||||
`);
|
|
||||||
|
|
||||||
if (checkRes.recordset.length > 0) {
|
|
||||||
console.log('MSSQL manifest already has created_at column.');
|
|
||||||
} else {
|
|
||||||
await pool.request().query(`
|
|
||||||
ALTER TABLE manifest ADD created_at DATETIME2 NOT NULL CONSTRAINT DF_manifest_created_at DEFAULT SYSUTCDATETIME();
|
|
||||||
`);
|
|
||||||
await pool.request().query(`
|
|
||||||
UPDATE manifest SET created_at = [date];
|
|
||||||
`);
|
|
||||||
console.log('MSSQL migration completed successfully.');
|
|
||||||
}
|
|
||||||
|
|
||||||
const colsRes = await pool.request().query(`
|
|
||||||
SELECT COLUMN_NAME, DATA_TYPE, IS_NULLABLE, COLUMN_DEFAULT
|
|
||||||
FROM INFORMATION_SCHEMA.COLUMNS
|
|
||||||
WHERE TABLE_NAME = 'manifest'
|
|
||||||
`);
|
|
||||||
console.log('Updated MSSQL manifest columns:', colsRes.recordset);
|
|
||||||
const sampleRes = await pool.request().query(`
|
|
||||||
SELECT TOP 1 * FROM manifest
|
|
||||||
`);
|
|
||||||
console.log('Updated MSSQL manifest sample row:', sampleRes.recordset[0]);
|
|
||||||
await provider.close();
|
|
||||||
} catch (err) {
|
|
||||||
console.error('MSSQL migration failed:', err);
|
|
||||||
await provider.close();
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function run() {
|
|
||||||
await migrateSqlite('/home/seb/src/dashback/data/dash.db');
|
|
||||||
await migrateMssql();
|
|
||||||
}
|
|
||||||
|
|
||||||
run().catch((err) => {
|
|
||||||
console.error('Migration failed:', err);
|
|
||||||
process.exit(1);
|
|
||||||
});
|
|
||||||
@@ -1,105 +0,0 @@
|
|||||||
import 'dotenv/config';
|
|
||||||
import Database from 'better-sqlite3';
|
|
||||||
import { MssqlProvider } from '../src/providers/mssql/index.js';
|
|
||||||
|
|
||||||
async function migrateSqlite(sqlitePath) {
|
|
||||||
console.log(`Migrating SQLite database at: ${sqlitePath}`);
|
|
||||||
const db = new Database(sqlitePath);
|
|
||||||
|
|
||||||
const row = db.prepare("SELECT sql FROM sqlite_master WHERE type = 'table' AND name = 'printer'").get();
|
|
||||||
if (!row) {
|
|
||||||
console.log('Printer table does not exist in SQLite.');
|
|
||||||
db.close();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (row.sql.includes("'ipp'")) {
|
|
||||||
console.log("SQLite printer table already includes 'ipp' in CHECK constraint.");
|
|
||||||
db.close();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
db.pragma('foreign_keys = OFF');
|
|
||||||
db.exec(`
|
|
||||||
BEGIN TRANSACTION;
|
|
||||||
CREATE TABLE printer_new (
|
|
||||||
id INTEGER PRIMARY KEY,
|
|
||||||
company_id INTEGER NOT NULL,
|
|
||||||
address TEXT NOT NULL,
|
|
||||||
type TEXT NOT NULL CHECK (type IN ('esc/pos', 'zpl', 'ipp')),
|
|
||||||
model TEXT,
|
|
||||||
serial TEXT,
|
|
||||||
sort INTEGER NOT NULL DEFAULT 0,
|
|
||||||
"desc" TEXT,
|
|
||||||
FOREIGN KEY (company_id) REFERENCES company(id) ON DELETE CASCADE
|
|
||||||
);
|
|
||||||
INSERT INTO printer_new (id, company_id, address, type, model, serial, sort, "desc")
|
|
||||||
SELECT id, company_id, address, type, model, serial, sort, "desc"
|
|
||||||
FROM printer;
|
|
||||||
DROP TABLE printer;
|
|
||||||
ALTER TABLE printer_new RENAME TO printer;
|
|
||||||
COMMIT;
|
|
||||||
`);
|
|
||||||
db.pragma('foreign_keys = ON');
|
|
||||||
|
|
||||||
const fkErrors = db.pragma('foreign_key_check');
|
|
||||||
if (fkErrors.length > 0) {
|
|
||||||
console.error('Foreign key check failed after SQLite migration:', fkErrors);
|
|
||||||
} else {
|
|
||||||
console.log('SQLite migration completed successfully.');
|
|
||||||
}
|
|
||||||
|
|
||||||
const updatedRow = db.prepare("SELECT sql FROM sqlite_master WHERE type = 'table' AND name = 'printer'").get();
|
|
||||||
console.log('Updated SQLite printer table SQL:', updatedRow?.sql);
|
|
||||||
db.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
async function migrateMssql() {
|
|
||||||
console.log('Migrating MSSQL database...');
|
|
||||||
const config = {
|
|
||||||
server: process.env.MSSQL_SERVER,
|
|
||||||
database: process.env.MSSQL_DATABASE,
|
|
||||||
user: process.env.MSSQL_USER,
|
|
||||||
password: process.env.MSSQL_PASSWORD,
|
|
||||||
options: {
|
|
||||||
encrypt: false,
|
|
||||||
trustServerCertificate: true,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
try {
|
|
||||||
const provider = new MssqlProvider(config);
|
|
||||||
const pool = await provider.ensurePool();
|
|
||||||
const tableRes = await pool.request().query(`
|
|
||||||
SELECT * FROM sys.tables WHERE name = 'printer'
|
|
||||||
`);
|
|
||||||
if (tableRes.recordset.length === 0) {
|
|
||||||
console.log('Printer table does not exist in MSSQL.');
|
|
||||||
await provider.close();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Drop old CK_printer_type constraint if exists and add new one
|
|
||||||
await pool.request().query(`
|
|
||||||
IF EXISTS (SELECT * FROM sys.check_constraints WHERE name = 'CK_printer_type')
|
|
||||||
BEGIN
|
|
||||||
ALTER TABLE printer DROP CONSTRAINT CK_printer_type;
|
|
||||||
END
|
|
||||||
ALTER TABLE printer ADD CONSTRAINT CK_printer_type CHECK (type IN ('esc/pos', 'zpl', 'ipp'));
|
|
||||||
`);
|
|
||||||
console.log('MSSQL migration completed successfully.');
|
|
||||||
await provider.close();
|
|
||||||
} catch (err) {
|
|
||||||
console.warn('MSSQL migration skipped/failed (possibly server not running):', err.message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function run() {
|
|
||||||
await migrateSqlite('/home/seb/src/dashback/data/dash.db');
|
|
||||||
await migrateMssql();
|
|
||||||
}
|
|
||||||
|
|
||||||
run().catch((err) => {
|
|
||||||
console.error('Migration failed:', err);
|
|
||||||
process.exit(1);
|
|
||||||
});
|
|
||||||
Reference in New Issue
Block a user