Skip to content

Instantly share code, notes, and snippets.

View jeffjohnson9046's full-sized avatar

Jefe Johnson jeffjohnson9046

View GitHub Profile
@jeffjohnson9046
jeffjohnson9046 / replace-8bit-chars-ascii.py
Created April 8, 2019 05:25
Replace UTF-8 "extended" characters with ASCII equivalents
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import io
"""
Input file might look something like this:
cat input.txt
some ñ thing
foo ñññ
@jeffjohnson9046
jeffjohnson9046 / mvc-add-msg-to-validation-summary.js
Last active November 15, 2018 16:33
How to programmatically add an error message to the MVC ValidationSummary
// Set up an associative array for the error messages. The keys must be the exact name of the
// controls that need to have the error message displayed.
var errorArray = {
SomeProperty: "'SomeProperty' has shit the bed",
SomeOtherProperty: "Whoa - TWO errors? You are up the creek"
};
// Show the errors next to their associated controls
$('#form-to-validate').validate().showErrors(errorArray);
@jeffjohnson9046
jeffjohnson9046 / kill-all-connections-to-db.sql
Created June 18, 2018 18:10
How to kill all connections to a Postgres database
-- Accepted answer from here: https://stackoverflow.com/questions/5408156/how-to-drop-a-postgresql-database-if-there-are-active-connections-to-it
SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = '[your database name goes here]'
AND pid <> pg_backend_pid();
@jeffjohnson9046
jeffjohnson9046 / spring-cloud-config-decrypt-curl.sh
Last active August 5, 2019 23:38
Use the Spring Cloud Config service to encrypt/decrypt an encrypted value
# Encrypt
curl -sbiL -X POST http://localhost:8888/encrypt -d '[the value you want to encrypt]'
# Decrypt
curl -sbiL -X POST http://localhost:8888/decrypt -d '[the encrypted value, without the "{cipher}" prefix]'
@jeffjohnson9046
jeffjohnson9046 / get-or-default.cs
Last active March 14, 2018 16:19
A .NET equivalent of Java's "GetOrDefault()" method
/// <summary>
/// Get a value <code>V</code> for the specified key <code>K</code>. If there is no value for the specified
/// key, return a default value instead.
/// </summary>
/// <remarks>
/// This code emulates the Java <code>GetOrDefault()</code> method on a <code>Map</code>. It's also a convenient
/// way to "inline" the <code>Dictionary#TryParse</code> and have it return some value in the event the key we're
/// looking for doesn't exist.
/// </remarks>
/// <example>
@jeffjohnson9046
jeffjohnson9046 / TransactionRollbackIntegrationTestBase.cs
Last active February 17, 2018 20:06
A C# class for rolling back database modifications after they've happened.
/// <summary>
/// A class for rolling back database transactions after the test has completed.
/// </summary>
[TestClass]
public abstract class TransactionRollbackIntegrationTestBase
{
private TransactionScope transactionScope;
/// <summary>
/// Create the transaction scope to enforce creating a new transaction prior to the test executing.
@jeffjohnson9046
jeffjohnson9046 / mssql-find-foreign-key-references.sql
Created November 10, 2017 22:11
MS SQL Server: for a given table, find all "child" tables that reference it
-- Based on answer from here:
-- https://stackoverflow.com/questions/483193/how-can-i-list-all-foreign-keys-referencing-a-given-table-in-sql-server
SELECT
child.name AS child_table,
fk.constraint_column_id AS fk_part_no,
c.name AS foreign_key_column
FROM
sys.foreign_key_columns AS fk
JOIN
sys.tables child
@jeffjohnson9046
jeffjohnson9046 / find-text-in-stored-procedure.sql
Created November 10, 2017 18:14
MS SQL Server: find text in a stored procedure
-- Second answer from here: https://stackoverflow.com/questions/14704105/search-text-in-stored-procedure-in-sql-server
SELECT
name,
SCHEMA_NAME(schema_id) AS [schema]
FROM
sys.procedures
WHERE
OBJECT_DEFINITION(object_id) LIKE '%whatever text you are looking for%';
@jeffjohnson9046
jeffjohnson9046 / backup-all-user-dbs.sql
Created November 9, 2017 19:17
MS SQL Server: Create full database backups of all "user" databases. This script overwrites the previous backup, so there's only ever one backup of the db. Fine for development, not so much for production.
DECLARE
@backupMsg varchar(max),
@backupPath varchar(max),
@backupCommand varchar(max),
@dbName varchar(max);
SELECT
@backupMsg = 'Full backup',
@backupPath = 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\Backup\';
@jeffjohnson9046
jeffjohnson9046 / alter-index-rebuild-reorganize.sql
Last active March 25, 2024 19:05
Rebuild and reorganize indexes for every table in an MS SQL Server database
-- from here: https://gallery.technet.microsoft.com/scriptcenter/Rebuild-and-Reorganize-7ff5624e
DECLARE
@fragPercentThreshold decimal(11,2),
@schemaName nvarchar(128);
-- Determine maximum fragmentation threshold and the schema to operate against
SET @fragPercentThreshold = 5.0;
SET @schemaName = N'dbo';