Skip to content

Commit

Permalink
feat: try now
Browse files Browse the repository at this point in the history
  • Loading branch information
dr5hn committed Mar 8, 2025
1 parent 0bf32d5 commit f22072f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 91 deletions.
46 changes: 24 additions & 22 deletions .github/workflows/export.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ on:

jobs:
export:
name: JSON/XML/YAML/CSV/MYSQL/PSQL/SQLITE/SQLSERVER
name: JSON/XML/YAML/CSV/MYSQL/PSQL/SQLITE/SQLSERVER/MONGODB
runs-on: ubuntu-24.04

strategy:
Expand Down Expand Up @@ -64,12 +64,18 @@ jobs:
mongodb-version: '6.0'
mongodb-replica-set: rs0

- name: Check MongoDB service
- name: Install MongoDB Database Tools
run: |
# Wait for MongoDB to be ready
sleep 5
echo "Checking MongoDB status..."
mongosh --eval "db.runCommand({ ping: 1 })"
# Add MongoDB package source
wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add -
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu $(lsb_release -cs)/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
# Install only the MongoDB Database Tools package
sudo apt-get update
sudo apt-get install -y mongodb-database-tools
# Verify installation
mongoimport --version
- name: Add clean commands to world.sql
run: |
Expand Down Expand Up @@ -172,29 +178,25 @@ jobs:
- name: Import MongoDB
run: |
cd mongodb
chmod +x import.sh
./import.sh
# Verify data was imported
echo "Verifying imported data..."
mongosh --eval "
db = db.getSiblingDB('world');
print('Regions: ' + db.regions.countDocuments());
print('Subregions: ' + db.subregions.countDocuments());
print('Countries: ' + db.countries.countDocuments());
print('States: ' + db.states.countDocuments());
print('Cities: ' + db.cities.countDocuments());
"
# Wait for MongoDB to be ready
sleep 5
echo "Importing collections..."
mongoimport --host localhost:27017 --db world --collection regions --file regions.json --jsonArray
mongoimport --host localhost:27017 --db world --collection subregions --file subregions.json --jsonArray
mongoimport --host localhost:27017 --db world --collection countries --file countries.json --jsonArray
mongoimport --host localhost:27017 --db world --collection states --file states.json --jsonArray
mongoimport --host localhost:27017 --db world --collection cities --file cities.json --jsonArray
echo "Import completed"
# Create a MongoDB dump
mongodump --db world --out mongodb-dump
mongodump --host localhost:27017 --db world --out mongodb-dump
# Compress the dump
tar -czvf world-mongodb-dump.tar.gz mongodb-dump
echo "MongoDB dump created at mongodb/world-mongodb-dump.tar.gz"
rm -rf mongodb-dump import.sh
rm -rf mongodb-dump
- name: Update README.md
run: |
Expand Down
69 changes: 0 additions & 69 deletions bin/Commands/ExportMongoDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->processStates($io, $rootDir);
$this->processCities($io, $rootDir);

// Create a script to import all collections
$this->createImportScript($io, $rootDir);

$io->success('MongoDB export completed successfully');
return Command::SUCCESS;
} catch (\Exception $e) {
Expand Down Expand Up @@ -253,70 +250,4 @@ private function saveCollection(string $rootDir, string $collection, array $data
$outputFile = "$rootDir/mongodb/$collection.json";
$this->filesystem->dumpFile($outputFile, json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
}

private function createImportScript(SymfonyStyle $io, string $rootDir): void
{
$scriptContent = <<<'BASH'
#!/bin/bash
# MongoDB Import Script
# This script will import all collections into MongoDB
# Configuration
DB_NAME="world"
HOST="localhost"
PORT="27017"
# Function to import a collection
import_collection() {
collection=$1
echo "Importing $collection..."
mongoimport --host $HOST:$PORT --db $DB_NAME --collection $collection --file $(dirname "$0")/$collection.json --jsonArray
echo "Import of $collection completed"
}
# Make sure MongoDB is running
echo "Checking MongoDB connection..."
if ! mongosh --host $HOST:$PORT --eval "db.stats()" > /dev/null; then
echo "Cannot connect to MongoDB. Please make sure MongoDB is running."
exit 1
fi
# Create database if it doesn't exist
mongosh --host $HOST:$PORT --eval "use $DB_NAME"
# Import all collections
import_collection "regions"
import_collection "subregions"
import_collection "countries"
import_collection "states"
import_collection "cities"
# Create indexes for better query performance
echo "Creating indexes..."
mongosh --host $HOST:$PORT --db $DB_NAME --eval '
db.regions.createIndex({ name: 1 });
db.subregions.createIndex({ name: 1 });
db.subregions.createIndex({ "region.$id": 1 });
db.countries.createIndex({ name: 1 });
db.countries.createIndex({ iso2: 1 });
db.countries.createIndex({ iso3: 1 });
db.countries.createIndex({ "region.$id": 1 });
db.countries.createIndex({ "subregion.$id": 1 });
db.states.createIndex({ name: 1 });
db.states.createIndex({ "country.$id": 1 });
db.cities.createIndex({ name: 1 });
db.cities.createIndex({ "state.$id": 1 });
db.cities.createIndex({ "country.$id": 1 });
db.cities.createIndex({ location: "2dsphere" });
'
echo "All collections imported successfully and indexes created"
BASH;

$outputFile = "$rootDir/mongodb/import.sh";
$this->filesystem->dumpFile($outputFile, $scriptContent);
$this->filesystem->chmod($outputFile, 0755);

$io->info('Created MongoDB import script at ' . $outputFile);
}
}

0 comments on commit f22072f

Please sign in to comment.