106 lines
3.3 KiB
JavaScript
106 lines
3.3 KiB
JavaScript
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);
|
|
});
|