diff --git a/.github/workflows/export.yml b/.github/workflows/export.yml index 066d3900..e2723e57 100644 --- a/.github/workflows/export.yml +++ b/.github/workflows/export.yml @@ -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: @@ -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: | @@ -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: | diff --git a/bin/Commands/ExportMongoDB.php b/bin/Commands/ExportMongoDB.php index 0b7c0a98..1e2c496f 100644 --- a/bin/Commands/ExportMongoDB.php +++ b/bin/Commands/ExportMongoDB.php @@ -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) { @@ -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); - } }