Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
[run]
source=sql2csv
source = src

[report]
omit =
setup.py
src/__main__.py
src/unittest/*
venv/*
exclude_lines =
if __name__ == '__main__':
main()
if __name__ == .__main__.:
110 changes: 67 additions & 43 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,65 +1,89 @@
name: CI

on: [push, pull_request]
on:
push:
branches: [main]
pull_request:

permissions:
contents: read

jobs:
build:
test:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version: ['3.7', '3.8', '3.9']
python-version: ['3.10', '3.11', '3.12', '3.13', '3.14']

services:
mysql:
image: mysql:8.4
env:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: my_db
ports:
- 3306:3306
options: >-
--health-cmd="mysqladmin ping -h 127.0.0.1 -proot"
--health-interval=5s --health-timeout=5s --health-retries=20
postgres:
image: postgres:17
env:
POSTGRES_USER: db_user
POSTGRES_PASSWORD: db_password
POSTGRES_DB: my_db
ports:
- 5432:5432
options: >-
--health-cmd="pg_isready -U db_user"
--health-interval=5s --health-timeout=5s --health-retries=20

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v5
- name: Set up Python
uses: actions/setup-python@v2
uses: actions/setup-python@v6
with:
python-version: ${{ matrix.python-version }}
architecture: x64
- name: Display Python version
run: python -c "import sys; print(sys.version)"
- name: Set up MySQL
run: |
sudo systemctl start mysql.service
mysql -e 'CREATE DATABASE my_db;' -uroot -proot
- name: Import MySQL schemas
run: |
mysql my_db -uroot -proot < schema/mysql.sql
- name: Start PostgreSQL
run: |
sudo systemctl start postgresql.service
pg_isready
- name: Create PostgreSQL user
run: |
sudo -u postgres psql --command="CREATE USER db_user PASSWORD 'db_password'" --command="\du"
- name: Create PostgreSQL database
run: |
sudo -u postgres createdb --owner=db_user my_db
- name: Import MySQL schema
run: mysql -h 127.0.0.1 -uroot -proot my_db < schema/mysql.sql
- name: Import PostgreSQL schema
run: |
psql -h localhost -f schema/postgresql.sql -d my_db -U db_user
run: psql -h 127.0.0.1 -U db_user -d my_db -f schema/postgresql.sql
env:
PGPASSWORD: db_password
- name: Install deb dependencies
run: |
sudo apt update
sudo apt install pandoc
- name: Install dependencies
run: |
pip install -U pip
pip install pycodestyle coverage pytest pypandoc
python setup.py install
python -m pip install -U pip
python -m pip install pycodestyle coverage pytest
python -m pip install .
- name: Run pycodestyle
run: |
pycodestyle --exclude=venv --ignore=E501 .
run: pycodestyle --exclude=venv,build --ignore=E501 src
- name: Run pytest
run: |
coverage run --source=. -m pytest
- name: Generate coverage report
run: coverage run -m pytest
- name: Coverage report
run: |
coverage report -m
- name: Upload coverage reports to Codecov
coverage xml
- name: Upload coverage to Codecov
if: matrix.python-version == '3.14'
uses: codecov/codecov-action@v5
with:
files: coverage.xml
token: ${{ secrets.CODECOV_TOKEN }}

build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v5
- uses: actions/setup-python@v6
with:
python-version: '3.14'
- name: Build sdist and wheel
run: |
curl -Os https://uploader.codecov.io/latest/linux/codecov
chmod +x codecov
./codecov -t ${CODECOV_TOKEN}
python -m pip install -U build twine
python -m build
twine check --strict dist/*
- uses: actions/upload-artifact@v4
with:
name: dist
path: dist/
43 changes: 43 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
[build-system]
requires = ["setuptools>=77"]
build-backend = "setuptools.build_meta"

[project]
name = "sql2csv"
version = "1.4"
description = "Run MySQL and PostgreSQL queries and store result in CSV"
readme = "README.md"
license = "MIT"
license-files = ["LICENSE"]
authors = [{ name = "Gabriel Bordeaux", email = "pypi@gab.lc" }]
requires-python = ">=3.10"
dependencies = [
"PyMySQL>=1.1",
"psycopg2-binary>=2.9.10",
]
classifiers = [
"Topic :: Database",
"Topic :: Database :: Database Engines/Servers",
"Operating System :: MacOS",
"Operating System :: POSIX :: Linux",
"Natural Language :: English",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
"Programming Language :: Python :: 3.14",
"Development Status :: 4 - Beta",
]

[project.urls]
Homepage = "https://github.com/gabfl/sql2csv"

[project.scripts]
sql2csv = "sql2csv.sql2csv:main"

[tool.setuptools]
packages = ["sql2csv"]
package-dir = { "sql2csv" = "src" }
36 changes: 0 additions & 36 deletions setup.py

This file was deleted.

15 changes: 9 additions & 6 deletions src/sql2csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import sys
import csv
import tempfile
import os
from os.path import expanduser
import json

Expand All @@ -22,7 +23,7 @@ def get_mysql_connection(host, user, port, password, database):
user=user,
port=port,
password=password,
db=database,
database=database,
charset='utf8mb4',
client_flag=pymysql.constants.CLIENT.MULTI_STATEMENTS
)
Expand Down Expand Up @@ -173,12 +174,16 @@ def get_writer(file_, delimiter=',', quotechar='"'):
)


def file_to_stdout():
def file_to_stdout(remove=False):
""" Print file content to stdout """

with open(file_.name) as f:
print(f.read())

# Remove the temporary file once it has been printed
if remove:
os.remove(file_.name)


def stdin_to_csv(delimiter=',', quotechar='"'):
""" Parse stdin and return output in a CSV format """
Expand All @@ -190,8 +195,6 @@ def stdin_to_csv(delimiter=',', quotechar='"'):
# Parse lines and add to file
separator = None
for line in sys.stdin:
# Strip whitespaces
line.strip()

if not discard_line(line):
# Get column separator
Expand All @@ -211,7 +214,7 @@ def stdin_to_csv(delimiter=',', quotechar='"'):
# Write row
writer.writerow(row)

file_to_stdout()
file_to_stdout(remove=True)


def query_to_csv(engine, host, user, port, password, database, query, headers=False, out_type='stdout', destination_file=None, delimiter=',', quotechar='"', print_info=1000):
Expand Down Expand Up @@ -261,7 +264,7 @@ def query_to_csv(engine, host, user, port, password, database, query, headers=Fa

# Print stdout
if out_type == 'stdout':
file_to_stdout()
file_to_stdout(remove=True)


def main():
Expand Down
Loading
Loading