Skip to content

Commit 2cd830c

Browse files
authored
Merge pull request #1234 from appwrite/sync-tables
2 parents 5970def + 746dcc0 commit 2cd830c

File tree

1 file changed

+61
-1
lines changed

1 file changed

+61
-1
lines changed

templates/cli/lib/commands/push.js.twig

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ const {
5858
tablesDBUpdateTable,
5959
tablesDBList,
6060
tablesDBDelete,
61-
tablesDBListTables
61+
tablesDBListTables,
62+
tablesDBDeleteTable
6263
} = require("./tables-db");
6364
const {
6465
storageGetBucket, storageUpdateBucket, storageCreateBucket
@@ -1884,6 +1885,65 @@ const pushTable = async ({ returnOnZero, attempts } = { returnOnZero: false }) =
18841885
console.log();
18851886
}
18861887

1888+
log('Checking for deleted tables ...');
1889+
const localTablesDBs = localConfig.getTablesDBs();
1890+
const localTables = localConfig.getTables();
1891+
const tablesToDelete = [];
1892+
1893+
for (const db of localTablesDBs) {
1894+
try {
1895+
const { tables: remoteTables } = await paginate(tablesDBListTables, {
1896+
databaseId: db.$id,
1897+
parseOutput: false
1898+
}, 100, 'tables');
1899+
1900+
for (const remoteTable of remoteTables) {
1901+
const localTable = localTables.find(t => t.$id === remoteTable.$id && t.databaseId === db.$id);
1902+
if (!localTable) {
1903+
tablesToDelete.push({
1904+
...remoteTable,
1905+
databaseId: db.$id,
1906+
databaseName: db.name
1907+
});
1908+
}
1909+
}
1910+
} catch (e) {
1911+
// Skip if database doesn't exist or other errors
1912+
}
1913+
}
1914+
1915+
if (tablesToDelete.length > 0) {
1916+
log('Found tables that exist remotely but not locally:');
1917+
const deletionChanges = tablesToDelete.map(table => ({
1918+
id: table.$id,
1919+
action: chalk.red('deleting'),
1920+
key: 'Table',
1921+
database: table.databaseName,
1922+
remote: table.name,
1923+
local: '(deleted locally)'
1924+
}));
1925+
drawTable(deletionChanges);
1926+
1927+
if ((await getConfirmation()) === true) {
1928+
for (const table of tablesToDelete) {
1929+
try {
1930+
log(`Deleting table ${table.name} ( ${table.$id} ) from database ${table.databaseName} ...`);
1931+
await tablesDBDeleteTable({
1932+
databaseId: table.databaseId,
1933+
tableId: table.$id,
1934+
parseOutput: false
1935+
});
1936+
success(`Deleted ${table.name} ( ${table.$id} )`);
1937+
} catch (e) {
1938+
error(`Failed to delete table ${table.name} ( ${table.$id} ): ${e.message}`);
1939+
}
1940+
}
1941+
}
1942+
} else {
1943+
console.log('No tables found to delete');
1944+
}
1945+
console.log();
1946+
18871947
if (cliConfig.all) {
18881948
checkDeployConditions(localConfig);
18891949
tables.push(...localConfig.getTables());

0 commit comments

Comments
 (0)