@@ -55,7 +55,10 @@ const {
5555 tablesDBUpdate,
5656 tablesDBCreateTable,
5757 tablesDBGetTable,
58- tablesDBUpdateTable
58+ tablesDBUpdateTable,
59+ tablesDBList,
60+ tablesDBDelete,
61+ tablesDBListTables
5962} = require("./tables-db");
6063const {
6164 storageGetBucket, storageUpdateBucket, storageCreateBucket
@@ -1700,13 +1703,147 @@ const pushFunction = async ({ functionId, async, code, withVariables } = { retur
17001703 }
17011704}
17021705
1706+ const checkAndApplyTablesDBChanges = async () => {
1707+ log('Checking for tablesDB changes ...');
1708+
1709+ const localTablesDBs = localConfig.getTablesDBs();
1710+ const { databases: remoteTablesDBs } = await paginate(tablesDBList, { parseOutput: false }, 100, 'databases');
1711+
1712+ if (localTablesDBs.length === 0 && remoteTablesDBs.length === 0) {
1713+ return { applied: false, resyncNeeded: false };
1714+ }
1715+
1716+ const changes = [];
1717+ const toCreate = [];
1718+ const toUpdate = [];
1719+ const toDelete = [];
1720+
1721+ // Check for deletions - remote DBs that aren't in local config
1722+ for (const remoteDB of remoteTablesDBs) {
1723+ const localDB = localTablesDBs.find(db => db.$id === remoteDB.$id);
1724+ if (!localDB) {
1725+ toDelete.push(remoteDB);
1726+ changes.push({
1727+ id: remoteDB.$id,
1728+ key: 'Database',
1729+ remote: chalk.red(`${remoteDB.name} (${remoteDB.$id})`),
1730+ local: chalk.green('(deleted locally)')
1731+ });
1732+ }
1733+ }
1734+
1735+ // Check for additions and updates
1736+ for (const localDB of localTablesDBs) {
1737+ const remoteDB = remoteTablesDBs.find(db => db.$id === localDB.$id);
1738+
1739+ if (!remoteDB) {
1740+ toCreate.push(localDB);
1741+ changes.push({
1742+ id: localDB.$id,
1743+ key: 'Database',
1744+ remote: chalk.red('(does not exist)'),
1745+ local: chalk.green(`${localDB.name} (${localDB.$id})`)
1746+ });
1747+ } else if (remoteDB.name !== localDB.name) {
1748+ toUpdate.push(localDB);
1749+ changes.push({
1750+ id: localDB.$id,
1751+ key: 'Name',
1752+ remote: chalk.red(remoteDB.name),
1753+ local: chalk.green(localDB.name)
1754+ });
1755+ } else if (remoteDB.enabled !== localDB.enabled) {
1756+ toUpdate.push(localDB);
1757+ changes.push({
1758+ id: localDB.$id,
1759+ key: 'Enabled?',
1760+ remote: chalk.red(remoteDB.enabled),
1761+ local: chalk.green(localDB.enabled)
1762+ });
1763+ }
1764+ }
1765+
1766+ if (changes.length === 0) {
1767+ return { applied: false, resyncNeeded: false };
1768+ }
1769+
1770+ log('Found changes in tablesDB resources:');
1771+ drawTable(changes);
1772+
1773+ if (toDelete.length > 0) {
1774+ console.log(`${chalk.red('-------------------------------------------------------------------')}`);
1775+ console.log(`${chalk.red('| WARNING: Database deletion will also delete all related tables |')}`);
1776+ console.log(`${chalk.red('-------------------------------------------------------------------')}`);
1777+ }
1778+
1779+ if ((await getConfirmation()) !== true) {
1780+ return { applied: false, resyncNeeded: false };
1781+ }
1782+
1783+ // Apply deletions first
1784+ let needsResync = false;
1785+ for (const db of toDelete) {
1786+ log(`Deleting database ${db.name} ( ${db.$id} ) ...`);
1787+ await tablesDBDelete({
1788+ databaseId: db.$id,
1789+ parseOutput: false
1790+ });
1791+ success(`Deleted ${db.name} ( ${db.$id} )`);
1792+ needsResync = true;
1793+ }
1794+
1795+ // Apply creations
1796+ for (const db of toCreate) {
1797+ log(`Creating database ${db.name} ( ${db.$id} ) ...`);
1798+ await tablesDBCreate({
1799+ databaseId: db.$id,
1800+ name: db.name,
1801+ parseOutput: false
1802+ });
1803+ success(`Created ${db.name} ( ${db.$id} )`);
1804+ }
1805+
1806+ // Apply updates
1807+ for (const db of toUpdate) {
1808+ log(`Updating database ${db.name} ( ${db.$id} ) ...`);
1809+ await tablesDBUpdate({
1810+ databaseId: db.$id,
1811+ name: db.name,
1812+ enabled: db.enabled,
1813+ parseOutput: false
1814+ });
1815+ success(`Updated ${db.name} ( ${db.$id} )`);
1816+ }
1817+
1818+ return { applied: true, resyncNeeded: needsResync };
1819+ };
1820+
17031821const pushTable = async ({ returnOnZero, attempts } = { returnOnZero: false }) => {
17041822 const tables = [];
17051823
17061824 if (attempts) {
17071825 pollMaxDebounces = attempts;
17081826 }
17091827
1828+ const { applied: tablesDBApplied, resyncNeeded } = await checkAndApplyTablesDBChanges();
1829+ if (resyncNeeded) {
1830+ log('Resyncing configuration due to tablesDB deletions ...');
1831+
1832+ const remoteTablesDBs = (await paginate(tablesDBList, { parseOutput: false }, 100, 'databases')).databases;
1833+ const localTablesDBs = localConfig.getTablesDBs();
1834+
1835+ const remoteDatabaseIds = new Set(remoteTablesDBs.map(db => db.$id));
1836+ const localTables = localConfig.getTables();
1837+ const validTables = localTables.filter(table => remoteDatabaseIds.has(table.databaseId));
1838+
1839+ localConfig.set('tables', validTables);
1840+
1841+ const validTablesDBs = localTablesDBs.filter(db => remoteDatabaseIds.has(db.$id));
1842+ localConfig.set('tablesDB', validTablesDBs);
1843+
1844+ success('Configuration resynced successfully.');
1845+ }
1846+
17101847 if (cliConfig.all) {
17111848 checkDeployConditions(localConfig);
17121849 tables.push(...localConfig.getTables());
0 commit comments